WTF shouldn't have both Thread and ThreadIdentifier
[WebKit.git] / Source / WTF / wtf / mac / MainThreadMac.mm
1 /*
2  * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer. 
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution. 
13  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28  
29 #import "config.h"
30 #import "MainThread.h"
31
32 #import <CoreFoundation/CoreFoundation.h>
33 #import <Foundation/NSThread.h>
34 #import <dispatch/dispatch.h>
35 #import <stdio.h>
36 #import <wtf/Assertions.h>
37 #import <wtf/HashSet.h>
38 #import <wtf/RetainPtr.h>
39 #import <wtf/SchedulePair.h>
40 #import <wtf/Threading.h>
41
42 #if USE(WEB_THREAD)
43 #include <wtf/ios/WebCoreThread.h>
44 #endif
45
46 @interface JSWTFMainThreadCaller : NSObject {
47 }
48 - (void)call;
49 @end
50
51 @implementation JSWTFMainThreadCaller
52
53 - (void)call
54 {
55     WTF::dispatchFunctionsFromMainThread();
56 }
57
58 @end // implementation JSWTFMainThreadCaller
59
60 namespace WTF {
61
62 static JSWTFMainThreadCaller* staticMainThreadCaller;
63 static bool isTimerPosted; // This is only accessed on the 'main' thread.
64 static bool mainThreadEstablishedAsPthreadMain;
65 static pthread_t mainThreadPthread;
66 static NSThread* mainThreadNSThread;
67
68 #if USE(WEB_THREAD)
69 static Thread* sApplicationUIThread;
70 static Thread* sWebThread;
71 #endif
72
73 void initializeMainThreadPlatform()
74 {
75     ASSERT(!staticMainThreadCaller);
76     staticMainThreadCaller = [[JSWTFMainThreadCaller alloc] init];
77
78 #if !USE(WEB_THREAD)
79     mainThreadEstablishedAsPthreadMain = false;
80     mainThreadPthread = pthread_self();
81     mainThreadNSThread = [[NSThread currentThread] retain];
82 #else
83     mainThreadEstablishedAsPthreadMain = true;
84     ASSERT(!mainThreadPthread);
85     ASSERT(!mainThreadNSThread);
86 #endif
87 }
88
89 #if !USE(WEB_THREAD)
90 void initializeMainThreadToProcessMainThreadPlatform()
91 {
92     if (!pthread_main_np())
93         NSLog(@"WebKit Threading Violation - initial use of WebKit from a secondary thread.");
94
95     ASSERT(!staticMainThreadCaller);
96     staticMainThreadCaller = [[JSWTFMainThreadCaller alloc] init];
97
98     mainThreadEstablishedAsPthreadMain = true;
99     mainThreadPthread = 0;
100     mainThreadNSThread = nil;
101 }
102 #endif // !USE(WEB_THREAD)
103
104 static void timerFired(CFRunLoopTimerRef timer, void*)
105 {
106     CFRelease(timer);
107     isTimerPosted = false;
108
109     @autoreleasepool {
110         WTF::dispatchFunctionsFromMainThread();
111     }
112 }
113
114 static void postTimer()
115 {
116     ASSERT(isMainThread());
117
118     if (isTimerPosted)
119         return;
120
121     isTimerPosted = true;
122     CFRunLoopAddTimer(CFRunLoopGetCurrent(), CFRunLoopTimerCreate(0, 0, 0, 0, 0, timerFired, 0), kCFRunLoopCommonModes);
123 }
124
125 void scheduleDispatchFunctionsOnMainThread()
126 {
127     ASSERT(staticMainThreadCaller);
128
129     if (isWebThread()) {
130         postTimer();
131         return;
132     }
133     
134     if (mainThreadEstablishedAsPthreadMain) {
135         ASSERT(!mainThreadNSThread);
136         [staticMainThreadCaller performSelectorOnMainThread:@selector(call) withObject:nil waitUntilDone:NO];
137         return;
138     }
139
140     ASSERT(mainThreadNSThread);
141     [staticMainThreadCaller performSelector:@selector(call) onThread:mainThreadNSThread withObject:nil waitUntilDone:NO];
142 }
143
144 void callOnWebThreadOrDispatchAsyncOnMainThread(void (^block)())
145 {
146 #if USE(WEB_THREAD)
147     if (WebCoreWebThreadIsEnabled && WebCoreWebThreadIsEnabled()) {
148         WebCoreWebThreadRun(block);
149         return;
150     }
151 #endif
152     dispatch_async(dispatch_get_main_queue(), block);
153 }
154
155 #if USE(WEB_THREAD)
156 static bool webThreadIsUninitializedOrLockedOrDisabled()
157 {
158     return !WebCoreWebThreadIsLockedOrDisabled || WebCoreWebThreadIsLockedOrDisabled();
159 }
160
161 bool isMainThread()
162 {
163     return (isWebThread() || pthread_main_np()) && webThreadIsUninitializedOrLockedOrDisabled();
164 }
165
166 bool isUIThread()
167 {
168     return pthread_main_np();
169 }
170
171 bool isWebThread()
172 {
173     return pthread_equal(pthread_self(), mainThreadPthread);
174 }
175
176 void initializeApplicationUIThread()
177 {
178     ASSERT(pthread_main_np());
179     sApplicationUIThread = &Thread::current();
180 }
181
182 void initializeWebThreadPlatform()
183 {
184     ASSERT(!pthread_main_np());
185
186     mainThreadEstablishedAsPthreadMain = false;
187     mainThreadPthread = pthread_self();
188     mainThreadNSThread = [[NSThread currentThread] retain];
189
190     sWebThread = &Thread::current();
191 }
192
193 bool canAccessThreadLocalDataForThread(Thread& thread)
194 {
195     Thread& currentThread = Thread::current();
196     if (&thread == &currentThread)
197         return true;
198
199     if (&thread == sWebThread || &thread == sApplicationUIThread)
200         return (&currentThread == sWebThread || &currentThread == sApplicationUIThread) && webThreadIsUninitializedOrLockedOrDisabled();
201
202     return false;
203 }
204 #else
205 bool isMainThread()
206 {
207     if (mainThreadEstablishedAsPthreadMain) {
208         ASSERT(!mainThreadPthread);
209         return pthread_main_np();
210     }
211
212     ASSERT(mainThreadPthread);
213     return pthread_equal(pthread_self(), mainThreadPthread);
214 }
215 #endif // USE(WEB_THREAD)
216
217 } // namespace WTF