OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
mutex.hpp
1#pragma once
2#include <ossia/detail/config.hpp>
3
4#include <mutex>
5#if defined(OSSIA_SHARED_MUTEX_AVAILABLE)
6#include <shared_mutex>
7#endif
8
9namespace ossia
10{
11#if defined(OSSIA_SHARED_MUTEX_AVAILABLE)
12// https://stackoverflow.com/questions/69990339/why-is-stdmutex-so-much-worse-than-stdshared-mutex-in-visual-c
13#if !defined(_MSC_VER)
14using mutex_t = std::mutex;
15using lock_t = std::lock_guard<mutex_t>;
16#else
17using mutex_t = std::shared_mutex;
18using lock_t = std::lock_guard<mutex_t>;
19#endif
20
21using shared_mutex_t = std::shared_timed_mutex;
22using write_lock_t = std::lock_guard<shared_mutex_t>;
23using read_lock_t = std::shared_lock<shared_mutex_t>;
24#else
25using mutex_t = std::mutex;
26using shared_mutex_t = std::mutex;
27using lock_t = std::lock_guard<mutex_t>;
28using write_lock_t = std::lock_guard<mutex_t>;
29using read_lock_t = std::lock_guard<mutex_t>;
30#endif
31}
32
33// Enable thread safety attributes only with clang.
34// The attributes can be safely erased when compiling with other compilers.
35#if defined(__clang__) && (!defined(SWIG))
36#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
37#else
38#define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
39#endif
40
41#define TS_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
42
43#define TS_SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
44
45#define TS_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
46
47#define TS_PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
48
49#define TS_ACQUIRED_BEFORE(...) \
50 THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
51
52#define TS_ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
53
54#define TS_REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
55
56#define TS_REQUIRES_SHARED(...) \
57 THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))
58
59#define TS_ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
60
61#define TS_ACQUIRE_SHARED(...) \
62 THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))
63
64#define TS_RELEASE(...) THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
65
66#define TS_RELEASE_SHARED(...) \
67 THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))
68
69#define TS_RELEASE_GENERIC(...) \
70 THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(__VA_ARGS__))
71
72#define TS_TRY_ACQUIRE(...) \
73 THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
74
75#define TS_TRY_ACQUIRE_SHARED(...) \
76 THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))
77
78#define TS_EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
79
80#define TS_ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
81
82#define TS_ASSERT_SHARED_CAPABILITY(x) \
83 THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
84
85#define TS_RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
86
87#define TS_NO_THREAD_SAFETY_ANALYSIS \
88 THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
Definition git_info.h:7