Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: src/platform/mutex.cc

Issue 23625003: Cleanup Mutex and related classes. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: REBASE Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/platform/mutex.h ('k') | src/platform/time.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "platform/mutex.h"
29
30 #include <cerrno>
31
32 namespace v8 {
33 namespace internal {
34
35 #if V8_OS_POSIX
36
37 static V8_INLINE(void InitializeNativeHandle(pthread_mutex_t* mutex)) {
38 int result;
39 #if defined(DEBUG)
40 // Use an error checking mutex in debug mode.
41 pthread_mutexattr_t attr;
42 result = pthread_mutexattr_init(&attr);
43 ASSERT_EQ(0, result);
44 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
45 ASSERT_EQ(0, result);
46 result = pthread_mutex_init(mutex, &attr);
47 ASSERT_EQ(0, result);
48 result = pthread_mutexattr_destroy(&attr);
49 #else
50 // Use a fast mutex (default attributes).
51 result = pthread_mutex_init(mutex, NULL);
52 #endif // defined(DEBUG)
53 ASSERT_EQ(0, result);
54 USE(result);
55 }
56
57
58 static V8_INLINE(void InitializeRecursiveNativeHandle(pthread_mutex_t* mutex)) {
59 pthread_mutexattr_t attr;
60 int result = pthread_mutexattr_init(&attr);
61 ASSERT_EQ(0, result);
62 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
63 ASSERT_EQ(0, result);
64 result = pthread_mutex_init(mutex, &attr);
65 ASSERT_EQ(0, result);
66 result = pthread_mutexattr_destroy(&attr);
67 ASSERT_EQ(0, result);
68 USE(result);
69 }
70
71
72 static V8_INLINE(void DestroyNativeHandle(pthread_mutex_t* mutex)) {
73 int result = pthread_mutex_destroy(mutex);
74 ASSERT_EQ(0, result);
75 USE(result);
76 }
77
78
79 static V8_INLINE(void LockNativeHandle(pthread_mutex_t* mutex)) {
80 int result = pthread_mutex_lock(mutex);
81 ASSERT_EQ(0, result);
82 USE(result);
83 }
84
85
86 static V8_INLINE(void UnlockNativeHandle(pthread_mutex_t* mutex)) {
87 int result = pthread_mutex_unlock(mutex);
88 ASSERT_EQ(0, result);
89 USE(result);
90 }
91
92
93 static V8_INLINE(bool TryLockNativeHandle(pthread_mutex_t* mutex)) {
94 int result = pthread_mutex_trylock(mutex);
95 if (result == EBUSY) {
96 return false;
97 }
98 ASSERT_EQ(0, result);
99 return true;
100 }
101
102 #elif V8_OS_WIN
103
104 static V8_INLINE(void InitializeNativeHandle(CRITICAL_SECTION* cs)) {
105 InitializeCriticalSection(cs);
106 }
107
108
109 static V8_INLINE(void InitializeRecursiveNativeHandle(CRITICAL_SECTION* cs)) {
110 InitializeCriticalSection(cs);
111 }
112
113
114 static V8_INLINE(void DestroyNativeHandle(CRITICAL_SECTION* cs)) {
115 DeleteCriticalSection(cs);
116 }
117
118
119 static V8_INLINE(void LockNativeHandle(CRITICAL_SECTION* cs)) {
120 EnterCriticalSection(cs);
121 }
122
123
124 static V8_INLINE(void UnlockNativeHandle(CRITICAL_SECTION* cs)) {
125 LeaveCriticalSection(cs);
126 }
127
128
129 static V8_INLINE(bool TryLockNativeHandle(CRITICAL_SECTION* cs)) {
130 return TryEnterCriticalSection(cs);
131 }
132
133 #endif // V8_OS_POSIX
134
135
136 Mutex::Mutex() {
137 InitializeNativeHandle(&native_handle_);
138 #ifdef DEBUG
139 level_ = 0;
140 #endif
141 }
142
143
144 Mutex::~Mutex() {
145 DestroyNativeHandle(&native_handle_);
146 ASSERT_EQ(0, level_);
147 }
148
149
150 void Mutex::Lock() {
151 LockNativeHandle(&native_handle_);
152 #ifdef DEBUG
153 ASSERT_EQ(0, level_);
154 level_++;
155 #endif
156 }
157
158
159 void Mutex::Unlock() {
160 #ifdef DEBUG
161 ASSERT_EQ(1, level_);
162 level_--;
163 #endif
164 UnlockNativeHandle(&native_handle_);
165 }
166
167
168 bool Mutex::TryLock() {
169 if (!TryLockNativeHandle(&native_handle_)) {
170 return false;
171 }
172 #ifdef DEBUG
173 ASSERT_EQ(0, level_);
174 level_++;
175 #endif
176 return true;
177 }
178
179
180 RecursiveMutex::RecursiveMutex() {
181 InitializeRecursiveNativeHandle(&native_handle_);
182 #ifdef DEBUG
183 level_ = 0;
184 #endif
185 }
186
187
188 RecursiveMutex::~RecursiveMutex() {
189 DestroyNativeHandle(&native_handle_);
190 ASSERT_EQ(0, level_);
191 }
192
193
194 void RecursiveMutex::Lock() {
195 LockNativeHandle(&native_handle_);
196 #ifdef DEBUG
197 ASSERT_LE(0, level_);
198 level_++;
199 #endif
200 }
201
202
203 void RecursiveMutex::Unlock() {
204 #ifdef DEBUG
205 ASSERT_LT(0, level_);
206 level_--;
207 #endif
208 UnlockNativeHandle(&native_handle_);
209 }
210
211
212 bool RecursiveMutex::TryLock() {
213 if (!TryLockNativeHandle(&native_handle_)) {
214 return false;
215 }
216 #ifdef DEBUG
217 ASSERT_LE(0, level_);
218 level_++;
219 #endif
220 return true;
221 }
222
223 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform/mutex.h ('k') | src/platform/time.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698