r/ProgrammingLanguages • u/Tasty_Replacement_29 Bau • 19h ago
Requesting criticism Multithreading for a Reference-Counted Language without Annotations
I'm working on "Bau" (link in a comment), a small, memory-safe systems language that transpiles to C. It uses reference counting and automatic borrow (most reads don't touch counts). Types declared owned have a single owner and no ref count, and are faster. I want to add multithreading support, and would like to get feedback on my design.
Goals
- No annotations, lifetimes,
Send/Sync,async. Any thread can read and write any object. - Memory safe, but data races are allowed, as in Java.
- Single-threaded programs have no overhead; a small overhead for shared objects.
How it should work
- Each object has a hidden
sharedbit in the ref-count field. Objects start local: non-atomic counts, as today. When an object is stored where another thread can reach it (global, channel, etc.), thesharedbit is set in this and all referenced objects, recursively. I think this is like Lean 4. I know setting the bit can take time (same as freeing up a large graph recursively). - Shared objects use atomic counts, and atomic exchanges for pointer stores.
- Borrowed reads stay count-free and lock-free. To do this, if the ref count of a shared object becomes zero, objects are not immediately freed, but only after a grace period, similar to Linux RCU.
- Epoch based reclamation (EBR) is used. There are no safepoint polls (as in QSBR), but there is a bookkeeper thread that keeps track of the epoch and uses a process-wide barrier (
membarrier). - Mutable
ownedobjects move between threads. Storing them where several threads can reach them is a compile error. Types can beconstwhich means all fields are immutable after construction; those can be shared freely.
Compared to...
- Go: Go isn't memory safe under data races (torn interfaces and slices), and has a tracing GC.
- Swift: uses atomic counts always, not only for shared objects.
- Rust: needs lifetimes and
Send/Sync,Rc,Arc. My language doesn't need those annotations. - Java: has the same safety contract, but uses more memory and has pauses due to tracing GC.
- Lean 4, Koka: my language is primarily imperativ.
Disadvantages
- Not data-race free: racy programs can give weird / wrong answers, as in Java.
consttypes (which are opt-in) can prevent this. - Sharing is invisible: one store into a global can make a large graph shared, and slower.
close()of a shared object may run on another thread, a few milliseconds late. Or then, is bit slower (never borrows, and disallowowned).
Questions
- Is "memory safe, but not data-race free" the right trade-off for a simple language?
- Implicit sharing hides the fast/slow split. Would you prefer an explicit
sharedmarker, or is tooling enough? - Setting the shared bit is O(n). Can this be a problem?
- Has anyone shipped asymmetric fences (
membarrier,FlushProcessWriteBuffers) in a language runtime? - Is a non-deterministic
close()acceptable for shared objects? Alternatively I could use refcounts here, always. - What did I miss? Prior art I know: CPython free-threading (PEP 703), Lean 4, Koka, Linux RCU.
15
Upvotes
12
u/stumpychubbins 18h ago
My two cents is that preventing data races should be the absolute bare minimum for a modern language and it’s embarrassing that Go doesn’t. If I were you, I’d use Swift's approach for now and put together a basic design for something more complex as a follow-up. Always-atomic can serve as a baseline that you can benchmark other approaches against.
4
u/garagedragon 16h ago
I don't see how you can maintain memory safety if you're not tracking whether a read borrow of a shared object still exists before you reclaim it. "Only after a grace period" is going to reduce the chances of something going wrong, but can't eliminate the possibility that say, a thread holding a borrow sleeps for a long time (perhaps waiting on something else) before trying to use it.
If you're preventing this by having a distinction between the borrow and the "owning" shared reference ala Rust's Arc, you also need to ensure the former can't outlive the latter.
2
u/pthierry 12h ago
What is the goal of your language if it doesn't guarantee the absence of data races? That is, if I want to write a concurrent program that actually works correctly, why would I choose your language?
2
u/matthieum 8h ago
Honestly, to keep the language simple, I see only two possibilities:
The latter is even simpler, but it's a significantly different style too.
sharedintroduces complexity at the language level, and with it ergonomics issues.So you're trading off ergonomics issues at writing/reading time vs ergonomics issues at performance investigation time. No silver bullet.
At least it's linear...
... still, I do want to point out that the cost isn't just time, there's also a memory cost here. Whether you use a recursive approach, or an explicit stack approach, you'll need to track what's left to visit, and that could go up to O(N) too.
So, yes, it can be a problem in specific cases.
A quick hack may be to expose a
sharedfunction, allowing the user to eagerly mark an object as shared (even if it's invisible in the type system), so they can build objects to share directly as shared, and NOT pay the cost later.I'm not quite sure I understand the question. Are you talking about explicit
closeon files vs finalizers?I'd be wary of sharing file handles across threads, in general. Those file handles regularly "hide" state: cursors, buffers, etc... so that using them from multiple-threads is a recipe for failure, if not outright Undefined Behavior. A mutex if shared would avoid UB, but the race-conditions could still be... very annoying to debug.
1
u/Tasty_Replacement_29 Bau 19h ago
Link the the programming language: https://github.com/thomasmueller/bau-lang