summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libutils/include/utils/Mutex.h93
-rw-r--r--libutils/tests/Android.bp2
-rw-r--r--libutils/tests/Mutex_test.cpp32
3 files changed, 106 insertions, 21 deletions
diff --git a/libutils/include/utils/Mutex.h b/libutils/include/utils/Mutex.h
index d106185f0..af6076cf3 100644
--- a/libutils/include/utils/Mutex.h
+++ b/libutils/include/utils/Mutex.h
@@ -28,6 +28,53 @@
28#include <utils/Errors.h> 28#include <utils/Errors.h>
29#include <utils/Timers.h> 29#include <utils/Timers.h>
30 30
31// Enable thread safety attributes only with clang.
32// The attributes can be safely erased when compiling with other compilers.
33#if defined(__clang__) && (!defined(SWIG))
34#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
35#else
36#define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
37#endif
38
39#define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
40
41#define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
42
43#define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
44
45#define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
46
47#define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
48
49#define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
50
51#define REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
52
53#define REQUIRES_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))
54
55#define ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
56
57#define ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))
58
59#define RELEASE(...) THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
60
61#define RELEASE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))
62
63#define TRY_ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
64
65#define TRY_ACQUIRE_SHARED(...) \
66 THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))
67
68#define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
69
70#define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
71
72#define ASSERT_SHARED_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
73
74#define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
75
76#define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
77
31// --------------------------------------------------------------------------- 78// ---------------------------------------------------------------------------
32namespace android { 79namespace android {
33// --------------------------------------------------------------------------- 80// ---------------------------------------------------------------------------
@@ -44,24 +91,24 @@ class Condition;
44 * The mutex must be unlocked by the thread that locked it. They are not 91 * The mutex must be unlocked by the thread that locked it. They are not
45 * recursive, i.e. the same thread can't lock it multiple times. 92 * recursive, i.e. the same thread can't lock it multiple times.
46 */ 93 */
47class Mutex { 94class CAPABILITY("mutex") Mutex {
48public: 95 public:
49 enum { 96 enum {
50 PRIVATE = 0, 97 PRIVATE = 0,
51 SHARED = 1 98 SHARED = 1
52 }; 99 };
53 100
54 Mutex(); 101 Mutex();
55 explicit Mutex(const char* name); 102 explicit Mutex(const char* name);
56 explicit Mutex(int type, const char* name = NULL); 103 explicit Mutex(int type, const char* name = NULL);
57 ~Mutex(); 104 ~Mutex();
58 105
59 // lock or unlock the mutex 106 // lock or unlock the mutex
60 status_t lock(); 107 status_t lock() ACQUIRE();
61 void unlock(); 108 void unlock() RELEASE();
62 109
63 // lock if possible; returns 0 on success, error otherwise 110 // lock if possible; returns 0 on success, error otherwise
64 status_t tryLock(); 111 status_t tryLock() TRY_ACQUIRE(true);
65 112
66#if defined(__ANDROID__) 113#if defined(__ANDROID__)
67 // Lock the mutex, but don't wait longer than timeoutNs (relative time). 114 // Lock the mutex, but don't wait longer than timeoutNs (relative time).
@@ -75,32 +122,36 @@ public:
75 // which is subject to NTP adjustments, and includes time during suspend, 122 // which is subject to NTP adjustments, and includes time during suspend,
76 // so a timeout may occur even though no processes could run. 123 // so a timeout may occur even though no processes could run.
77 // Not holding a partial wakelock may lead to a system suspend. 124 // Not holding a partial wakelock may lead to a system suspend.
78 status_t timedLock(nsecs_t timeoutNs); 125 status_t timedLock(nsecs_t timeoutNs) TRY_ACQUIRE(true);
79#endif 126#endif
80 127
81 // Manages the mutex automatically. It'll be locked when Autolock is 128 // Manages the mutex automatically. It'll be locked when Autolock is
82 // constructed and released when Autolock goes out of scope. 129 // constructed and released when Autolock goes out of scope.
83 class Autolock { 130 class SCOPED_CAPABILITY Autolock {
84 public: 131 public:
85 inline explicit Autolock(Mutex& mutex) : mLock(mutex) { mLock.lock(); } 132 inline explicit Autolock(Mutex& mutex) ACQUIRE(mutex) : mLock(mutex) { mLock.lock(); }
86 inline explicit Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); } 133 inline explicit Autolock(Mutex* mutex) ACQUIRE(mutex) : mLock(*mutex) { mLock.lock(); }
87 inline ~Autolock() { mLock.unlock(); } 134 inline ~Autolock() RELEASE() { mLock.unlock(); }
88 private: 135
136 private:
89 Mutex& mLock; 137 Mutex& mLock;
138 // Cannot be copied or moved - declarations only
139 Autolock(const Autolock&);
140 Autolock& operator=(const Autolock&);
90 }; 141 };
91 142
92private: 143 private:
93 friend class Condition; 144 friend class Condition;
94 145
95 // A mutex cannot be copied 146 // A mutex cannot be copied
96 Mutex(const Mutex&); 147 Mutex(const Mutex&);
97 Mutex& operator = (const Mutex&); 148 Mutex& operator=(const Mutex&);
98 149
99#if !defined(_WIN32) 150#if !defined(_WIN32)
100 pthread_mutex_t mMutex; 151 pthread_mutex_t mMutex;
101#else 152#else
102 void _init(); 153 void _init();
103 void* mState; 154 void* mState;
104#endif 155#endif
105}; 156};
106 157
diff --git a/libutils/tests/Android.bp b/libutils/tests/Android.bp
index 7b62c2436..08691756a 100644
--- a/libutils/tests/Android.bp
+++ b/libutils/tests/Android.bp
@@ -23,6 +23,7 @@ cc_test {
23 srcs: [ 23 srcs: [
24 "BitSet_test.cpp", 24 "BitSet_test.cpp",
25 "LruCache_test.cpp", 25 "LruCache_test.cpp",
26 "Mutex_test.cpp",
26 "Singleton_test.cpp", 27 "Singleton_test.cpp",
27 "String8_test.cpp", 28 "String8_test.cpp",
28 "StrongPointer_test.cpp", 29 "StrongPointer_test.cpp",
@@ -71,6 +72,7 @@ cc_test {
71 "-Wall", 72 "-Wall",
72 "-Wextra", 73 "-Wextra",
73 "-Werror", 74 "-Werror",
75 "-Wthread-safety",
74 ], 76 ],
75} 77}
76 78
diff --git a/libutils/tests/Mutex_test.cpp b/libutils/tests/Mutex_test.cpp
new file mode 100644
index 000000000..8a1805f51
--- /dev/null
+++ b/libutils/tests/Mutex_test.cpp
@@ -0,0 +1,32 @@
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <utils/Mutex.h>
18
19#include <gtest/gtest.h>
20
21static android::Mutex mLock;
22static int i GUARDED_BY(mLock);
23
24void modifyLockedVariable() REQUIRES(mLock) {
25 i = 1;
26}
27
28TEST(Mutex, compile) {
29 android::Mutex::Autolock _l(mLock);
30 i = 0;
31 modifyLockedVariable();
32} \ No newline at end of file