Skip to content

Instantly share code, notes, and snippets.

View timboudreau's full-sized avatar

Tim Boudreau timboudreau

View GitHub Profile
@timboudreau
timboudreau / IOPolicies.swift
Created September 14, 2025 08:30
A friendly wrapper around `setiopolicy_np` for iOS / Mac OS
/// A friendly wrapper around `setiopolicy_np`.
public enum IOPolicies {
case DontResolveSymlinks
case DiskIONormal
case DiskIOPassive
case DiskIOThrottled
case DiskIOImportant
case MaterializeVirtualFiles
case DontMaterializeVirtualFiles
case BackgroundIOImportant

Fun With Posix Threads

Sometimes you really need to be able to abort something that doesn't have abort semantics - in my case, attempts to read files from iCloud containers mid-download on iOS, which can hang until process exit if you initiate them at exactly the wrong time.

So, POSIX's pthread_cancel() to the rescue, with a bunch of attendant boilerplate.

Your project's settings will need some adjustment - allow non-modular includes, defines-module set to YES, a project header file with the required boilerplate, and

In my case, this all lives in a framework project called AppUtils - if it is in the app project, proper, you can use an Objective C bridging header in place of the module map.

@timboudreau
timboudreau / TreiberishStack.swift
Created July 20, 2025 05:12
A Treiber-ish stack in Swift
import Atomics
/// A thread-safe, lockless, concurrent sequence which can be appended to and drained.
/// This is not quite a Treiber stack in the sense that single elements cannot
/// be **popped** off of it - Treiber stacks rely on every interaction with a given memory
/// address being a *single atomic read, write or exchange operation*. The Swift atomics
/// package is a bit too anemic for that - we would need to be able to exchange a pointer
/// to the head cell with a pointer to the head cell's child (if any) without reading the
/// head pointer twice. That's implementable in C or assembly, but not with the access
/// Swift atomics give us.
private final class DelayedRegistration : Sendable {
static let singleton = DelayedRegistration()
private let queue : TreiberishStack<DREntry> = .init()
private let enqueued = ManagedAtomic(false)
struct DREntry : @unchecked Sendable {
weak var stub : FolderMonitorStub?
weak var monitor : (any ChangeMonitor)?
func withValues(_ f : (FolderMonitorStub, any ChangeMonitor) -> Void) -> Bool {
@timboudreau
timboudreau / TreiberishStack.swift
Last active July 20, 2025 05:24
A not-quite Treiber stack using Swift's Atomics package
import Atomics
/// A thread-safe, lockless, concurrent sequence which can be appended to and drained.
/// This is not quite a Treiber stack in the sense that single elements cannot
/// be **popped** off of it - Treiber stacks rely on every interaction with a given memory
/// address being a *single atomic read, write or exchange operation*. The Swift atomics
/// package is a bit too anemic for that - we would need to be able to exchange a pointer
/// to the head cell with a pointer to the head cell's child (if any) without reading the
/// head pointer twice. That's implementable in C or assembly and a memory offset, but not with the access
/// Swift atomics give us.
//! A rough cut of how to build a processing graph for samples.
//! I omitted dealing with multiple channels via const generics, as it
//! complicates things and the point here is just how you'd chain things up.
/// Takes a vector of samples, does horrible things to them and returns the result.
fn processing_demo(input: Vec<f32>) -> Vec<f32> {
let mut graph = demo_graph(Vec::with_capacity(input.len()));
graph.process_audio(input.iter());
graph.close()
}
@timboudreau
timboudreau / main.rs
Created May 25, 2024 08:10
Floem inter-window messaging bug
use floem::{
close_window,
context::ComputeLayoutCx,
event::EventPropagation,
keyboard::NamedKey,
kurbo::{Rect, Size},
new_window,
reactive::{create_rw_signal, ReadSignal, RwSignal},
style::{Background, Style, TextColor, TextOverflow},
taffy::{
package com.mastfrog.lambda.gc;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public class LambdaGCTest {
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.image.VolatileImage;
@timboudreau
timboudreau / Data.java
Created December 6, 2013 15:06 — forked from jtulach/Data.java
Histogram in Java and HTML+CSS
package dew.demo.histogram;
import java.util.ArrayList;
import java.util.List;
import net.java.html.json.ComputedProperty;
import net.java.html.json.Model;
import net.java.html.json.Property;
/** Model annotation generates class Data with
* one property for list of of numbers and read-only property