This source file includes following definitions.
- m_pthreadHandle
- joinableState
- pthreadHandle
- didBecomeDetached
- didExit
- didJoin
- hasExited
- threadMapMutex
- initializeThreading
- lockAtomicallyInitializedStaticMutex
- unlockAtomicallyInitializedStaticMutex
- threadMap
- identifierByPthreadHandle
- establishIdentifierForPthreadHandle
- pthreadHandleForIdentifierWithLockAlreadyHeld
- wtfThreadEntryPoint
- createThreadInternal
- initializeCurrentThreadInternal
- waitForThreadCompletion
- detachThread
- threadDidExit
- yield
- currentThread
- lock
- tryLock
- unlock
- wait
- timedWait
- signal
- broadcast
#include "config.h"
#include "wtf/Threading.h"
#if USE(PTHREADS)
#include "wtf/DateMath.h"
#include "wtf/HashMap.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/StdLibExtras.h"
#include "wtf/ThreadFunctionInvocation.h"
#include "wtf/ThreadIdentifierDataPthreads.h"
#include "wtf/ThreadSpecific.h"
#include "wtf/ThreadingPrimitives.h"
#include "wtf/WTFThreadData.h"
#include "wtf/dtoa.h"
#include "wtf/dtoa/cached-powers.h"
#include <errno.h>
#if !COMPILER(MSVC)
#include <limits.h>
#include <sched.h>
#include <sys/time.h>
#endif
#if OS(MACOSX)
#include <objc/objc-auto.h>
#endif
namespace WTF {
class PthreadState {
WTF_MAKE_FAST_ALLOCATED;
public:
enum JoinableState {
Joinable,
Joined,
Detached
};
PthreadState(pthread_t handle)
: m_joinableState(Joinable)
, m_didExit(false)
, m_pthreadHandle(handle)
{
}
JoinableState joinableState() { return m_joinableState; }
pthread_t pthreadHandle() { return m_pthreadHandle; }
void didBecomeDetached() { m_joinableState = Detached; }
void didExit() { m_didExit = true; }
void didJoin() { m_joinableState = Joined; }
bool hasExited() { return m_didExit; }
private:
JoinableState m_joinableState;
bool m_didExit;
pthread_t m_pthreadHandle;
};
typedef HashMap<ThreadIdentifier, OwnPtr<PthreadState> > ThreadMap;
static Mutex* atomicallyInitializedStaticMutex;
void unsafeThreadWasDetached(ThreadIdentifier);
void threadDidExit(ThreadIdentifier);
void threadWasJoined(ThreadIdentifier);
static Mutex& threadMapMutex()
{
DEFINE_STATIC_LOCAL(Mutex, mutex, ());
return mutex;
}
void initializeThreading()
{
ASSERT(!atomicallyInitializedStaticMutex);
StringImpl::empty();
atomicallyInitializedStaticMutex = new Mutex;
threadMapMutex();
ThreadIdentifierData::initializeOnce();
wtfThreadData();
s_dtoaP5Mutex = new Mutex;
initializeDates();
}
void lockAtomicallyInitializedStaticMutex()
{
ASSERT(atomicallyInitializedStaticMutex);
atomicallyInitializedStaticMutex->lock();
}
void unlockAtomicallyInitializedStaticMutex()
{
atomicallyInitializedStaticMutex->unlock();
}
static ThreadMap& threadMap()
{
DEFINE_STATIC_LOCAL(ThreadMap, map, ());
return map;
}
static ThreadIdentifier identifierByPthreadHandle(const pthread_t& pthreadHandle)
{
MutexLocker locker(threadMapMutex());
ThreadMap::iterator i = threadMap().begin();
for (; i != threadMap().end(); ++i) {
if (pthread_equal(i->value->pthreadHandle(), pthreadHandle) && !i->value->hasExited())
return i->key;
}
return 0;
}
static ThreadIdentifier establishIdentifierForPthreadHandle(const pthread_t& pthreadHandle)
{
ASSERT(!identifierByPthreadHandle(pthreadHandle));
MutexLocker locker(threadMapMutex());
static ThreadIdentifier identifierCount = 1;
threadMap().add(identifierCount, adoptPtr(new PthreadState(pthreadHandle)));
return identifierCount++;
}
static pthread_t pthreadHandleForIdentifierWithLockAlreadyHeld(ThreadIdentifier id)
{
return threadMap().get(id)->pthreadHandle();
}
static void* wtfThreadEntryPoint(void* param)
{
OwnPtr<ThreadFunctionInvocation> invocation = adoptPtr(static_cast<ThreadFunctionInvocation*>(param));
invocation->function(invocation->data);
return 0;
}
ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, const char*)
{
OwnPtr<ThreadFunctionInvocation> invocation = adoptPtr(new ThreadFunctionInvocation(entryPoint, data));
pthread_t threadHandle;
if (pthread_create(&threadHandle, 0, wtfThreadEntryPoint, invocation.get())) {
WTF_LOG_ERROR("Failed to create pthread at entry point %p with data %p", wtfThreadEntryPoint, invocation.get());
return 0;
}
ThreadFunctionInvocation* ALLOW_UNUSED leakedInvocation = invocation.leakPtr();
return establishIdentifierForPthreadHandle(threadHandle);
}
void initializeCurrentThreadInternal(const char* threadName)
{
#if HAVE(PTHREAD_SETNAME_NP)
pthread_setname_np(threadName);
#endif
#if OS(MACOSX)
objc_registerThreadWithCollector();
#endif
ThreadIdentifier id = identifierByPthreadHandle(pthread_self());
ASSERT(id);
ThreadIdentifierData::initialize(id);
}
int waitForThreadCompletion(ThreadIdentifier threadID)
{
pthread_t pthreadHandle;
ASSERT(threadID);
{
MutexLocker locker(threadMapMutex());
pthreadHandle = pthreadHandleForIdentifierWithLockAlreadyHeld(threadID);
ASSERT(pthreadHandle);
}
int joinResult = pthread_join(pthreadHandle, 0);
if (joinResult == EDEADLK)
WTF_LOG_ERROR("ThreadIdentifier %u was found to be deadlocked trying to quit", threadID);
else if (joinResult)
WTF_LOG_ERROR("ThreadIdentifier %u was unable to be joined.\n", threadID);
MutexLocker locker(threadMapMutex());
PthreadState* state = threadMap().get(threadID);
ASSERT(state);
ASSERT(state->joinableState() == PthreadState::Joinable);
if (state->hasExited())
threadMap().remove(threadID);
else
state->didJoin();
return joinResult;
}
void detachThread(ThreadIdentifier threadID)
{
ASSERT(threadID);
MutexLocker locker(threadMapMutex());
pthread_t pthreadHandle = pthreadHandleForIdentifierWithLockAlreadyHeld(threadID);
ASSERT(pthreadHandle);
int detachResult = pthread_detach(pthreadHandle);
if (detachResult)
WTF_LOG_ERROR("ThreadIdentifier %u was unable to be detached\n", threadID);
PthreadState* state = threadMap().get(threadID);
ASSERT(state);
if (state->hasExited())
threadMap().remove(threadID);
else
threadMap().get(threadID)->didBecomeDetached();
}
void threadDidExit(ThreadIdentifier threadID)
{
MutexLocker locker(threadMapMutex());
PthreadState* state = threadMap().get(threadID);
ASSERT(state);
state->didExit();
if (state->joinableState() != PthreadState::Joinable)
threadMap().remove(threadID);
}
void yield()
{
sched_yield();
}
ThreadIdentifier currentThread()
{
ThreadIdentifier id = ThreadIdentifierData::identifier();
if (id)
return id;
id = establishIdentifierForPthreadHandle(pthread_self());
ThreadIdentifierData::initialize(id);
return id;
}
Mutex::Mutex()
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
int result = pthread_mutex_init(&m_mutex, &attr);
ASSERT_UNUSED(result, !result);
pthread_mutexattr_destroy(&attr);
}
Mutex::~Mutex()
{
int result = pthread_mutex_destroy(&m_mutex);
ASSERT_UNUSED(result, !result);
}
void Mutex::lock()
{
int result = pthread_mutex_lock(&m_mutex);
ASSERT_UNUSED(result, !result);
}
bool Mutex::tryLock()
{
int result = pthread_mutex_trylock(&m_mutex);
if (result == 0)
return true;
if (result == EBUSY)
return false;
ASSERT_NOT_REACHED();
return false;
}
void Mutex::unlock()
{
int result = pthread_mutex_unlock(&m_mutex);
ASSERT_UNUSED(result, !result);
}
ThreadCondition::ThreadCondition()
{
pthread_cond_init(&m_condition, NULL);
}
ThreadCondition::~ThreadCondition()
{
pthread_cond_destroy(&m_condition);
}
void ThreadCondition::wait(Mutex& mutex)
{
int result = pthread_cond_wait(&m_condition, &mutex.impl());
ASSERT_UNUSED(result, !result);
}
bool ThreadCondition::timedWait(Mutex& mutex, double absoluteTime)
{
if (absoluteTime < currentTime())
return false;
if (absoluteTime > INT_MAX) {
wait(mutex);
return true;
}
int timeSeconds = static_cast<int>(absoluteTime);
int timeNanoseconds = static_cast<int>((absoluteTime - timeSeconds) * 1E9);
timespec targetTime;
targetTime.tv_sec = timeSeconds;
targetTime.tv_nsec = timeNanoseconds;
return pthread_cond_timedwait(&m_condition, &mutex.impl(), &targetTime) == 0;
}
void ThreadCondition::signal()
{
int result = pthread_cond_signal(&m_condition);
ASSERT_UNUSED(result, !result);
}
void ThreadCondition::broadcast()
{
int result = pthread_cond_broadcast(&m_condition);
ASSERT_UNUSED(result, !result);
}
}
#endif