Multithreading – clang thread safety annotations and sharing capabilities

When I use clang thread annotations, the following code generates a warning I'm trying to wrap boost:: shared_ Mutex and boost:: shared_ lock. How do I use thread annotations to indicate that this lock is a shared lock?

Source code:

#include <mutex>
#include "boost/thread/shared_mutex.hpp"

class __attribute__((shared_capability("mutex"))) BoostSharedMutex {
public:
  boost::shared_mutex &getNativeHandle() { return m_mutex; }
private:
  mutable boost::shared_mutex m_mutex;
};

class __attribute__((scoped_lockable)) MutexSharedLock {
public:
    explicit MutexSharedLock(BoostSharedMutex &mutex) __attribute__((acquire_shared_capability(mutex)))
        : m_lock(mutex.getNativeHandle()) {}
    ~MutexSharedLock() __attribute__((release_shared_capability())) = default;

private:
    boost::shared_lock<boost::shared_mutex> m_lock;
};


int main() {
  BoostSharedMutex mutex;
  MutexSharedLock lock(mutex);
}

Sonorous output:

clang++-3.6 --std=c++11 -Wall -Wthread-safety /tmp/foo.cpp -lboost_system
/tmp/foo.cpp:25:5: warning: releasing mutex 'lock' using shared access,expected exclusive access [-Wthread-safety-analysis]
    }
    ^
1 warning generated.

Editor: this compilation, but it seems wrong Is that my problem?

#include <mutex>
#include "boost/thread/shared_mutex.hpp"

class __attribute__((shared_capability("mutex"))) BoostSharedMutex {
public:
  boost::shared_mutex &getNativeHandle() { return m_mutex; }
private:
  mutable boost::shared_mutex m_mutex;
};

class __attribute__((scoped_lockable)) MutexSharedLock {
public:
    explicit MutexSharedLock(BoostSharedMutex &mutex) __attribute__((acquire_capability(mutex))) // changed  from acquired_shared_capability
        : m_lock(mutex.getNativeHandle()) {}
    ~MutexSharedLock() __attribute__((release_capability())) = default; // changed from release_shared_capability

private:
    boost::shared_lock<boost::shared_mutex> m_lock;
};

BoostSharedMutex mutex;
int locked_variable __attribute__((guarded_by(mutex)));

int main() {
  MutexSharedLock lock(mutex);
  std::cout << locked_variable << std::endl; // ok,guarded variable is only read
  locked_variable = 42; // no warning while writing in the guarded variable while only holding a non-exclusive lock?
}

Solution

This seems to work after trying several combinations:

#include <mutex>
#include "boost/thread/shared_mutex.hpp"

class __attribute__((capability("mutex"))) BoostSharedMutex {
public:
  boost::shared_mutex &getNativeHandle() { return m_mutex; }
private:
  mutable boost::shared_mutex m_mutex;
};

class __attribute__((scoped_lockable)) MutexSharedLock {
public:
    explicit MutexSharedLock(BoostSharedMutex &mutex) __attribute__((acquire_shared_capability(mutex)))
        : m_lock(mutex.getNativeHandle()) {}
    ~MutexSharedLock() __attribute__((release_capability())) = default;

private:
    boost::shared_lock<boost::shared_mutex> m_lock;
};

class __attribute__((scoped_lockable)) MutexLock {
public:
    explicit MutexLock(BoostSharedMutex &mutex) __attribute__((acquire_capability(mutex)))
        : m_lock(mutex.getNativeHandle()) {}
    ~MutexLock() __attribute__((release_capability())) = default;

private:
    std::unique_lock<boost::shared_mutex> m_lock;
};

BoostSharedMutex mutex;
int locked_variable __attribute__((guarded_by(mutex)));

int main() {
  {
    MutexSharedLock lock(mutex);
    std::cout << locked_variable << std::endl;
    // locked_variable = 42; -- triger a error as expected
  }
  {
    MutexLock lock(mutex);
    std::cout << locked_variable << std::endl;
    locked_variable = 42;
  }
}

I wonder why mutexsharedlock should use acquire_ shared_ Capability but release_ capability …

(if anyone can confirm that the code is correct, I will open this problem)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>