[llvm-bugs] [Bug 38908] New: Thread Annotation doesn't recognize a mutex is hold

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Sep 12 02:05:20 PDT 2018


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

            Bug ID: 38908
           Summary: Thread Annotation doesn't recognize a mutex is hold
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Static Analyzer
          Assignee: dcoughlin at apple.com
          Reporter: mendola at gmail.com
                CC: llvm-bugs at lists.llvm.org

Compiling the following snippet the compiler is not able to recognize that the
privateAddComponent caller is holding the mutex:


#include <iostream>
#include <mutex>
#include <optional>
#include <map>

#define THREAD_ANNOTATION_ATTRIBUTE(x) __attribute__((x))

#define MUTEX_CAPABILITY      THREAD_ANNOTATION_ATTRIBUTE(capability("mutex"))
#define ACQUIRE_CAPABILITY(m)
THREAD_ANNOTATION_ATTRIBUTE(acquire_capability(m))
#define SCOPED_LOCKABLE       THREAD_ANNOTATION_ATTRIBUTE(scoped_lockable)
#define RELEASE_CAPABILITY    THREAD_ANNOTATION_ATTRIBUTE(release_capability())
#define GUARDED_BY(x)         THREAD_ANNOTATION_ATTRIBUTE(guarded_by(x))
#define EXCLUDES(...)        
THREAD_ANNOTATION_ATTRIBUTE(locks_excluded(__VA_ARGS__))

namespace blp {

using mutex MUTEX_CAPABILITY = std::mutex;

template <class T>
class SCOPED_LOCKABLE unique_lock
{
 public:
  explicit unique_lock(T& aMutex) ACQUIRE_CAPABILITY(aMutex)
      : theLock(aMutex) {
  }

  ~unique_lock() RELEASE_CAPABILITY = default;

  void foo(T& aMutex) EXCLUDES(aMutex) { }

 private:
  std::unique_lock<T> theLock;
};

}

class TSCollection {
 public:
  void addComponent(int aQuery, int aReply) EXCLUDES(theMutex) {
    blp::unique_lock<blp::mutex> myGuard(theMutex);
    privateAddComponent(aQuery, aReply);
  }

  void addDefaultComponent(int aQuery) EXCLUDES(theMutex) {
    blp::unique_lock<blp::mutex> myGuard(theMutex);
    privateAddComponent(aQuery, 42);
  }

  std::optional<int> component(int aQuery) const EXCLUDES(theMutex) {
    blp::unique_lock<blp::mutex> myGuard(theMutex);
    auto it = theCollection.find(aQuery);
    return it != 
        theCollection.end() ? std::optional<int>(it->second) : std::nullopt;
  }
 private:
  void privateAddComponent(int aQuery, int aReply) {
    theCollection[aQuery] = aReply;
  }

  mutable blp::mutex theMutex;
  std::map<int, int> theCollection GUARDED_BY(theMutex);
};

int main() {
  std::cout << "hello" << std::endl;
}

-- 
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/20180912/0dc05418/attachment.html>


More information about the llvm-bugs mailing list