<div dir="ltr">While experimenting with Clang's thread safety analysis (<a href="http://clang.llvm.org/docs/ThreadSafetyAnalysis.html">http://clang.llvm.org/docs/ThreadSafetyAnalysis.html</a>) I ran into a compiler warning I didn't understand. It seems that the release_shared_capability attribute is not doing what I expect. The code to reproduce is below:<div><br></div><div>// Begin file: getvalue.cc</div><div>// Fake mutex class.</div><div><div>class __attribute__((capability("mutex"))) mutex {</div><div>public:</div><div>  void SharedLock() __attribute__((acquire_shared_capability())) {</div><div>    // Do nothing for this fake mutex.</div><div>  }</div><div>  void SharedUnlock() __attribute__((release_shared_capability())) {</div><div>    // Do nothing for this fake mutex.</div><div>  }</div><div>};</div><div><br></div><div>// Scoped lock class to do RAII locking for the fake mutex class above.</div><div>class __attribute__((scoped_lockable)) mutex_lock {</div><div>public:</div><div>  mutex_lock(mutex *Mutex) __attribute__((acquire_shared_capability(Mutex)))</div><div>  : Mutex(Mutex) {</div><div>    Mutex->SharedLock();</div><div>  }</div><div>  ~mutex_lock() __attribute__((release_shared_capability())) {</div><div>    Mutex->SharedUnlock();</div><div>  }</div><div><br></div><div>private:</div><div>  mutex *Mutex;</div><div>};</div><div><br></div><div>// Global mutex and a corresponding value for it to protect.</div><div>mutex Mutex;</div><div>int Value __attribute__((guarded_by(Mutex))) = 42;</div><div><br></div><div>// Explicitly lock and unlock the mutex to read the value.</div><div>int getValueExplicitUnlock() {</div><div>  Mutex.SharedLock();</div><div>  int LocalValue = Value;</div><div>  Mutex.SharedUnlock();</div><div>  return LocalValue;</div><div>}</div><div><br></div><div>// Use the scoped lock class to lock and unlock the mutex.</div><div>// This is where the problem happens.</div><div>int getValueScopedLock() {</div><div>  mutex_lock Lock(&Mutex);</div><div>  return Value;</div><div>}</div></div><div>// End file: getvalue.cc</div><div><br></div><div>Compiling with tip-of-tree clang with the -Wthread-safety flag, I get the following warning:</div><div><br></div><div># Begin shell session</div><div><div>$ clang++ -Wthread-safety -c getvalue.cc</div><div>getvalue.cc:37:10: warning: releasing mutex 'Lock' using shared access, expected exclusive access</div><div>      [-Wthread-safety-analysis]</div><div>  return Value;</div><div>         ^</div><div>1 warning generated.</div></div><div># End shell session</div><div><br></div><div>So there is a problem with my scoped lock in the getValueScopedLock function, but there is no problem with the explicit locking and unlocking in my getValueExplicitUnlock function.</div><div><br></div><div>The warning goes away if I switch the annotation on the ~mutex_lock destructor to "release_capability" instead of "release_shared_capability". Is this the expected behavior? It seems like an "acquire_shared_capability" should be paired with a "release_shared_capability".</div><div><br></div><div>I also found a stack overflow question where someone else ran into this problem: <a href="http://stackoverflow.com/questions/33608378/clang-thread-safety-annotation-and-shared-capabilities">http://stackoverflow.com/questions/33608378/clang-thread-safety-annotation-and-shared-capabilities</a></div><div><br></div></div>