dim added a subscriber: ed.
dim added a comment.

In https://reviews.llvm.org/D28520#646160, @aaron.ballman wrote:

> I feel like I must be missing something; why is this disabling rather than 
> specifying the thread safety behavior? e.g., `__libcpp_mutex_lock()` 
> specifying that it acquires the capability and `__libcpp_mutex_unlock()` 
> specifying that it releases it?


I wasn't able to figure out how that should work.  The Thread Safety Analysis 
documentation specifies a number of macros that can be used, but does not 
directly document the attributes themselves.  It looks like some of the 
attributes have a slightly different signature than the actual pthread 
functions, and the `__libcpp` wrapper for them, e.g. the example for 
`TRY_ACQUIRE` seems to assume a `bool` return value:

  // Try to acquire the mutex.  Returns true on success, and false on failure.
  bool TryLock() TRY_ACQUIRE(true);

where `TRY_ACQUIRE(true)` gets replaced by `__attribute__ 
((try_acquire_capability(true)))`.  However, the signature for the `__libcpp` 
variant, and the "real" pthread function, is:

  int __libcpp_mutex_trylock(__libcpp_mutex_t *__m)

so the return value is an `int`, where `0` means success, and any other value 
an error.  I am unsure how this can be mapped to the attribute, and the 
documentation does not specify it.

In https://reviews.llvm.org/D28520#646169, @EricWF wrote:

> Also how is `pthread_mutex_t` getting annotated as a mutex type? Is it now 
> done automatically?


Since a few years, FreeBSD's <pthread.h> has locking annotations, for example 
https://svnweb.freebsd.org/base/head/include/pthread.h?view=markup#l228 which 
has:

  int         pthread_mutex_init(pthread_mutex_t *__mutex,
                          const pthread_mutexattr_t *)
                      __requires_unlocked(*__mutex);
  int         pthread_mutex_lock(pthread_mutex_t *__mutex)
                      __locks_exclusive(*__mutex);
  int         pthread_mutex_trylock(pthread_mutex_t *__mutex)
                      __trylocks_exclusive(0, *__mutex);
  int         pthread_mutex_timedlock(pthread_mutex_t *__mutex,
                          const struct timespec *)
                      __trylocks_exclusive(0, *__mutex);

These annotations expand to `__attribute__((locks_excluded))`, 
`__attribute__((exclusive_lock_function))`, and so on, if 
`__has_extension(c_thread_safety_attributes)` is true.  Apparently @ed added 
these.


https://reviews.llvm.org/D28520



_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to