Commit | Line | Data |
---|---|---|
9dae56ea A |
1 | /* |
2 | * Copyright (C) 2007 Apple Inc. All rights reserved. | |
3 | * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | |
15 | * its contributors may be used to endorse or promote products derived | |
16 | * from this software without specific prior written permission. | |
17 | * | |
18 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
21 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | */ | |
29 | #include "config.h" | |
30 | #include "Threading.h" | |
31 | ||
32 | #include "StdLibExtras.h" | |
33 | ||
34 | #if USE(PTHREADS) | |
35 | ||
36 | #include "CurrentTime.h" | |
37 | #include "HashMap.h" | |
38 | #include "MainThread.h" | |
39 | #include "RandomNumberSeed.h" | |
40 | ||
41 | #include <errno.h> | |
42 | #include <limits.h> | |
43 | #include <sys/time.h> | |
44 | ||
45 | namespace WTF { | |
46 | ||
47 | typedef HashMap<ThreadIdentifier, pthread_t> ThreadMap; | |
48 | ||
49 | static Mutex* atomicallyInitializedStaticMutex; | |
50 | ||
51 | static ThreadIdentifier mainThreadIdentifier; // The thread that was the first to call initializeThreading(), which must be the main thread. | |
52 | ||
53 | static Mutex& threadMapMutex() | |
54 | { | |
55 | DEFINE_STATIC_LOCAL(Mutex, mutex, ()); | |
56 | return mutex; | |
57 | } | |
58 | ||
59 | void initializeThreading() | |
60 | { | |
61 | if (!atomicallyInitializedStaticMutex) { | |
62 | atomicallyInitializedStaticMutex = new Mutex; | |
63 | threadMapMutex(); | |
64 | initializeRandomNumberGenerator(); | |
65 | mainThreadIdentifier = currentThread(); | |
66 | initializeMainNSThread(); | |
67 | initializeMainThread(); | |
68 | } | |
69 | } | |
70 | ||
71 | void lockAtomicallyInitializedStaticMutex() | |
72 | { | |
73 | ASSERT(atomicallyInitializedStaticMutex); | |
74 | atomicallyInitializedStaticMutex->lock(); | |
75 | } | |
76 | ||
77 | void unlockAtomicallyInitializedStaticMutex() | |
78 | { | |
79 | atomicallyInitializedStaticMutex->unlock(); | |
80 | } | |
81 | ||
82 | static ThreadMap& threadMap() | |
83 | { | |
84 | DEFINE_STATIC_LOCAL(ThreadMap, map, ()); | |
85 | return map; | |
86 | } | |
87 | ||
88 | static ThreadIdentifier identifierByPthreadHandle(const pthread_t& pthreadHandle) | |
89 | { | |
90 | MutexLocker locker(threadMapMutex()); | |
91 | ||
92 | ThreadMap::iterator i = threadMap().begin(); | |
93 | for (; i != threadMap().end(); ++i) { | |
94 | if (pthread_equal(i->second, pthreadHandle)) | |
95 | return i->first; | |
96 | } | |
97 | ||
98 | return 0; | |
99 | } | |
100 | ||
101 | static ThreadIdentifier establishIdentifierForPthreadHandle(pthread_t& pthreadHandle) | |
102 | { | |
103 | ASSERT(!identifierByPthreadHandle(pthreadHandle)); | |
104 | ||
105 | MutexLocker locker(threadMapMutex()); | |
106 | ||
107 | static ThreadIdentifier identifierCount = 1; | |
108 | ||
109 | threadMap().add(identifierCount, pthreadHandle); | |
110 | ||
111 | return identifierCount++; | |
112 | } | |
113 | ||
114 | static pthread_t pthreadHandleForIdentifier(ThreadIdentifier id) | |
115 | { | |
116 | MutexLocker locker(threadMapMutex()); | |
117 | ||
118 | return threadMap().get(id); | |
119 | } | |
120 | ||
121 | static void clearPthreadHandleForIdentifier(ThreadIdentifier id) | |
122 | { | |
123 | MutexLocker locker(threadMapMutex()); | |
124 | ||
125 | ASSERT(threadMap().contains(id)); | |
126 | ||
127 | threadMap().remove(id); | |
128 | } | |
129 | ||
130 | ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, const char*) | |
131 | { | |
132 | pthread_t threadHandle; | |
133 | if (pthread_create(&threadHandle, NULL, entryPoint, data)) { | |
134 | LOG_ERROR("Failed to create pthread at entry point %p with data %p", entryPoint, data); | |
135 | return 0; | |
136 | } | |
137 | ||
138 | return establishIdentifierForPthreadHandle(threadHandle); | |
139 | } | |
140 | ||
141 | int waitForThreadCompletion(ThreadIdentifier threadID, void** result) | |
142 | { | |
143 | ASSERT(threadID); | |
144 | ||
145 | pthread_t pthreadHandle = pthreadHandleForIdentifier(threadID); | |
146 | ||
147 | int joinResult = pthread_join(pthreadHandle, result); | |
148 | if (joinResult == EDEADLK) | |
149 | LOG_ERROR("ThreadIdentifier %u was found to be deadlocked trying to quit", threadID); | |
150 | ||
151 | clearPthreadHandleForIdentifier(threadID); | |
152 | return joinResult; | |
153 | } | |
154 | ||
155 | void detachThread(ThreadIdentifier threadID) | |
156 | { | |
157 | ASSERT(threadID); | |
158 | ||
159 | pthread_t pthreadHandle = pthreadHandleForIdentifier(threadID); | |
160 | ||
161 | pthread_detach(pthreadHandle); | |
162 | ||
163 | clearPthreadHandleForIdentifier(threadID); | |
164 | } | |
165 | ||
166 | ThreadIdentifier currentThread() | |
167 | { | |
168 | pthread_t currentThread = pthread_self(); | |
169 | if (ThreadIdentifier id = identifierByPthreadHandle(currentThread)) | |
170 | return id; | |
171 | return establishIdentifierForPthreadHandle(currentThread); | |
172 | } | |
173 | ||
174 | bool isMainThread() | |
175 | { | |
176 | return currentThread() == mainThreadIdentifier; | |
177 | } | |
178 | ||
179 | Mutex::Mutex() | |
180 | { | |
181 | pthread_mutex_init(&m_mutex, NULL); | |
182 | } | |
183 | ||
184 | Mutex::~Mutex() | |
185 | { | |
186 | pthread_mutex_destroy(&m_mutex); | |
187 | } | |
188 | ||
189 | void Mutex::lock() | |
190 | { | |
191 | if (pthread_mutex_lock(&m_mutex) != 0) | |
192 | ASSERT(false); | |
193 | } | |
194 | ||
195 | bool Mutex::tryLock() | |
196 | { | |
197 | int result = pthread_mutex_trylock(&m_mutex); | |
198 | ||
199 | if (result == 0) | |
200 | return true; | |
201 | else if (result == EBUSY) | |
202 | return false; | |
203 | ||
204 | ASSERT(false); | |
205 | return false; | |
206 | } | |
207 | ||
208 | void Mutex::unlock() | |
209 | { | |
210 | if (pthread_mutex_unlock(&m_mutex) != 0) | |
211 | ASSERT(false); | |
212 | } | |
213 | ||
214 | ThreadCondition::ThreadCondition() | |
215 | { | |
216 | pthread_cond_init(&m_condition, NULL); | |
217 | } | |
218 | ||
219 | ThreadCondition::~ThreadCondition() | |
220 | { | |
221 | pthread_cond_destroy(&m_condition); | |
222 | } | |
223 | ||
224 | void ThreadCondition::wait(Mutex& mutex) | |
225 | { | |
226 | if (pthread_cond_wait(&m_condition, &mutex.impl()) != 0) | |
227 | ASSERT(false); | |
228 | } | |
229 | ||
230 | bool ThreadCondition::timedWait(Mutex& mutex, double absoluteTime) | |
231 | { | |
232 | if (absoluteTime < currentTime()) | |
233 | return false; | |
234 | ||
235 | if (absoluteTime > INT_MAX) { | |
236 | wait(mutex); | |
237 | return true; | |
238 | } | |
239 | ||
240 | int timeSeconds = static_cast<int>(absoluteTime); | |
241 | int timeNanoseconds = static_cast<int>((absoluteTime - timeSeconds) * 1E9); | |
242 | ||
243 | timespec targetTime; | |
244 | targetTime.tv_sec = timeSeconds; | |
245 | targetTime.tv_nsec = timeNanoseconds; | |
246 | ||
247 | return pthread_cond_timedwait(&m_condition, &mutex.impl(), &targetTime) == 0; | |
248 | } | |
249 | ||
250 | void ThreadCondition::signal() | |
251 | { | |
252 | if (pthread_cond_signal(&m_condition) != 0) | |
253 | ASSERT(false); | |
254 | } | |
255 | ||
256 | void ThreadCondition::broadcast() | |
257 | { | |
258 | if (pthread_cond_broadcast(&m_condition) != 0) | |
259 | ASSERT(false); | |
260 | } | |
261 | ||
262 | } // namespace WTF | |
263 | ||
264 | #endif // USE(PTHREADS) |