[llvm-commits] [PATCH] Thread-safe ManagedStatic
Chris Lattner
clattner at apple.com
Tue May 19 03:08:30 PDT 2009
On May 18, 2009, at 6:25 PM, Owen Anderson wrote:
> So, now that we have the basic infrastructure more or less in place,
> here's another attempt at making a thread-safe version of
> ManagedStatic based on double-checked locking with explicit fences.
> It also proved necessary to provide atomic update facilities for the
> global StaticList as well.
>
> Please see the included block comment for more details on exactly
> what safety guarantees this code provides.
The basic algorithm looks fine, however:
1. Please move the meat of the code for LazyInit out of line somehow.
Only the case when tmp != 0 should be inline ideally.
2. Again, we only need a single global lock, we do not need a mutex
per ManagedStatic.
3. Why do you create your own mutex out of atomic operations instead
of just using a pthreads lock? spinning is not always an efficient
solution to contention, particularly if your machine really only has
one cpu! (in which case you end up spinning away the rest of your
timeslice)
4. Instead of going through the trouble to make the locking more
efficient, I think it would be good to special case the scenario when
there is only one LLVM thread. Why not make clients explicitly opt-in
to multithreaded llvm, by making an explicit llvm_multithread call?
If multithreading is disabled, this call should return an error. If
enabled, it would set a global. All the various "locking" clients
could just check the global before taking potential heavy-weight locks.
-Chris
More information about the llvm-commits
mailing list