[llvm-commits] [llvm] r119718 - /llvm/trunk/lib/System/DynamicLibrary.cpp

Chris Lattner clattner at apple.com
Mon Nov 22 19:06:37 PST 2010


On Nov 18, 2010, at 10:49 AM, Owen Anderson wrote:

> Author: resistor
> Date: Thu Nov 18 12:49:05 2010
> New Revision: 119718
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=119718&view=rev
> Log:
> Use thread-safe statics to avoid a static constructor here.  This isn't thread-safe on MSVC, but we don't
> support threaded LLVM there anyways.

Thanks!  Not to be a pain, but this will cause a leak of the new'd mutex.  Why not just do something like:

static SmartMutex<true> &getHandlesMutex() {
  static SmartMutex<true> R;
  return R;
}


...
SmartScopedLock<true> Lock(getHandlesMutex());
...

-Chris

> 
> Modified:
>    llvm/trunk/lib/System/DynamicLibrary.cpp
> 
> Modified: llvm/trunk/lib/System/DynamicLibrary.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/DynamicLibrary.cpp?rev=119718&r1=119717&r2=119718&view=diff
> ==============================================================================
> --- llvm/trunk/lib/System/DynamicLibrary.cpp (original)
> +++ llvm/trunk/lib/System/DynamicLibrary.cpp Thu Nov 18 12:49:05 2010
> @@ -61,9 +61,19 @@
> //===          independent code.
> //===----------------------------------------------------------------------===//
> 
> -static SmartMutex<true> HandlesMutex;
> +static SmartMutex<true>* HandlesMutex;
> static std::vector<void *> *OpenedHandles = 0;
> 
> +static bool InitializeMutex() {
> +  HandlesMutex = new SmartMutex<true>;
> +  return HandlesMutex != 0;
> +}
> +
> +static bool EnsureMutexInitialized() {
> +  static bool result = InitializeMutex();
> +  return result;
> +}
> +
> 
> bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
>                                             std::string *ErrMsg) {
> @@ -78,7 +88,8 @@
>   if (Filename == NULL)
>     H = RTLD_DEFAULT;
> #endif
> -  SmartScopedLock<true> Lock(HandlesMutex);
> +  EnsureMutexInitialized();
> +  SmartScopedLock<true> Lock(*HandlesMutex);
>   if (OpenedHandles == 0)
>     OpenedHandles = new std::vector<void *>();
>   OpenedHandles->push_back(H);
> @@ -113,7 +124,8 @@
> 
> #if HAVE_DLFCN_H
>   // Now search the libraries.
> -  SmartScopedLock<true> Lock(HandlesMutex);
> +  EnsureMutexInitialized();
> +  SmartScopedLock<true> Lock(*HandlesMutex);
>   if (OpenedHandles) {
>     for (std::vector<void *>::iterator I = OpenedHandles->begin(),
>          E = OpenedHandles->end(); I != E; ++I) {
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits





More information about the llvm-commits mailing list