[LLVMdev] Multi-threading and mutexes in LLVM

Aaron Ballman aaron at aaronballman.com
Sat Jun 7 07:26:29 PDT 2014


On Fri, Jun 6, 2014 at 9:47 PM, Zachary Turner <zturner at google.com> wrote:
> +chandlerc, aaronballman, in case there are additional carryovers and/or
> issues from the review thread which I've left out.
>
>
> I have a patch up for review[1, 2] that attempts to replace LLVM's mutex
> implementation with std::mutex and std::recursive_mutex.  While the patch
> seems to work, there are questions surrounding whether or not the approach
> used is correct.
>
> I'll try to summarize the issues as best I can, in hopes of getting some
> feedback from a broader audience, to make sure this is done correctly:
>
> 1) Should support multi-threading be a compile-time or runtime parameter in
> LLVM?
>
> Currently it is both.  It is compile-time through the use of the define
> LLVM_ENABLE_THREADS, and it is runtime through the use of functions
> llvm_start_multithreaded, llvm_is_multithreaded, etc.  I and some others
> feel like runtime support for multi-threading could be removed, and it
> should be compile-time only.  However, I am not aware of all the ways in
> which this is being used, so this is where I would like some feedback.  The
> issues I have with runtime multithreading support are the following:
>
> * It leads to confusing code.  At any given point, is multi-threading
> enabled or disabled?  You never know without calling llvm_is_multithreaded,
> but even calling that is inherently racy, because someone else could disable
> it after it returns.
>
> * It leads to subtle bugs.  clang_createIndex, the first time it's called,
> enables multi-threading.  What happens if someone else disables it later?
> Things like this shouldn't even be possible.
>
> * Not all platforms even support threading to begin with.  This works now
> because llvm_start_multithreaded(), if the compile time flag is set to
> disable threads, simply does nothing.  But this decision should be made by
> someone else.  Nobody really checks the return value from
> llvm_start_multithreaded anyway, so there's already probably bugs where
> someone tries to start multi-threading, and it fails.
>
> * What does it actually mean to turn multi-threading support on and off?
>
> Anybody that tries to do this is almost certainly broken due to some edge
> cases about when it's on and when it's off.  So this goes back to the first
> two points about confusing code and subtle bugs.

I agree with your assessment, and my personal feeling is that this
should be a compile-time switch. Code which attempts to do this at
runtime often has subtle bugs.

>
> 2) What should happen when you try to acquire a mutex in an app with
> threading disabled?
>
> If this is a compile-time parameter, the solution is simple: make an empty
> mutex class that satisfies the Lockable concept, and have its methods do
> nothing.  Then typedef something like llvm::mutex to be either std::mutex or
> llvm::null_mutex accordingly.  If this is a runtime parameter, it's more
> complicated, and we should ask ourselves whether or not it's necessary to
> avoid the overhead of acquiring an uncontended mutex in single threaded
> apps.  For what it's worth, all reasonable STL implementations will use a
> lightweight mutex implementation, which generally require less than 100
> nanoseconds to acquire uncontended, so I think we don't need to care, but
> again some feedback would be nice.

I agree with the compile time approach.

>
> 3) We have some debug code in our mutex implementation that is intended to
> try to help catch deadlocks and/or race conditions.  Do we need this code?
>
> I think we can get by without it, but again some feedback about how, if at
> all, people are using this would be nice.  For example, if you want to
> detect deadlocks and race conditions you can use TSan.

I don't think the code is needed (I believe TSan suffices), but is it
possible that there are platforms for which TSan is unsupported and
this code may prove useful? I'm not certain. As Chandler points out, I
think supporting this at the libc++ level may actually be more
profitable.

~Aaron



More information about the llvm-dev mailing list