<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - -Wthread-safety try_acquire_capability is broken?"
   href="https://bugs.llvm.org/show_bug.cgi?id=32954">32954</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>-Wthread-safety try_acquire_capability is broken?
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Frontend
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>lebedev.ri@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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:
<a href="https://github.com/darktable-org/darktable/blob/715877a/src/views/darkroom.c#L511-L518">https://github.com/darktable-org/darktable/blob/715877a/src/views/darkroom.c#L511-L518</a>

Roman.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>