1 /*
2 * Copyright (C) 2007, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com)
4 * Copyright (C) 2011 Research In Motion Limited. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
16 * its contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "third_party/blink/renderer/platform/wtf/threading.h"
32
33 #include "build/build_config.h"
34
35 #if defined(OS_POSIX) || defined(OS_FUCHSIA)
36
37 #include <errno.h>
38 #include <limits.h>
39 #include <sched.h>
40 #include <sys/time.h>
41 #include "base/threading/scoped_blocking_call.h"
42 #include "third_party/blink/renderer/platform/wtf/date_math.h"
43 #include "third_party/blink/renderer/platform/wtf/hash_map.h"
44 #include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
45 #include "third_party/blink/renderer/platform/wtf/thread_specific.h"
46 #include "third_party/blink/renderer/platform/wtf/threading.h"
47 #include "third_party/blink/renderer/platform/wtf/threading_primitives.h"
48
49 #if defined(OS_MACOSX)
50 #include <objc/objc-auto.h>
51 #endif
52
53 #if defined(OS_LINUX) || defined(OS_BSD)
54 #include <sys/syscall.h>
55 #endif
56
57 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_BSD)
58 #include <unistd.h>
59 #endif
60
61 #if defined(OS_BSD)
62 #include <sys/signal.h>
63 #include <pthread_np.h>
64 #endif
65
66 namespace WTF {
67
MutexBase(bool recursive)68 MutexBase::MutexBase(bool recursive) {
69 pthread_mutexattr_t attr;
70 pthread_mutexattr_init(&attr);
71 pthread_mutexattr_settype(
72 &attr, recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL);
73
74 int result = pthread_mutex_init(&mutex_.internal_mutex_, &attr);
75 DCHECK_EQ(result, 0);
76 #if DCHECK_IS_ON()
77 mutex_.recursion_count_ = 0;
78 #endif
79
80 pthread_mutexattr_destroy(&attr);
81 }
82
~MutexBase()83 MutexBase::~MutexBase() {
84 int result = pthread_mutex_destroy(&mutex_.internal_mutex_);
85 DCHECK_EQ(result, 0);
86 }
87
lock()88 void MutexBase::lock() {
89 int result = pthread_mutex_lock(&mutex_.internal_mutex_);
90 DCHECK_EQ(result, 0);
91 #if DCHECK_IS_ON()
92 DCHECK(!mutex_.recursion_count_)
93 << "WTF does not support recursive mutex acquisition!";
94 ++mutex_.recursion_count_;
95 #endif
96 }
97
unlock()98 void MutexBase::unlock() {
99 #if DCHECK_IS_ON()
100 DCHECK(mutex_.recursion_count_);
101 --mutex_.recursion_count_;
102 #endif
103 int result = pthread_mutex_unlock(&mutex_.internal_mutex_);
104 DCHECK_EQ(result, 0);
105 }
106
107 // There is a separate tryLock implementation for the Mutex and the
108 // RecursiveMutex since on Windows we need to manually check if tryLock should
109 // succeed or not for the non-recursive mutex. On Linux the two implementations
110 // are equal except we can assert the recursion count is always zero for the
111 // non-recursive mutex.
TryLock()112 bool Mutex::TryLock() {
113 int result = pthread_mutex_trylock(&mutex_.internal_mutex_);
114 if (result == 0) {
115 #if DCHECK_IS_ON()
116 // The Mutex class is not recursive, so the recursionCount should be
117 // zero after getting the lock.
118 DCHECK(!mutex_.recursion_count_)
119 << "WTF does not support recursive mutex acquisition!";
120 ++mutex_.recursion_count_;
121 #endif
122 return true;
123 }
124 if (result == EBUSY)
125 return false;
126
127 NOTREACHED();
128 return false;
129 }
130
TryLock()131 bool RecursiveMutex::TryLock() {
132 int result = pthread_mutex_trylock(&mutex_.internal_mutex_);
133 if (result == 0) {
134 #if DCHECK_IS_ON()
135 DCHECK(!mutex_.recursion_count_)
136 << "WTF does not support recursive mutex acquisition!";
137 ++mutex_.recursion_count_;
138 #endif
139 return true;
140 }
141 if (result == EBUSY)
142 return false;
143
144 NOTREACHED();
145 return false;
146 }
147
ThreadCondition(Mutex & mutex)148 ThreadCondition::ThreadCondition(Mutex& mutex) : mutex_(mutex.Impl()) {
149 pthread_cond_init(&condition_, nullptr);
150 }
151
~ThreadCondition()152 ThreadCondition::~ThreadCondition() {
153 pthread_cond_destroy(&condition_);
154 }
155
Wait()156 void ThreadCondition::Wait() {
157 base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
158 base::BlockingType::MAY_BLOCK);
159 #if DCHECK_IS_ON()
160 --mutex_.recursion_count_;
161 #endif
162 int result = pthread_cond_wait(&condition_, &mutex_.internal_mutex_);
163 DCHECK_EQ(result, 0);
164 #if DCHECK_IS_ON()
165 ++mutex_.recursion_count_;
166 #endif
167 }
168
Signal()169 void ThreadCondition::Signal() {
170 int result = pthread_cond_signal(&condition_);
171 DCHECK_EQ(result, 0);
172 }
173
Broadcast()174 void ThreadCondition::Broadcast() {
175 int result = pthread_cond_broadcast(&condition_);
176 DCHECK_EQ(result, 0);
177 }
178
179 } // namespace WTF
180
181 #endif // defined(OS_POSIX) || defined(OS_FUCHSIA)
182