source: webkit/trunk/JavaScriptCore/wtf/ThreadingGtk.cpp@ 36846

Last change on this file since 36846 was 36846, checked in by [email protected], 17 years ago

2008-09-24 Jan Michael Alonzo <[email protected]>

Reviewed by Alp Toker.

https://bugs.webkit.org/show_bug.cgi?id=20992
Build fails on GTK+ Mac OS

  • wtf/ThreadingGtk.cpp: Remove platform ifdef as suggested by Richard Hult. (WTF::initializeThreading):
  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
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
39namespace WTF {
40
41Mutex* atomicallyInitializedStaticMutex;
42
43static ThreadIdentifier mainThreadIdentifier;
44
45static Mutex& threadMapMutex()
46{
47 static Mutex mutex;
48 return mutex;
49}
50
51void 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
66static HashMap<ThreadIdentifier, GThread*>& threadMap()
67{
68 static HashMap<ThreadIdentifier, GThread*> map;
69 return map;
70}
71
72static 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
83static 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
96static GThread* threadForIdentifier(ThreadIdentifier id)
97{
98 MutexLocker locker(threadMapMutex());
99
100 return threadMap().get(id);
101}
102
103static void clearThreadForIdentifier(ThreadIdentifier id)
104{
105 MutexLocker locker(threadMapMutex());
106
107 ASSERT(threadMap().contains(id));
108
109 threadMap().remove(id);
110}
111
112ThreadIdentifier 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
124int 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
136void detachThread(ThreadIdentifier)
137{
138}
139
140ThreadIdentifier currentThread()
141{
142 GThread* currentThread = g_thread_self();
143 if (ThreadIdentifier id = identifierByGthreadHandle(currentThread))
144 return id;
145 return establishIdentifierForThread(currentThread);
146}
147
148bool isMainThread()
149{
150 return currentThread() == mainThreadIdentifier;
151}
152
153Mutex::Mutex()
154 : m_mutex(g_mutex_new())
155{
156}
157
158Mutex::~Mutex()
159{
160 g_mutex_free(m_mutex);
161}
162
163void Mutex::lock()
164{
165 g_mutex_lock(m_mutex);
166}
167
168bool Mutex::tryLock()
169{
170 return g_mutex_trylock(m_mutex);
171}
172
173void Mutex::unlock()
174{
175 g_mutex_unlock(m_mutex);
176}
177
178ThreadCondition::ThreadCondition()
179 : m_condition(g_cond_new())
180{
181}
182
183ThreadCondition::~ThreadCondition()
184{
185 g_cond_free(m_condition);
186}
187
188void ThreadCondition::wait(Mutex& mutex)
189{
190 g_cond_wait(m_condition, mutex.impl());
191}
192
193bool ThreadCondition::timedWait(Mutex& mutex, double interval)
194{
195 if (interval < 0.0) {
196 wait(mutex);
197 return true;
198 }
199
200 int intervalSeconds = static_cast<int>(interval);
201 int intervalMicroseconds = static_cast<int>((interval - intervalSeconds) * 1000000.0);
202
203 GTimeVal targetTime;
204 g_get_current_time(&targetTime);
205
206 targetTime.tv_sec += intervalSeconds;
207 targetTime.tv_usec += intervalMicroseconds;
208 if (targetTime.tv_usec > 1000000) {
209 targetTime.tv_usec -= 1000000;
210 targetTime.tv_sec++;
211 }
212
213 return g_cond_timed_wait(m_condition, mutex.impl(), &targetTime);
214}
215
216void ThreadCondition::signal()
217{
218 g_cond_signal(m_condition);
219}
220
221void ThreadCondition::broadcast()
222{
223 g_cond_broadcast(m_condition);
224}
225
226
227}
Note: See TracBrowser for help on using the repository browser.