]> git.saurik.com Git - apple/javascriptcore.git/blame - wtf/Threading.h
JavaScriptCore-903.tar.gz
[apple/javascriptcore.git] / wtf / Threading.h
CommitLineData
9dae56ea 1/*
4e4e5a6f 2 * Copyright (C) 2007, 2008, 2010 Apple Inc. All rights reserved.
9dae56ea
A
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 Computer, 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 * Note: The implementations of InterlockedIncrement and InterlockedDecrement are based
31 * on atomic_increment and atomic_exchange_and_add from the Boost C++ Library. The license
32 * is virtually identical to the Apple license above but is included here for completeness.
33 *
34 * Boost Software License - Version 1.0 - August 17th, 2003
35 *
36 * Permission is hereby granted, free of charge, to any person or organization
37 * obtaining a copy of the software and accompanying documentation covered by
38 * this license (the "Software") to use, reproduce, display, distribute,
39 * execute, and transmit the Software, and to prepare derivative works of the
40 * Software, and to permit third-parties to whom the Software is furnished to
41 * do so, all subject to the following:
42 *
43 * The copyright notices in the Software and this entire statement, including
44 * the above license grant, this restriction and the following disclaimer,
45 * must be included in all copies of the Software, in whole or in part, and
46 * all derivative works of the Software, unless such copies or derivative
47 * works are solely in the form of machine-executable object code generated by
48 * a source language processor.
49 *
50 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
51 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
52 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
53 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
54 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
55 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
56 * DEALINGS IN THE SOFTWARE.
57 */
58
59#ifndef Threading_h
60#define Threading_h
61
f9bf01c6
A
62#include "Platform.h"
63
4e4e5a6f 64#include <stdint.h>
9dae56ea 65#include <wtf/Assertions.h>
4e4e5a6f 66#include <wtf/Atomics.h>
9dae56ea 67#include <wtf/Locker.h>
4e4e5a6f 68#include <wtf/MainThread.h>
9dae56ea 69#include <wtf/Noncopyable.h>
14957cd0 70#include <wtf/ThreadSafeRefCounted.h>
4e4e5a6f 71#include <wtf/ThreadingPrimitives.h>
9dae56ea
A
72
73// For portability, we do not use thread-safe statics natively supported by some compilers (e.g. gcc).
74#define AtomicallyInitializedStatic(T, name) \
75 WTF::lockAtomicallyInitializedStaticMutex(); \
76 static T name; \
77 WTF::unlockAtomicallyInitializedStaticMutex();
78
79namespace WTF {
80
81typedef uint32_t ThreadIdentifier;
82typedef void* (*ThreadFunction)(void* argument);
83
4e4e5a6f
A
84// This function must be called from the main thread. It is safe to call it repeatedly.
85// Darwin is an exception to this rule: it is OK to call it from any thread, the only
86// requirement is that the calls are not reentrant.
87void initializeThreading();
88
ba379fdc
A
89// Returns 0 if thread creation failed.
90// The thread name must be a literal since on some platforms it's passed in to the thread.
9dae56ea 91ThreadIdentifier createThread(ThreadFunction, void*, const char* threadName);
ba379fdc
A
92
93// Internal platform-specific createThread implementation.
9dae56ea
A
94ThreadIdentifier createThreadInternal(ThreadFunction, void*, const char* threadName);
95
ba379fdc
A
96// Called in the thread during initialization.
97// Helpful for platforms where the thread name must be set from within the thread.
f9bf01c6 98void initializeCurrentThreadInternal(const char* threadName);
ba379fdc 99
9dae56ea 100ThreadIdentifier currentThread();
9dae56ea
A
101int waitForThreadCompletion(ThreadIdentifier, void**);
102void detachThread(ThreadIdentifier);
103
14957cd0 104void yield();
9dae56ea 105
9dae56ea
A
106void lockAtomicallyInitializedStaticMutex();
107void unlockAtomicallyInitializedStaticMutex();
108
109} // namespace WTF
110
9dae56ea 111using WTF::ThreadIdentifier;
9dae56ea
A
112using WTF::createThread;
113using WTF::currentThread;
9dae56ea
A
114using WTF::detachThread;
115using WTF::waitForThreadCompletion;
14957cd0 116using WTF::yield;
9dae56ea
A
117
118#endif // Threading_h