initial import
[vuplus_webkit] / Source / JavaScriptCore / wtf / gtk / ThreadingGtk.cpp
1 /*
2  * Copyright (C) 2007, 2008 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 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 #if !USE(PTHREADS)
34
35 #include "CurrentTime.h"
36 #include "DateMath.h"
37 #include "dtoa.h"
38 #include "HashMap.h"
39 #include "MainThread.h"
40 #include "RandomNumberSeed.h"
41 #include <wtf/StdLibExtras.h>
42 #include <wtf/WTFThreadData.h>
43
44 #include <glib.h>
45 #include <limits.h>
46
47 namespace WTF {
48
49 typedef HashMap<ThreadIdentifier, GThread*> ThreadMap;
50
51 static Mutex* atomicallyInitializedStaticMutex;
52
53 static Mutex& threadMapMutex()
54 {
55     DEFINE_STATIC_LOCAL(Mutex, mutex, ());
56     return mutex;
57 }
58
59 void initializeThreading()
60 {
61     if (!g_thread_supported())
62         g_thread_init(NULL);
63     ASSERT(g_thread_supported());
64
65     if (!atomicallyInitializedStaticMutex) {
66         // StringImpl::empty() does not construct its static string in a threadsafe fashion,
67         // so ensure it has been initialized from here.
68         StringImpl::empty();
69         atomicallyInitializedStaticMutex = new Mutex;
70         threadMapMutex();
71         initializeRandomNumberGenerator();
72         wtfThreadData();
73 #if ENABLE(WTF_MULTIPLE_THREADS)
74         s_dtoaP5Mutex = new Mutex;
75         initializeDates();
76 #endif
77     }
78 }
79
80 void lockAtomicallyInitializedStaticMutex()
81 {
82     ASSERT(atomicallyInitializedStaticMutex);
83     atomicallyInitializedStaticMutex->lock();
84 }
85
86 void unlockAtomicallyInitializedStaticMutex()
87 {
88     atomicallyInitializedStaticMutex->unlock();
89 }
90
91 static ThreadMap& threadMap()
92 {
93     DEFINE_STATIC_LOCAL(ThreadMap, map, ());
94     return map;
95 }
96
97 static ThreadIdentifier identifierByGthreadHandle(GThread*& thread)
98 {
99     MutexLocker locker(threadMapMutex());
100
101     ThreadMap::iterator i = threadMap().begin();
102     for (; i != threadMap().end(); ++i) {
103         if (i->second == thread)
104             return i->first;
105     }
106
107     return 0;
108 }
109
110 static ThreadIdentifier establishIdentifierForThread(GThread*& thread)
111 {
112     ASSERT(!identifierByGthreadHandle(thread));
113
114     MutexLocker locker(threadMapMutex());
115
116     static ThreadIdentifier identifierCount = 1;
117
118     threadMap().add(identifierCount, thread);
119
120     return identifierCount++;
121 }
122
123 static GThread* threadForIdentifier(ThreadIdentifier id)
124 {
125     MutexLocker locker(threadMapMutex());
126
127     return threadMap().get(id);
128 }
129
130 static void clearThreadForIdentifier(ThreadIdentifier id)
131 {
132     MutexLocker locker(threadMapMutex());
133
134     ASSERT(threadMap().contains(id));
135
136     threadMap().remove(id);
137 }
138
139 ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, const char*)
140 {
141     GThread* thread;
142     if (!(thread = g_thread_create(entryPoint, data, TRUE, 0))) {
143         LOG_ERROR("Failed to create thread at entry point %p with data %p", entryPoint, data);
144         return 0;
145     }
146
147     ThreadIdentifier threadID = establishIdentifierForThread(thread);
148     return threadID;
149 }
150
151 void initializeCurrentThreadInternal(const char*)
152 {
153 }
154
155 int waitForThreadCompletion(ThreadIdentifier threadID, void** result)
156 {
157     ASSERT(threadID);
158
159     GThread* thread = threadForIdentifier(threadID);
160
161     void* joinResult = g_thread_join(thread);
162     if (result)
163         *result = joinResult;
164
165     clearThreadForIdentifier(threadID);
166     return 0;
167 }
168
169 void detachThread(ThreadIdentifier)
170 {
171 }
172
173 ThreadIdentifier currentThread()
174 {
175     GThread* currentThread = g_thread_self();
176     if (ThreadIdentifier id = identifierByGthreadHandle(currentThread))
177         return id;
178     return establishIdentifierForThread(currentThread);
179 }
180
181 void yield()
182 {
183     g_thread_yield();
184 }
185
186 Mutex::Mutex()
187     : m_mutex(g_mutex_new())
188 {
189 }
190
191 Mutex::~Mutex()
192 {
193 }
194
195 void Mutex::lock()
196 {
197     g_mutex_lock(m_mutex.get());
198 }
199
200 bool Mutex::tryLock()
201 {
202     return g_mutex_trylock(m_mutex.get());
203 }
204
205 void Mutex::unlock()
206 {
207     g_mutex_unlock(m_mutex.get());
208 }
209
210 ThreadCondition::ThreadCondition()
211     : m_condition(g_cond_new())
212 {
213 }
214
215 ThreadCondition::~ThreadCondition()
216 {
217 }
218
219 void ThreadCondition::wait(Mutex& mutex)
220 {
221     g_cond_wait(m_condition.get(), mutex.impl().get());
222 }
223
224 bool ThreadCondition::timedWait(Mutex& mutex, double absoluteTime)
225 {
226     // Time is in the past - return right away.
227     if (absoluteTime < currentTime())
228         return false;
229     
230     // Time is too far in the future for g_cond_timed_wait - wait forever.
231     if (absoluteTime > INT_MAX) {
232         wait(mutex);
233         return true;
234     }
235
236     int timeSeconds = static_cast<int>(absoluteTime);
237     int timeMicroseconds = static_cast<int>((absoluteTime - timeSeconds) * 1000000.0);
238     
239     GTimeVal targetTime;
240     targetTime.tv_sec = timeSeconds;
241     targetTime.tv_usec = timeMicroseconds;
242
243     return g_cond_timed_wait(m_condition.get(), mutex.impl().get(), &targetTime);
244 }
245
246 void ThreadCondition::signal()
247 {
248     g_cond_signal(m_condition.get());
249 }
250
251 void ThreadCondition::broadcast()
252 {
253     g_cond_broadcast(m_condition.get());
254 }
255
256
257 }
258
259 #endif // !USE(PTHREADS)