1/*
2 * Copyright (C) 2007, 2008, 2010, 2014 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#ifndef Threading_h
31#define Threading_h
32
33// FIXME: Not sure why there are so many includes here.
34// Is this intended to be convenience so that others don't have to include the individual files?
35// Nothing in this header depends on Assertions, Atomics, Locker, Noncopyable, ThreadSafeRefCounted, or ThreadingPrimitives.
36
37#include <functional>
38#include <stdint.h>
39#include <wtf/Assertions.h>
40#include <wtf/Atomics.h>
41#include <wtf/Locker.h>
42#include <wtf/Noncopyable.h>
43#include <wtf/ThreadSafeRefCounted.h>
44#include <wtf/ThreadingPrimitives.h>
45
46namespace WTF {
47
48typedef uint32_t ThreadIdentifier;
49typedef void (*ThreadFunction)(void* argument);
50
51// This function must be called from the main thread. It is safe to call it repeatedly.
52// Darwin is an exception to this rule: it is OK to call it from any thread, the only
53// requirement is that the calls are not reentrant.
54WTF_EXPORT_PRIVATE void initializeThreading();
55
56// Returns 0 if thread creation failed.
57// The thread name must be a literal since on some platforms it's passed in to the thread.
58WTF_EXPORT_PRIVATE ThreadIdentifier createThread(const char* threadName, std::function<void()>);
59
60// Mark the current thread as requiring UI responsiveness.
61WTF_EXPORT_PRIVATE void setCurrentThreadIsUserInteractive();
62WTF_EXPORT_PRIVATE void setCurrentThreadIsUserInitiated();
63
64WTF_EXPORT_PRIVATE ThreadIdentifier currentThread();
65WTF_EXPORT_PRIVATE void changeThreadPriority(ThreadIdentifier, int);
66WTF_EXPORT_PRIVATE int waitForThreadCompletion(ThreadIdentifier);
67WTF_EXPORT_PRIVATE void detachThread(ThreadIdentifier);
68
69// Deprecated function-pointer-based thread creation.
70WTF_EXPORT_PRIVATE ThreadIdentifier createThread(ThreadFunction, void*, const char* threadName);
71
72// Internal platform-specific createThread implementation.
73ThreadIdentifier createThreadInternal(ThreadFunction, void*, const char* threadName);
74
75// Called in the thread during initialization.
76// Helpful for platforms where the thread name must be set from within the thread.
77void initializeCurrentThreadInternal(const char* threadName);
78
79} // namespace WTF
80
81using WTF::ThreadIdentifier;
82using WTF::createThread;
83using WTF::currentThread;
84using WTF::changeThreadPriority;
85using WTF::detachThread;
86using WTF::waitForThreadCompletion;
87
88#endif // Threading_h
89