r/ProgrammingLanguages • 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 shared bit 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.), the shared bit 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 owned objects move between threads. Storing them where several threads can reach them is a compile error. Types can be const which 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. const types (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 disallow owned).

Questions

  1. Is "memory safe, but not data-race free" the right trade-off for a simple language?
  2. Implicit sharing hides the fast/slow split. Would you prefer an explicit shared marker, or is tooling enough?
  3. Setting the shared bit is O(n). Can this be a problem?
  4. Has anyone shipped asymmetric fences (membarrier, FlushProcessWriteBuffers) in a language runtime?
  5. Is a non-deterministic close() acceptable for shared objects? Alternatively I could use refcounts here, always.
  6. What did I miss? Prior art I know: CPython free-threading (PEP 703), Lean 4, Koka, Linux RCU.
15 Upvotes

9 comments sorted by

View all comments

Show parent comment

1

u/Tasty_Replacement_29 Bau 7h ago

> Immutable values

Yes, I came to the same conclusion. I want to support them in my language as well. But they need to be really immutable, and not like in Java where you can change array values... But for my language, I want the default to be mutable.

> No silver bullet

Yes, there is a trade-off. I want the default to be "easy to use", and if performance improvement are needed, it means more work.

> Setting the shared bit is O(n)
> there's also a memory cost here

Yes. The same problem exists if you delete (drop) a large linked list. In Rust, this will panic (stack overflow) unless if drop is implemented in user code. In my language, I have implemented a stack-safe deallocation algorithm that is a hybrid. I think the same will be need to set the shared bit.

> A quick hack may be to expose a shared function

Yes, that makes sense, thanks!

>> Is a non-deterministic close() acceptable for shared objects?

So my language allows to define a close() method per type; I think many language support something like that (finalizer in Java, etc.). Yes, this is for files etc. I think it's fine if there are some limitations and performance issues in this case. For example, re-linking the object in the close method results in panic, same in in Swift, and probably other languages. (Java allows to do this.) Starting a thread, or even sharing stuff, in the close() method is probably not a good idea either. This method needs some hard limitations I guess, otherwise it will be very very tricky to get right.

> I'd be wary of sharing file handles across threads, in general

It's probably best if some types may not be shared at all... That's a good point.