1 | /*
|
---|
2 | * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
|
---|
3 | * Copyright (C) 2007 Justin Haygood ([email protected])
|
---|
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 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 |
|
---|
30 | #include "config.h"
|
---|
31 | #include "Threading.h"
|
---|
32 |
|
---|
33 | #include "HashMap.h"
|
---|
34 | #include "MainThread.h"
|
---|
35 | #include "MathExtras.h"
|
---|
36 |
|
---|
37 | #include <glib.h>
|
---|
38 |
|
---|
39 | namespace WTF {
|
---|
40 |
|
---|
41 | Mutex* atomicallyInitializedStaticMutex;
|
---|
42 |
|
---|
43 | static ThreadIdentifier mainThreadIdentifier;
|
---|
44 |
|
---|
45 | static Mutex& threadMapMutex()
|
---|
46 | {
|
---|
47 | static Mutex mutex;
|
---|
48 | return mutex;
|
---|
49 | }
|
---|
50 |
|
---|
51 | void initializeThreading()
|
---|
52 | {
|
---|
53 | if (!g_thread_supported())
|
---|
54 | g_thread_init(NULL);
|
---|
55 | ASSERT(g_thread_supported());
|
---|
56 |
|
---|
57 | if (!atomicallyInitializedStaticMutex) {
|
---|
58 | atomicallyInitializedStaticMutex = new Mutex;
|
---|
59 | threadMapMutex();
|
---|
60 | wtf_random_init();
|
---|
61 | mainThreadIdentifier = currentThread();
|
---|
62 | initializeMainThread();
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | static HashMap<ThreadIdentifier, GThread*>& threadMap()
|
---|
67 | {
|
---|
68 | static HashMap<ThreadIdentifier, GThread*> map;
|
---|
69 | return map;
|
---|
70 | }
|
---|
71 |
|
---|
72 | static ThreadIdentifier establishIdentifierForThread(GThread*& thread)
|
---|
73 | {
|
---|
74 | MutexLocker locker(threadMapMutex());
|
---|
75 |
|
---|
76 | static ThreadIdentifier identifierCount = 1;
|
---|
77 |
|
---|
78 | threadMap().add(identifierCount, thread);
|
---|
79 |
|
---|
80 | return identifierCount++;
|
---|
81 | }
|
---|
82 |
|
---|
83 | static ThreadIdentifier identifierByGthreadHandle(GThread*& thread)
|
---|
84 | {
|
---|
85 | MutexLocker locker(threadMapMutex());
|
---|
86 |
|
---|
87 | HashMap<ThreadIdentifier, GThread*>::iterator i = threadMap().begin();
|
---|
88 | for (; i != threadMap().end(); ++i) {
|
---|
89 | if (i->second == thread)
|
---|
90 | return i->first;
|
---|
91 | }
|
---|
92 |
|
---|
93 | return 0;
|
---|
94 | }
|
---|
95 |
|
---|
96 | static GThread* threadForIdentifier(ThreadIdentifier id)
|
---|
97 | {
|
---|
98 | MutexLocker locker(threadMapMutex());
|
---|
99 |
|
---|
100 | return threadMap().get(id);
|
---|
101 | }
|
---|
102 |
|
---|
103 | static void clearThreadForIdentifier(ThreadIdentifier id)
|
---|
104 | {
|
---|
105 | MutexLocker locker(threadMapMutex());
|
---|
106 |
|
---|
107 | ASSERT(threadMap().contains(id));
|
---|
108 |
|
---|
109 | threadMap().remove(id);
|
---|
110 | }
|
---|
111 |
|
---|
112 | ThreadIdentifier createThread(ThreadFunction entryPoint, void* data, const char*)
|
---|
113 | {
|
---|
114 | GThread* thread;
|
---|
115 | if (!(thread = g_thread_create(entryPoint, data, TRUE, 0))) {
|
---|
116 | LOG_ERROR("Failed to create thread at entry point %p with data %p", entryPoint, data);
|
---|
117 | return 0;
|
---|
118 | }
|
---|
119 |
|
---|
120 | ThreadIdentifier threadID = establishIdentifierForThread(thread);
|
---|
121 | return threadID;
|
---|
122 | }
|
---|
123 |
|
---|
124 | int waitForThreadCompletion(ThreadIdentifier threadID, void** result)
|
---|
125 | {
|
---|
126 | ASSERT(threadID);
|
---|
127 |
|
---|
128 | GThread* thread = threadForIdentifier(threadID);
|
---|
129 |
|
---|
130 | *result = g_thread_join(thread);
|
---|
131 |
|
---|
132 | clearThreadForIdentifier(threadID);
|
---|
133 | return 0;
|
---|
134 | }
|
---|
135 |
|
---|
136 | void detachThread(ThreadIdentifier)
|
---|
137 | {
|
---|
138 | }
|
---|
139 |
|
---|
140 | ThreadIdentifier currentThread()
|
---|
141 | {
|
---|
142 | GThread* currentThread = g_thread_self();
|
---|
143 | if (ThreadIdentifier id = identifierByGthreadHandle(currentThread))
|
---|
144 | return id;
|
---|
145 | return establishIdentifierForThread(currentThread);
|
---|
146 | }
|
---|
147 |
|
---|
148 | bool isMainThread()
|
---|
149 | {
|
---|
150 | return currentThread() == mainThreadIdentifier;
|
---|
151 | }
|
---|
152 |
|
---|
153 | Mutex::Mutex()
|
---|
154 | : m_mutex(g_mutex_new())
|
---|
155 | {
|
---|
156 | }
|
---|
157 |
|
---|
158 | Mutex::~Mutex()
|
---|
159 | {
|
---|
160 | }
|
---|
161 |
|
---|
162 | void Mutex::lock()
|
---|
163 | {
|
---|
164 | g_mutex_lock(m_mutex.get());
|
---|
165 | }
|
---|
166 |
|
---|
167 | bool Mutex::tryLock()
|
---|
168 | {
|
---|
169 | return g_mutex_trylock(m_mutex.get());
|
---|
170 | }
|
---|
171 |
|
---|
172 | void Mutex::unlock()
|
---|
173 | {
|
---|
174 | g_mutex_unlock(m_mutex.get());
|
---|
175 | }
|
---|
176 |
|
---|
177 | ThreadCondition::ThreadCondition()
|
---|
178 | : m_condition(g_cond_new())
|
---|
179 | {
|
---|
180 | }
|
---|
181 |
|
---|
182 | ThreadCondition::~ThreadCondition()
|
---|
183 | {
|
---|
184 | }
|
---|
185 |
|
---|
186 | void ThreadCondition::wait(Mutex& mutex)
|
---|
187 | {
|
---|
188 | g_cond_wait(m_condition.get(), mutex.impl().get());
|
---|
189 | }
|
---|
190 |
|
---|
191 | bool ThreadCondition::timedWait(Mutex& mutex, double interval)
|
---|
192 | {
|
---|
193 | if (interval < 0.0) {
|
---|
194 | wait(mutex);
|
---|
195 | return true;
|
---|
196 | }
|
---|
197 |
|
---|
198 | int intervalSeconds = static_cast<int>(interval);
|
---|
199 | int intervalMicroseconds = static_cast<int>((interval - intervalSeconds) * 1000000.0);
|
---|
200 |
|
---|
201 | GTimeVal targetTime;
|
---|
202 | g_get_current_time(&targetTime);
|
---|
203 |
|
---|
204 | targetTime.tv_sec += intervalSeconds;
|
---|
205 | targetTime.tv_usec += intervalMicroseconds;
|
---|
206 | if (targetTime.tv_usec > 1000000) {
|
---|
207 | targetTime.tv_usec -= 1000000;
|
---|
208 | targetTime.tv_sec++;
|
---|
209 | }
|
---|
210 |
|
---|
211 | return g_cond_timed_wait(m_condition.get(), mutex.impl().get(), &targetTime);
|
---|
212 | }
|
---|
213 |
|
---|
214 | void ThreadCondition::signal()
|
---|
215 | {
|
---|
216 | g_cond_signal(m_condition.get());
|
---|
217 | }
|
---|
218 |
|
---|
219 | void ThreadCondition::broadcast()
|
---|
220 | {
|
---|
221 | g_cond_broadcast(m_condition.get());
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 | }
|
---|