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