1 | /* |
2 | * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2007 Justin Haygood ([email protected]) |
4 | * Copyright (C) 2016 Konstantin Tokavev <[email protected]> |
5 | * Copyright (C) 2016 Yusuke Suzuki <[email protected]> |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions |
9 | * are met: |
10 | * |
11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
16 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
17 | * its contributors may be used to endorse or promote products derived |
18 | * from this software without specific prior written permission. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
21 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
23 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
24 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
29 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
31 | |
32 | #include "config.h" |
33 | #include <pthread.h> |
34 | #if HAVE(PTHREAD_NP_H) |
35 | #include <pthread_np.h> |
36 | #endif |
37 | |
38 | #include <wtf/RunLoop.h> |
39 | #if USE(GLIB) |
40 | #include <wtf/glib/RunLoopSourcePriority.h> |
41 | #endif |
42 | |
43 | namespace WTF { |
44 | |
45 | #if !HAVE(PTHREAD_MAIN_NP) |
46 | static pthread_t mainThread; |
47 | #endif |
48 | |
49 | class MainThreadDispatcher { |
50 | public: |
51 | MainThreadDispatcher() |
52 | : m_timer(RunLoop::main(), this, &MainThreadDispatcher::fired) |
53 | { |
54 | #if USE(GLIB) |
55 | m_timer.setPriority(RunLoopSourcePriority::MainThreadDispatcherTimer); |
56 | #endif |
57 | } |
58 | |
59 | void schedule() |
60 | { |
61 | m_timer.startOneShot(0_s); |
62 | } |
63 | |
64 | private: |
65 | void fired() |
66 | { |
67 | dispatchFunctionsFromMainThread(); |
68 | } |
69 | |
70 | RunLoop::Timer<MainThreadDispatcher> m_timer; |
71 | }; |
72 | |
73 | void initializeMainThreadPlatform() |
74 | { |
75 | #if !HAVE(PTHREAD_MAIN_NP) |
76 | mainThread = pthread_self(); |
77 | #endif |
78 | } |
79 | |
80 | bool isMainThread() |
81 | { |
82 | #if HAVE(PTHREAD_MAIN_NP) |
83 | return pthread_main_np(); |
84 | #else |
85 | return pthread_equal(pthread_self(), mainThread); |
86 | #endif |
87 | } |
88 | |
89 | bool isMainThreadIfInitialized() |
90 | { |
91 | return isMainThread(); |
92 | } |
93 | |
94 | void scheduleDispatchFunctionsOnMainThread() |
95 | { |
96 | // Use a RunLoop::Timer instead of RunLoop::dispatch() to be able to use a different priority and |
97 | // avoid the double queue because dispatchOnMainThread also queues the functions. |
98 | static MainThreadDispatcher dispatcher; |
99 | dispatcher.schedule(); |
100 | } |
101 | |
102 | } // namespace WTF |
103 | |