ThreadingGtk.cpp 4.51 KB
Newer Older
bdash's avatar
bdash committed
1
/*
darin@apple.com's avatar
darin@apple.com committed
2
 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3
 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com)
bdash's avatar
bdash committed
4 5 6 7 8 9
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
10
 *     notice, this list of conditions and the following disclaimer.
bdash's avatar
bdash committed
11 12
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
13
 *     documentation and/or other materials provided with the distribution.
bdash's avatar
bdash committed
14 15
 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
16
 *     from this software without specific prior written permission.
bdash's avatar
bdash committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "Threading.h"

bdash's avatar
bdash committed
33
#include "HashMap.h"
bdash's avatar
bdash committed
34 35 36

#include <glib.h>

ap@webkit.org's avatar
ap@webkit.org committed
37
namespace WTF {
bdash's avatar
bdash committed
38

ap@webkit.org's avatar
ap@webkit.org committed
39 40
Mutex* atomicallyInitializedStaticMutex;

bdash's avatar
bdash committed
41 42
void initializeThreading()
{
ap@webkit.org's avatar
ap@webkit.org committed
43
    if (!g_thread_supported()) {
bdash's avatar
bdash committed
44
        g_thread_init(NULL);
ap@webkit.org's avatar
ap@webkit.org committed
45 46 47
        ASSERT(!atomicallyInitializedStaticMutex);
        atomicallyInitializedStaticMutex = new Mutex;
    }
bdash's avatar
bdash committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    ASSERT(g_thread_supported());
}

static Mutex& threadMapMutex()
{
    static Mutex mutex;
    return mutex;
}

static HashMap<ThreadIdentifier, GThread*>& threadMap()
{
    static HashMap<ThreadIdentifier, GThread*> map;
    return map;
}

static ThreadIdentifier establishIdentifierForThread(GThread*& thread)
{
    MutexLocker locker(threadMapMutex());

    static ThreadIdentifier identifierCount = 1;

    threadMap().add(identifierCount, thread);
70

bdash's avatar
bdash committed
71 72 73
    return identifierCount++;
}

74 75 76 77 78 79 80 81 82 83 84 85 86
static ThreadIdentifier identifierByGthreadHandle(GThread*& thread)
{
    MutexLocker locker(threadMapMutex());

    HashMap<ThreadIdentifier, GThread*>::iterator i = threadMap().begin();
    for (; i != threadMap().end(); ++i) {
        if (i->second == thread)
            return i->first;
    }

    return 0;
}

bdash's avatar
bdash committed
87 88 89
static GThread* threadForIdentifier(ThreadIdentifier id)
{
    MutexLocker locker(threadMapMutex());
90

bdash's avatar
bdash committed
91 92 93 94 95 96 97 98
    return threadMap().get(id);
}

static void clearThreadForIdentifier(ThreadIdentifier id)
{
    MutexLocker locker(threadMapMutex());

    ASSERT(threadMap().contains(id));
99

bdash's avatar
bdash committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    threadMap().remove(id);
}

ThreadIdentifier createThread(ThreadFunction entryPoint, void* data)
{
    GThread* thread;
    if (!(thread = g_thread_create(entryPoint, data, TRUE, 0))) {
        LOG_ERROR("Failed to create thread at entry point %p with data %p", entryPoint, data);
        return 0;
    }

    ThreadIdentifier threadID = establishIdentifierForThread(thread);
    return threadID;
}

int waitForThreadCompletion(ThreadIdentifier threadID, void** result)
{
    ASSERT(threadID);

    GThread* thread = threadForIdentifier(threadID);

    *result = g_thread_join(thread);

    clearThreadForIdentifier(threadID);
    return 0;
}

void detachThread(ThreadIdentifier)
{
}

131 132 133 134 135 136 137 138
ThreadIdentifier currentThread()
{
    GThread* currentThread = g_thread_self();
    if (ThreadIdentifier id = identifierByGthreadHandle(currentThread))
        return id;
    return establishIdentifierForThread(currentThread);
}

bdash's avatar
bdash committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152
Mutex::Mutex()
    : m_mutex(g_mutex_new())
{
}

Mutex::~Mutex()
{
    g_mutex_free(m_mutex);
}

void Mutex::lock()
{
    g_mutex_lock(m_mutex);
}
153

bdash's avatar
bdash committed
154 155 156 157 158 159 160 161 162 163 164 165
bool Mutex::tryLock()
{
    return g_mutex_trylock(m_mutex);
}

void Mutex::unlock()
{
    g_mutex_unlock(m_mutex);
}

ThreadCondition::ThreadCondition()
    : m_condition(g_cond_new())
166
{
bdash's avatar
bdash committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
}

ThreadCondition::~ThreadCondition()
{
    g_cond_free(m_condition);
}

void ThreadCondition::wait(Mutex& mutex)
{
    g_cond_wait(m_condition, mutex.impl());
}

void ThreadCondition::signal()
{
    g_cond_signal(m_condition);
}

void ThreadCondition::broadcast()
{
    g_cond_broadcast(m_condition);
}


bdash's avatar
bdash committed
190
}