Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 1
    A problem with fine-grained locks is that it is extremely easy to produce deadlocks. For this to work efficiently and safely, there would have to be extra rules like "every thread may lock at most 1 GC object at a time" and "GC object locks must only be held for a short duration". Only the GC system itself would be exempt. But now these fine-grained locks are more like a single coarse-grained lock, and we have arrived at something like CPython's GIL. Commented Feb 17, 2024 at 15:54
  • I should mention since not everyone knows. Reader-writer lock is an actual thing! Such as the one defined in POSIX, as well as the one implemented in Mr Butenhof's book for illustration. Commented Feb 17, 2024 at 23:21
  • IIRC the writer lock cannot be acquired unless all reader locks have been released. That's kind of the point of the reader-writer lock. So i don't think the problem you're describing should occur with a properly implemented lock. However, this will also prevent you from doing the stop the world event, because the writer lock only locks when nobody is using your objects. Executing arbitrary code within locks is generally not a good idea. Commented Feb 18, 2024 at 0:16
  • @Ccm "writer lock cannot be acquired unless all reader locks have been released" - Yes, but will the thread acquiring the writer lock see all the changes made by threads that held reader lock? Especially on NUMA architectures? I'm asking this from a memory-order sematic point of view. Commented Feb 18, 2024 at 0:48
  • 2
    I think your concern about the insufficient fence is valid, but this illustrates that the scenario is inherently unsound. (1) You want a conventional rwlock but want the "read" lock to order writes. That doesn't make sense. But you could use the same techniques as an rwlock to implement a threads-xor-gc lock that does guarantee suitable memory order. (2) You imply that objects can be shared between threads. If they modify the object without explicit synchronization, that's already unsound – regardless of GC strategy. Commented Feb 18, 2024 at 6:49