diff --git a/include/sled/lang/attributes.h b/include/sled/lang/attributes.h index 6e06b92..b91befa 100644 --- a/include/sled/lang/attributes.h +++ b/include/sled/lang/attributes.h @@ -1,5 +1,41 @@ #pragma once #ifndef SLED_LANG_ATTRIBUTES_H #define SLED_LANG_ATTRIBUTES_H -#define SLED_DEPRECATED __attribute__((deprecated)) + +#define SLED_DEPRECATED() __attribute__((deprecated)) +#if defined(__GNUC__) && defined(__SUPPORT_TS_ANNOTATION__) && !defined(SWIG) +#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) +#elif defined(__clang__) +#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) +#else +#define THREAD_ANNOTATION_ATTRIBUTE__(x) +#endif + +#if defined(GUARDED_BY) +#undef GUARDED_BY +#endif +#define GUARDED_BY(x) __attribute__((guarded_by(x))) + +#if defined(__clang__) +#define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__)) + +#define EXCLUSIVE_LOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__)) + +#define UNLOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__)) + +#define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) + +#else + +#define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock(__VA_ARGS__)) +#define EXCLUSIVE_LOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock(__VA_ARGS__)) +#define UNLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(unlock(__VA_ARGS__)) +#define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded_by(x)) +#endif + #endif// SLED_LANG_ATTRIBUTES_H diff --git a/include/sled/synchronization/event.h b/include/sled/synchronization/event.h index 1b4c4bc..fd0881f 100644 --- a/include/sled/synchronization/event.h +++ b/include/sled/synchronization/event.h @@ -36,7 +36,7 @@ public: Mutex mutex_; ConditionVariable cv_; const bool is_manual_reset_; - bool event_status_; + bool event_status_ GUARDED_BY(mutex_); }; }// namespace sled diff --git a/include/sled/synchronization/mutex.h b/include/sled/synchronization/mutex.h index 3ff109c..673addc 100644 --- a/include/sled/synchronization/mutex.h +++ b/include/sled/synchronization/mutex.h @@ -5,10 +5,10 @@ **/ #pragma once -#include "marl/conditionvariable.h" #ifndef SLED_SYNCHRONIZATION_MUTEX_H #define SLED_SYNCHRONIZATION_MUTEX_H +#include "marl/conditionvariable.h" #include "sled/lang/attributes.h" #include "sled/units/time_delta.h" #include @@ -61,13 +61,13 @@ public: RecursiveMutex(const RecursiveMutex &) = delete; RecursiveMutex &operator=(const RecursiveMutex &) = delete; - inline void Lock() { impl_.lock(); } + inline void Lock() EXCLUSIVE_LOCK_FUNCTION() { impl_.lock(); } inline bool TryLock() { return impl_.try_lock(); } inline void AssertHeld() {} - inline void Unlock() { impl_.unlock(); } + inline void Unlock() UNLOCK_FUNCTION() { impl_.unlock(); } private: std::recursive_mutex impl_; @@ -76,14 +76,17 @@ private: template::value, TLock>::type * = nullptr> -class SLED_DEPRECATED LockGuard final { +class SLED_DEPRECATED() LockGuard final { public: LockGuard(const LockGuard &) = delete; LockGuard &operator=(const LockGuard &) = delete; - explicit LockGuard(TLock *lock) : mutex_(lock) { mutex_->Lock(); }; + explicit LockGuard(TLock *lock) EXCLUSIVE_LOCK_FUNCTION() : mutex_(lock) + { + mutex_->Lock(); + }; - ~LockGuard() { mutex_->Unlock(); }; + ~LockGuard() UNLOCK_FUNCTION() { mutex_->Unlock(); }; private: TLock *mutex_; @@ -102,11 +105,11 @@ private: marl::lock lock_; }; -using MutexGuard SLED_DEPRECATED = MutexLock; +using MutexGuard SLED_DEPRECATED() = MutexLock; // using MutexGuard = marl::lock; // using MutexLock = LockGuard; // using MutexGuard = LockGuard; -using RecursiveMutexLock SLED_DEPRECATED = LockGuard; +using RecursiveMutexLock SLED_DEPRECATED() = LockGuard; // class MutexLock final { // public: diff --git a/include/sled/synchronization/one_time_event.h b/include/sled/synchronization/one_time_event.h index 4bd31e9..a1a50ed 100644 --- a/include/sled/synchronization/one_time_event.h +++ b/include/sled/synchronization/one_time_event.h @@ -25,7 +25,7 @@ public: } private: - bool happended_ = false; + bool happended_ GUARDED_BY(mutex_) = false; Mutex mutex_; }; diff --git a/include/sled/system/thread.h b/include/sled/system/thread.h index 1466893..084f1ae 100644 --- a/include/sled/system/thread.h +++ b/include/sled/system/thread.h @@ -159,9 +159,9 @@ private: void ClearCurrentTaskQueue(); mutable Mutex mutex_; - std::queue> messages_; - std::priority_queue delayed_messages_; - uint32_t delayed_next_num_; + std::queue> messages_ GUARDED_BY(mutex_); + std::priority_queue delayed_messages_ GUARDED_BY(mutex_); + uint32_t delayed_next_num_ GUARDED_BY(mutex_); bool fInitialized_; bool fDestroyed_; std::atomic stop_; @@ -183,8 +183,9 @@ public: AutoSocketServerThread(const AutoSocketServerThread &) = delete; AutoSocketServerThread &operator=(const AutoSocketServerThread &) = delete; + private: - Thread* old_thread_; + Thread *old_thread_; }; }// namespace sled