[llvm-bugs] [Bug 32954] New: -Wthread-safety try_acquire_capability is broken?

via llvm-bugs llvm-bugs at lists.llvm.org
Sun May 7 07:16:15 PDT 2017


https://bugs.llvm.org/show_bug.cgi?id=32954

            Bug ID: 32954
           Summary: -Wthread-safety try_acquire_capability is broken?
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Frontend
          Assignee: unassignedclangbugs at nondot.org
          Reporter: lebedev.ri at gmail.com
                CC: llvm-bugs at lists.llvm.org

When trying to use -Wthread-safety on darktable (~220KLOC), i have encountered
the following problem (minimized):
To be noted, the problem i encountered is when using this in C, but apparently
it happens with C++ too.


// Enable thread safety attributes only with clang.
// The attributes can be safely erased when compiling with other compilers.
#if defined(__clang__) && (!defined(SWIG))
#define THREAD_ANNOTATION_ATTRIBUTE__(x)   __attribute__((x))
#else
#define THREAD_ANNOTATION_ATTRIBUTE__(x)   // no-op
#endif

#define THREAD_ANNOTATION_ATTRIBUTE__(x)   __attribute__((x))

#define CAPABILITY(x)    THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
#define TRY_ACQUIRE(...)
THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
#define RELEASE(...)    
THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))

struct CAPABILITY("mutex") Mutex {};

// like pthread_mutex_trylock(), which it internally calls, 0 means success.
int trylock(Mutex *mutex) TRY_ACQUIRE(0, mutex);

int  unlock(Mutex *mutex) RELEASE(mutex);

Mutex first;
Mutex second;

int main() {
  if(trylock(&first) != 0) {
    return 0;
  }

  if(trylock(&second) != 0) {
    unlock(&first);
    return 0;
  }

  unlock(&second);
  unlock(&first);

  return 0;
}

Which results in warnings:

<source>:26:5: warning: releasing mutex 'first' that was not held
[-Wthread-safety-analysis]
    unlock(&first);
    ^
<source>:30:3: warning: releasing mutex 'second' that was not held
[-Wthread-safety-analysis]
  unlock(&second);
  ^
<source>:31:3: warning: releasing mutex 'first' that was not held
[-Wthread-safety-analysis]
  unlock(&first);
  ^
3 warnings generated.


or equivalently (only the main() is different)

int main() {
  if(trylock(&first) == 0) {
    if(trylock(&second) == 0)
      unlock(&second);

    unlock(&first);
  }

  return 0;
}

<source>:23:7: warning: releasing mutex 'second' that was not held
[-Wthread-safety-analysis]
      unlock(&second);
      ^
<source>:25:5: warning: releasing mutex 'first' that was not held
[-Wthread-safety-analysis]
    unlock(&first);
    ^
2 warnings generated.


This only happens with trylock, regardless of the trylock's return type (bool,
int) i try, and regardless of the first parameter i pass to TRY_ACQUIRE
(0/1/true/false).

The original code that does not yet use these annotations is around here:
https://github.com/darktable-org/darktable/blob/715877a/src/views/darkroom.c#L511-L518

Roman.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20170507/40311353/attachment-0001.html>


More information about the llvm-bugs mailing list