2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
31 #include "Threading.h"
35 #include "CurrentTime.h"
39 #include "MainThread.h"
40 #include "RandomNumberSeed.h"
41 #include <wtf/StdLibExtras.h>
42 #include <wtf/WTFThreadData.h>
49 typedef HashMap<ThreadIdentifier, GThread*> ThreadMap;
51 static Mutex* atomicallyInitializedStaticMutex;
53 static Mutex& threadMapMutex()
55 DEFINE_STATIC_LOCAL(Mutex, mutex, ());
59 void initializeThreading()
61 if (!g_thread_supported())
63 ASSERT(g_thread_supported());
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.
69 atomicallyInitializedStaticMutex = new Mutex;
71 initializeRandomNumberGenerator();
73 #if ENABLE(WTF_MULTIPLE_THREADS)
74 s_dtoaP5Mutex = new Mutex;
80 void lockAtomicallyInitializedStaticMutex()
82 ASSERT(atomicallyInitializedStaticMutex);
83 atomicallyInitializedStaticMutex->lock();
86 void unlockAtomicallyInitializedStaticMutex()
88 atomicallyInitializedStaticMutex->unlock();
91 static ThreadMap& threadMap()
93 DEFINE_STATIC_LOCAL(ThreadMap, map, ());
97 static ThreadIdentifier identifierByGthreadHandle(GThread*& thread)
99 MutexLocker locker(threadMapMutex());
101 ThreadMap::iterator i = threadMap().begin();
102 for (; i != threadMap().end(); ++i) {
103 if (i->second == thread)
110 static ThreadIdentifier establishIdentifierForThread(GThread*& thread)
112 ASSERT(!identifierByGthreadHandle(thread));
114 MutexLocker locker(threadMapMutex());
116 static ThreadIdentifier identifierCount = 1;
118 threadMap().add(identifierCount, thread);
120 return identifierCount++;
123 static GThread* threadForIdentifier(ThreadIdentifier id)
125 MutexLocker locker(threadMapMutex());
127 return threadMap().get(id);
130 static void clearThreadForIdentifier(ThreadIdentifier id)
132 MutexLocker locker(threadMapMutex());
134 ASSERT(threadMap().contains(id));
136 threadMap().remove(id);
139 ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, const char*)
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);
147 ThreadIdentifier threadID = establishIdentifierForThread(thread);
151 void initializeCurrentThreadInternal(const char*)
155 int waitForThreadCompletion(ThreadIdentifier threadID, void** result)
159 GThread* thread = threadForIdentifier(threadID);
161 void* joinResult = g_thread_join(thread);
163 *result = joinResult;
165 clearThreadForIdentifier(threadID);
169 void detachThread(ThreadIdentifier)
173 ThreadIdentifier currentThread()
175 GThread* currentThread = g_thread_self();
176 if (ThreadIdentifier id = identifierByGthreadHandle(currentThread))
178 return establishIdentifierForThread(currentThread);
187 : m_mutex(g_mutex_new())
197 g_mutex_lock(m_mutex.get());
200 bool Mutex::tryLock()
202 return g_mutex_trylock(m_mutex.get());
207 g_mutex_unlock(m_mutex.get());
210 ThreadCondition::ThreadCondition()
211 : m_condition(g_cond_new())
215 ThreadCondition::~ThreadCondition()
219 void ThreadCondition::wait(Mutex& mutex)
221 g_cond_wait(m_condition.get(), mutex.impl().get());
224 bool ThreadCondition::timedWait(Mutex& mutex, double absoluteTime)
226 // Time is in the past - return right away.
227 if (absoluteTime < currentTime())
230 // Time is too far in the future for g_cond_timed_wait - wait forever.
231 if (absoluteTime > INT_MAX) {
236 int timeSeconds = static_cast<int>(absoluteTime);
237 int timeMicroseconds = static_cast<int>((absoluteTime - timeSeconds) * 1000000.0);
240 targetTime.tv_sec = timeSeconds;
241 targetTime.tv_usec = timeMicroseconds;
243 return g_cond_timed_wait(m_condition.get(), mutex.impl().get(), &targetTime);
246 void ThreadCondition::signal()
248 g_cond_signal(m_condition.get());
251 void ThreadCondition::broadcast()
253 g_cond_broadcast(m_condition.get());
259 #endif // !USE(PTHREADS)