Changeset 225529 in webkit


Ignore:
Timestamp:
Dec 5, 2017 12:26:37 PM (6 years ago)
Author:
Yusuke Suzuki
Message:

Modernize WebThread code for further refactoring
https://bugs.webkit.org/show_bug.cgi?id=180344

Reviewed by Alex Christensen.

When touching Thread and its initialization component, we need to touch
web thread initialization code too. However, since web thread code is old,
it's style is not following to WebKit coding style guidelines.

This patch first modernizes WebThread code for further refactoring.

  1. Type* instead of Type *.
  2. Use std::call_once instead of pthread_once.
  3. Use static_cast instead of C-cast.
  4. Use nullptr instead of NULL.
  5. Use ret or !ret instead of ret != 0 or ret == 0.
  6. Do not use #pragma unused.
  7. if-else with one statement should not have braces.

In this patch, we do not change pthread_condition_t to WTF::Condition and
pthread_mutex_t to WTF::Lock since web thread code requires extra care about
threading.

No behavior change.

  • platform/ios/wak/WebCoreThread.mm:

(-[NSObject releaseOnMainThread]):
(SendMessage):
(HandleDelegateSource):
(SendDelegateMessage):
(WebThreadAdoptAndRelease):
(lockWebCoreReleaseLock):
(unlockWebCoreReleaseLock):
(WebCoreObjCDeallocOnWebThreadImpl):
(WebCoreObjCDeallocWithWebThreadLockImpl):
(HandleWebThreadReleaseSource):
(WebThreadCallDelegate):
(WebThreadPostNotification):
(WebThreadCallDelegateAsync):
(WebThreadMakeNSInvocation):
(MainRunLoopAutoUnlock):
(_WebThreadAutoLock):
(WebRunLoopLock):
(WebRunLoopUnlock):
(MainRunLoopUnlockGuard):
(CurrentThreadContext):
(RunWebThread):
(StartWebThread):
(WebTimedConditionLock):
(_WebTryThreadLock):
(WebThreadUnlockFromAnyThread):
(WebThreadUnlockGuardForMail):
(_WebThreadUnlock):
(WebThreadIsLocked):
(WebThreadLockPopModal):
(WebThreadCurrentContext):
(WebThreadEnable):
(FreeThreadContext): Deleted.
(InitThreadContextKey): Deleted.

Location:
trunk/Source/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r225528 r225529  
     12017-12-05  Yusuke Suzuki  <utatane.tea@gmail.com>
     2
     3        Modernize WebThread code for further refactoring
     4        https://bugs.webkit.org/show_bug.cgi?id=180344
     5
     6        Reviewed by Alex Christensen.
     7
     8        When touching Thread and its initialization component, we need to touch
     9        web thread initialization code too. However, since web thread code is old,
     10        it's style is not following to WebKit coding style guidelines.
     11
     12        This patch first modernizes WebThread code for further refactoring.
     13
     14        1. `Type*` instead of `Type *`.
     15        2. Use std::call_once instead of pthread_once.
     16        3. Use static_cast instead of C-cast.
     17        4. Use nullptr instead of NULL.
     18        5. Use `ret` or `!ret` instead of `ret != 0` or `ret == 0`.
     19        6. Do not use #pragma unused.
     20        7. if-else with one statement should not have braces.
     21
     22        In this patch, we do not change pthread_condition_t to WTF::Condition and
     23        pthread_mutex_t to WTF::Lock since web thread code requires extra care about
     24        threading.
     25
     26        No behavior change.
     27
     28        * platform/ios/wak/WebCoreThread.mm:
     29        (-[NSObject releaseOnMainThread]):
     30        (SendMessage):
     31        (HandleDelegateSource):
     32        (SendDelegateMessage):
     33        (WebThreadAdoptAndRelease):
     34        (lockWebCoreReleaseLock):
     35        (unlockWebCoreReleaseLock):
     36        (WebCoreObjCDeallocOnWebThreadImpl):
     37        (WebCoreObjCDeallocWithWebThreadLockImpl):
     38        (HandleWebThreadReleaseSource):
     39        (WebThreadCallDelegate):
     40        (WebThreadPostNotification):
     41        (WebThreadCallDelegateAsync):
     42        (WebThreadMakeNSInvocation):
     43        (MainRunLoopAutoUnlock):
     44        (_WebThreadAutoLock):
     45        (WebRunLoopLock):
     46        (WebRunLoopUnlock):
     47        (MainRunLoopUnlockGuard):
     48        (CurrentThreadContext):
     49        (RunWebThread):
     50        (StartWebThread):
     51        (WebTimedConditionLock):
     52        (_WebTryThreadLock):
     53        (WebThreadUnlockFromAnyThread):
     54        (WebThreadUnlockGuardForMail):
     55        (_WebThreadUnlock):
     56        (WebThreadIsLocked):
     57        (WebThreadLockPopModal):
     58        (WebThreadCurrentContext):
     59        (WebThreadEnable):
     60        (FreeThreadContext): Deleted.
     61        (InitThreadContextKey): Deleted.
     62
    1632017-12-05  Alex Christensen  <achristensen@webkit.org>
    264
  • trunk/Source/WebCore/platform/ios/wak/WebCoreThread.mm

    r225470 r225529  
    7474
    7575- (void)releaseOnMainThread {
    76     if ([NSThread isMainThread]) {
     76    if ([NSThread isMainThread])
    7777        [self release];
    78     } else {
     78    else
    7979        [self performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
    80     }
    8180}
    8281
    8382@end
    8483
    85 typedef void *NSAutoreleasePoolMark;
     84using NSAutoreleasePoolMark = void*;
    8685#ifdef __cplusplus
    8786extern "C" {
     
    9392#endif
    9493
    95 static int WebTimedConditionLock (pthread_cond_t *condition, pthread_mutex_t *lock, CFAbsoluteTime interval);
     94static int WebTimedConditionLock(pthread_cond_t* condition, pthread_mutex_t* lock, CFAbsoluteTime interval);
    9695
    9796static pthread_mutex_t webLock;
     
    119118static pthread_mutex_t delegateLock = PTHREAD_MUTEX_INITIALIZER;
    120119static pthread_cond_t delegateCondition = PTHREAD_COND_INITIALIZER;
    121 static NSInvocation *delegateInvocation;
    122 static CFRunLoopSourceRef delegateSource = NULL;
     120static NSInvocation* delegateInvocation;
     121static CFRunLoopSourceRef delegateSource = nullptr;
    123122static BOOL delegateHandled;
    124123#if LOG_MAIN_THREAD_LOCKING
     
    131130static StaticCondition startupCondition;
    132131
    133 static WebThreadContext *webThreadContext;
     132static WebThreadContext* webThreadContext;
    134133static pthread_key_t threadContextKey;
    135134static unsigned mainThreadLockCount;
     
    144143static void WebCoreObjCDeallocWithWebThreadLockImpl(id self, SEL _cmd);
    145144
    146 static NSMutableArray *sAsyncDelegates = nil;
    147 
    148 static inline void SendMessage(NSInvocation *invocation)
     145static NSMutableArray* sAsyncDelegates = nil;
     146
     147static inline void SendMessage(NSInvocation* invocation)
    149148{
    150149    [invocation invoke];
     
    152151}
    153152
    154 static void HandleDelegateSource(void *info)
    155 {
    156     UNUSED_PARAM(info);
     153static void HandleDelegateSource(void*)
     154{
    157155    ASSERT(!WebThreadIsCurrent());
    158156
     
    164162
    165163    int result = pthread_mutex_lock(&delegateLock);
    166     ASSERT_WITH_MESSAGE(result == 0, "delegate lock failed with code:%d", result);
     164    ASSERT_WITH_MESSAGE(!result, "delegate lock failed with code:%d", result);
    167165
    168166#if LOG_MESSAGES
     
    171169        [delegateInvocation getArgument:&argument0 atIndex:0];
    172170        NSLog(@"notification receive: %@", argument0);
    173     } else {
     171    } else
    174172        NSLog(@"delegate receive: %@", NSStringFromSelector([delegateInvocation selector]));
    175     }
    176173#endif
    177174
     
    182179
    183180    result = pthread_mutex_unlock(&delegateLock);
    184     ASSERT_WITH_MESSAGE(result == 0, "delegate unlock failed with code:%d", result);
     181    ASSERT_WITH_MESSAGE(!result, "delegate unlock failed with code:%d", result);
    185182
    186183#if LOG_MAIN_THREAD_LOCKING
     
    189186}
    190187
    191 static void SendDelegateMessage(NSInvocation *invocation)
    192 {
    193     if (WebThreadIsCurrent()) {
    194         ASSERT(delegateSource);
    195         int result = pthread_mutex_lock(&delegateLock);
    196         ASSERT_WITH_MESSAGE(result == 0, "delegate lock failed with code:%d", result);
    197 
    198         delegateInvocation = invocation;
    199         delegateHandled = NO;
     188static void SendDelegateMessage(NSInvocation* invocation)
     189{
     190    if (!WebThreadIsCurrent()) {
     191        SendMessage(invocation);
     192        return;
     193    }
     194
     195    ASSERT(delegateSource);
     196    int result = pthread_mutex_lock(&delegateLock);
     197    ASSERT_WITH_MESSAGE(!result, "delegate lock failed with code:%d", result);
     198
     199    delegateInvocation = invocation;
     200    delegateHandled = NO;
    200201
    201202#if LOG_MESSAGES
    202         if ([[delegateInvocation target] isKindOfClass:[NSNotificationCenter class]]) {
    203             id argument0;
    204             [delegateInvocation getArgument:&argument0 atIndex:0];
    205             NSLog(@"notification send: %@", argument0);
    206         } else {
    207             NSLog(@"delegate send: %@", NSStringFromSelector([delegateInvocation selector]));
     203    if ([[delegateInvocation target] isKindOfClass:[NSNotificationCenter class]]) {
     204        id argument0;
     205        [delegateInvocation getArgument:&argument0 atIndex:0];
     206        NSLog(@"notification send: %@", argument0);
     207    } else
     208        NSLog(@"delegate send: %@", NSStringFromSelector([delegateInvocation selector]));
     209#endif
     210
     211    {
     212        // Code block created to scope JSC::JSLock::DropAllLocks outside of WebThreadLock()
     213        JSC::JSLock::DropAllLocks dropAllLocks(WebCore::commonVM());
     214        _WebThreadUnlock();
     215
     216        CFRunLoopSourceSignal(delegateSource);
     217        CFRunLoopWakeUp(CFRunLoopGetMain());
     218
     219        while (!delegateHandled) {
     220            if (WebTimedConditionLock(&delegateCondition, &delegateLock, DelegateWaitInterval)) {
     221                id delegateInformation;
     222                if ([[delegateInvocation target] isKindOfClass:[NSNotificationCenter class]])
     223                    [delegateInvocation getArgument:&delegateInformation atIndex:0];
     224                else
     225                    delegateInformation = NSStringFromSelector([delegateInvocation selector]);
     226   
     227                CFStringRef mode = CFRunLoopCopyCurrentMode(CFRunLoopGetMain());
     228                NSLog(@"%s: delegate (%@) failed to return after waiting %d seconds. main run loop mode: %@", __PRETTY_FUNCTION__, delegateInformation, DelegateWaitInterval, mode);
     229                if (mode)
     230                    CFRelease(mode);
     231            }
    208232        }
    209 #endif
    210 
    211         {
    212             // Code block created to scope JSC::JSLock::DropAllLocks outside of WebThreadLock()
    213             JSC::JSLock::DropAllLocks dropAllLocks(WebCore::commonVM());
    214             _WebThreadUnlock();
    215 
    216             CFRunLoopSourceSignal(delegateSource);
    217             CFRunLoopWakeUp(CFRunLoopGetMain());
    218 
    219             while (!delegateHandled) {
    220                 if (WebTimedConditionLock(&delegateCondition, &delegateLock, DelegateWaitInterval) != 0) {
    221                     id delegateInformation;
    222                     if ([[delegateInvocation target] isKindOfClass:[NSNotificationCenter class]])
    223                         [delegateInvocation getArgument:&delegateInformation atIndex:0];
    224                     else
    225                         delegateInformation = NSStringFromSelector([delegateInvocation selector]);
    226        
    227                     CFStringRef mode = CFRunLoopCopyCurrentMode(CFRunLoopGetMain());
    228                     NSLog(@"%s: delegate (%@) failed to return after waiting %d seconds. main run loop mode: %@", __PRETTY_FUNCTION__, delegateInformation, DelegateWaitInterval, mode);
    229                     if (mode)
    230                         CFRelease(mode);
    231                 }
    232             }
    233             result = pthread_mutex_unlock(&delegateLock);
    234 
    235             ASSERT_WITH_MESSAGE(result == 0, "delegate unlock failed with code:%d", result);
    236             _WebTryThreadLock(false);
    237         }
    238     } else {
    239         SendMessage(invocation);
     233        result = pthread_mutex_unlock(&delegateLock);
     234
     235        ASSERT_WITH_MESSAGE(!result, "delegate unlock failed with code:%d", result);
     236        _WebTryThreadLock(false);
    240237    }
    241238}
     
    286283
    287284    int result = pthread_mutex_lock(&WebThreadReleaseLock);
    288     ASSERT_WITH_MESSAGE(result == 0, "Release lock failed with code:%d", result);
     285    ASSERT_WITH_MESSAGE(!result, "Release lock failed with code:%d", result);
    289286
    290287    if (WebThreadReleaseObjArray == nil)
    291         WebThreadReleaseObjArray = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, NULL);
     288        WebThreadReleaseObjArray = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, nullptr);
    292289    CFArrayAppendValue(WebThreadReleaseObjArray, obj);
    293290    CFRunLoopSourceSignal(WebThreadReleaseSource);
     
    295292
    296293    result = pthread_mutex_unlock(&WebThreadReleaseLock);   
    297     ASSERT_WITH_MESSAGE(result == 0, "Release unlock failed with code:%d", result);
     294    ASSERT_WITH_MESSAGE(!result, "Release unlock failed with code:%d", result);
    298295}
    299296
     
    301298{
    302299    int lockcode = pthread_mutex_lock(&WebCoreReleaseLock);
    303 #pragma unused (lockcode)
    304     ASSERT_WITH_MESSAGE(lockcode == 0, "WebCoreReleaseLock lock failed with code:%d", lockcode);
     300    ASSERT_WITH_MESSAGE_UNUSED(lockcode, !lockcode, "WebCoreReleaseLock lock failed with code:%d", lockcode);
    305301}
    306302
     
    308304{
    309305    int lockcode = pthread_mutex_unlock(&WebCoreReleaseLock);
    310 #pragma unused (lockcode)
    311     ASSERT_WITH_MESSAGE(lockcode == 0, "WebCoreReleaseLock unlock failed with code:%d", lockcode);
     306    ASSERT_WITH_MESSAGE_UNUSED(lockcode, !lockcode, "WebCoreReleaseLock unlock failed with code:%d", lockcode);
    312307}
    313308
     
    364359}
    365360
    366 void WebCoreObjCDeallocOnWebThreadImpl(id self, SEL _cmd)
    367 {
    368     UNUSED_PARAM(_cmd);
    369     if (!WebThreadIsEnabled())
     361void WebCoreObjCDeallocOnWebThreadImpl(id self, SEL)
     362{
     363    if (!WebThreadIsEnabled()) {
    370364        [self _webcore_releaseOnWebThread];
    371     else {
    372         lockWebCoreReleaseLock();
    373         if ([self retainCount] == 1) {
    374             // This is the only reference retaining the object, so we can
    375             // safely release the WebCoreReleaseLock now.
    376             unlockWebCoreReleaseLock();
    377             if (WebThreadIsCurrent())
    378                 [self _webcore_releaseOnWebThread];
    379             else
    380                 WebThreadAdoptAndRelease(self);
    381         } else {
    382             // This is not the only reference retaining the object, so another
    383             // thread could also call release - hold the lock whilst calling
    384             // release to avoid a race condition.
     365        return;
     366    }
     367
     368    lockWebCoreReleaseLock();
     369    if ([self retainCount] == 1) {
     370        // This is the only reference retaining the object, so we can
     371        // safely release the WebCoreReleaseLock now.
     372        unlockWebCoreReleaseLock();
     373        if (WebThreadIsCurrent())
    385374            [self _webcore_releaseOnWebThread];
    386             unlockWebCoreReleaseLock();
    387         }
    388     }
    389 }
    390 
    391 void WebCoreObjCDeallocWithWebThreadLockImpl(id self, SEL _cmd)
    392 {
    393     UNUSED_PARAM(_cmd);
     375        else
     376            WebThreadAdoptAndRelease(self);
     377    } else {
     378        // This is not the only reference retaining the object, so another
     379        // thread could also call release - hold the lock whilst calling
     380        // release to avoid a race condition.
     381        [self _webcore_releaseOnWebThread];
     382        unlockWebCoreReleaseLock();
     383    }
     384}
     385
     386void WebCoreObjCDeallocWithWebThreadLockImpl(id self, SEL)
     387{
    394388    lockWebCoreReleaseLock();
    395389    if (WebThreadIsLockedOrDisabled() || 1 != [self retainCount])
     
    400394}
    401395
    402 static void HandleWebThreadReleaseSource(void *info)
    403 {
    404     UNUSED_PARAM(info);
     396static void HandleWebThreadReleaseSource(void*)
     397{
    405398    ASSERT(WebThreadIsCurrent());
    406399
    407400    int result = pthread_mutex_lock(&WebThreadReleaseLock);
    408     ASSERT_WITH_MESSAGE(result == 0, "Release lock failed with code:%d", result);
    409 
    410     CFMutableArrayRef objects = NULL;
     401    ASSERT_WITH_MESSAGE(!result, "Release lock failed with code:%d", result);
     402
     403    CFMutableArrayRef objects = nullptr;
    411404    if (CFArrayGetCount(WebThreadReleaseObjArray)) {
    412         objects = CFArrayCreateMutableCopy(NULL, 0, WebThreadReleaseObjArray);
     405        objects = CFArrayCreateMutableCopy(nullptr, 0, WebThreadReleaseObjArray);
    413406        CFArrayRemoveAllValues(WebThreadReleaseObjArray);
    414407    }
    415408
    416409    result = pthread_mutex_unlock(&WebThreadReleaseLock);
    417     ASSERT_WITH_MESSAGE(result == 0, "Release unlock failed with code:%d", result);
     410    ASSERT_WITH_MESSAGE(!result, "Release unlock failed with code:%d", result);
    418411
    419412    if (!objects)
    420413        return;
    421414
    422     unsigned count = CFArrayGetCount(objects);
    423     unsigned i;
    424     for (i = 0; i < count; i++) {
     415    for (unsigned i = 0, count = CFArrayGetCount(objects); i < count; ++i) {
    425416        id obj = (id)CFArrayGetValueAtIndex(objects, i);
    426417#if LOG_RELEASES
     
    433424}
    434425
    435 void WebThreadCallDelegate(NSInvocation *invocation)
     426void WebThreadCallDelegate(NSInvocation* invocation)
    436427{
    437428    // NSInvocation released in SendMessage()
     
    439430}
    440431
    441 void WebThreadPostNotification(NSString *name, id object, id userInfo)
     432void WebThreadPostNotification(NSString* name, id object, id userInfo)
    442433{
    443434    if (pthread_main_np())
     
    450441}
    451442
    452 void WebThreadCallDelegateAsync(NSInvocation *invocation)
     443void WebThreadCallDelegateAsync(NSInvocation* invocation)
    453444{
    454445    ASSERT(invocation);
     
    460451
    461452// Note: despite the name, returns an autoreleased object.
    462 NSInvocation *WebThreadMakeNSInvocation(id target, SEL selector)
    463 {
    464     NSMethodSignature *signature = [target methodSignatureForSelector:selector];
     453NSInvocation* WebThreadMakeNSInvocation(id target, SEL selector)
     454{
     455    NSMethodSignature* signature = [target methodSignatureForSelector:selector];
    465456    ASSERT(signature);
    466457    if (signature) {
    467         NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
     458        NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature];
    468459        [invocation setSelector:selector];
    469460        [invocation setTarget:target];
     
    474465}
    475466
    476 static void MainRunLoopAutoUnlock(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *context)
    477 {
    478     UNUSED_PARAM(observer);
    479     UNUSED_PARAM(activity);
    480     UNUSED_PARAM(context);
     467static void MainRunLoopAutoUnlock(CFRunLoopObserverRef, CFRunLoopActivity, void*)
     468{
    481469    ASSERT(!WebThreadIsCurrent());
    482470
    483     if (sMainThreadModalCount != 0)
     471    if (sMainThreadModalCount)
    484472        return;
    485473   
     
    493481    ASSERT(!WebThreadIsCurrent());
    494482
    495     if (mainThreadLockCount == 0) {
     483    if (!mainThreadLockCount) {
    496484        CFRunLoopAddObserver(CFRunLoopGetCurrent(), mainRunLoopAutoUnlockObserver, kCFRunLoopCommonModes);   
    497485        _WebTryThreadLock(false);
     
    524512}
    525513
    526 static void WebRunLoopLock(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *context)
    527 {
    528     UNUSED_PARAM(observer);
    529     UNUSED_PARAM(context);
     514static void WebRunLoopLock(CFRunLoopObserverRef, CFRunLoopActivity activity, void*)
     515{
    530516    ASSERT(WebThreadIsCurrent());
    531517    ASSERT_UNUSED(activity, activity == kCFRunLoopAfterWaiting || activity == kCFRunLoopBeforeTimers || activity == kCFRunLoopBeforeSources);
     
    538524}
    539525
    540 static void WebRunLoopUnlock(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *context)
    541 {
    542     UNUSED_PARAM(observer);
    543     UNUSED_PARAM(context);
     526static void WebRunLoopUnlock(CFRunLoopObserverRef, CFRunLoopActivity activity, void*)
     527{
    544528    ASSERT(WebThreadIsCurrent());
    545529    ASSERT_UNUSED(activity, activity == kCFRunLoopBeforeWaiting || activity == kCFRunLoopExit);
     
    551535}
    552536
    553 static void MainRunLoopUnlockGuard(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *context)
    554 {
    555     UNUSED_PARAM(observer);
    556     UNUSED_PARAM(activity);
    557     UNUSED_PARAM(context);
     537static void MainRunLoopUnlockGuard(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void* context)
     538{
    558539    ASSERT(!WebThreadIsCurrent());
    559540
     
    562543    // runloop exit, and web thread gets stuck at waiting for the lock. If this happens,
    563544    // we need to help release the lock.  See <rdar://problem/8005192>.
    564     if (mainThreadLockCount != 0 && sMainThreadModalCount == 0) {
     545    if (mainThreadLockCount && !sMainThreadModalCount) {
    565546        NSLog(@"WARNING: Main thread didn't release the lock at last runloop exit!");
    566547
    567548        MainRunLoopAutoUnlock(observer, activity, context);
    568         if (mainThreadLockCount != 0)
     549        if (mainThreadLockCount)
    569550            mainThreadLockCount = 0;
    570551    }
     
    613594}
    614595
    615 static void FreeThreadContext(void *threadContext)
    616 {
    617     if (threadContext != NULL)
    618        free(threadContext);
    619 }
    620 
    621 static void InitThreadContextKey()
    622 {
    623     int error = pthread_key_create(&threadContextKey, FreeThreadContext);
    624     if (error)
    625         CRASH();
    626 }
    627 
    628 static WebThreadContext *CurrentThreadContext(void)
    629 {
    630     static pthread_once_t initControl = PTHREAD_ONCE_INIT;
    631     pthread_once(&initControl, InitThreadContextKey);   
    632 
    633     WebThreadContext *threadContext = (WebThreadContext*)pthread_getspecific(threadContextKey);
    634     if (threadContext == NULL) {
    635         threadContext = (WebThreadContext *)calloc(sizeof(WebThreadContext), 1);
     596static WebThreadContext* CurrentThreadContext(void)
     597{
     598    static std::once_flag flag;
     599    std::call_once(flag, [] {
     600        int error = pthread_key_create(&threadContextKey, [] (void* threadContext) {
     601            if (threadContext)
     602                free(threadContext);
     603        });
     604        RELEASE_ASSERT(!error);
     605    });
     606
     607    WebThreadContext* threadContext = (WebThreadContext*)pthread_getspecific(threadContextKey);
     608    if (!threadContext) {
     609        threadContext = static_cast<WebThreadContext*>(calloc(sizeof(WebThreadContext), 1));
    636610        pthread_setspecific(threadContextKey, threadContext);
    637611    }       
     
    639613}
    640614
    641 static
    642 #ifndef __llvm__
    643 NO_RETURN
    644 #endif
    645 void *RunWebThread(void *arg)
     615static void* RunWebThread(void*)
    646616{
    647617    FloatingPointEnvironment::singleton().propagateMainThreadEnvironment();
    648618
    649     UNUSED_PARAM(arg);
    650619    // WTF::initializeMainThread() needs to be called before JSC::initializeThreading() since the
    651620    // code invoked by the latter needs to know if it's running on the WebThread. See
     
    666635    webThreadNSRunLoop = [[NSRunLoop currentRunLoop] retain];
    667636
    668     CFRunLoopObserverRef webRunLoopLockObserverRef = CFRunLoopObserverCreate(NULL, kCFRunLoopBeforeTimers|kCFRunLoopBeforeSources|kCFRunLoopAfterWaiting, YES, 0, WebRunLoopLock, NULL);
     637    CFRunLoopObserverRef webRunLoopLockObserverRef = CFRunLoopObserverCreate(nullptr, kCFRunLoopBeforeTimers | kCFRunLoopBeforeSources | kCFRunLoopAfterWaiting, YES, 0, WebRunLoopLock, nullptr);
    669638    CFRunLoopAddObserver(webThreadRunLoop, webRunLoopLockObserverRef, kCFRunLoopCommonModes);
    670639    CFRelease(webRunLoopLockObserverRef);
     
    673642
    674643    // We must have the lock when CA paints in the web thread. CA commits at 2000000 so we use larger order number than that to free the lock.
    675     CFRunLoopObserverRef webRunLoopUnlockObserverRef = CFRunLoopObserverCreate(NULL, kCFRunLoopBeforeWaiting|kCFRunLoopExit, YES, 2500000, WebRunLoopUnlock, NULL);   
     644    CFRunLoopObserverRef webRunLoopUnlockObserverRef = CFRunLoopObserverCreate(nullptr, kCFRunLoopBeforeWaiting | kCFRunLoopExit, YES, 2500000, WebRunLoopUnlock, nullptr);   
    676645    CFRunLoopAddObserver(webThreadRunLoop, webRunLoopUnlockObserverRef, kCFRunLoopCommonModes);
    677646    CFRelease(webRunLoopUnlockObserverRef);   
    678647
    679     CFRunLoopSourceContext ReleaseSourceContext = {0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, HandleWebThreadReleaseSource};
    680     WebThreadReleaseSource = CFRunLoopSourceCreate(NULL, -1, &ReleaseSourceContext);
     648    CFRunLoopSourceContext ReleaseSourceContext = {0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, HandleWebThreadReleaseSource};
     649    WebThreadReleaseSource = CFRunLoopSourceCreate(nullptr, -1, &ReleaseSourceContext);
    681650    CFRunLoopAddSource(webThreadRunLoop, WebThreadReleaseSource, kCFRunLoopDefaultMode);
    682651
     
    689658        CFRunLoopRunInMode(kCFRunLoopDefaultMode, DistantFuture, true);
    690659
    691 #ifdef __llvm__
    692     return NULL;
    693 #endif 
     660    return nullptr;
    694661}
    695662
     
    707674    // can later set it's thread-specific data to point to the same objects.
    708675    WebCore::ThreadGlobalData& unused = WebCore::threadGlobalData();
    709     (void)unused;
     676    UNUSED_PARAM(unused);
    710677
    711678    RunLoop::initializeMainRunLoop();
     
    728695
    729696    CFRunLoopRef runLoop = CFRunLoopGetCurrent();
    730     CFRunLoopSourceContext delegateSourceContext = {0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, HandleDelegateSource};
    731     delegateSource = CFRunLoopSourceCreate(NULL, 0, &delegateSourceContext);
     697    CFRunLoopSourceContext delegateSourceContext = {0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, HandleDelegateSource};
     698    delegateSource = CFRunLoopSourceCreate(nullptr, 0, &delegateSourceContext);
    732699
    733700    // We shouldn't get delegate callbacks while scrolling, but there might be
     
    738705    sAsyncDelegates = [[NSMutableArray alloc] init];
    739706
    740     mainRunLoopAutoUnlockObserver = CFRunLoopObserverCreate(NULL, kCFRunLoopBeforeWaiting | kCFRunLoopExit, YES, 3000001, MainRunLoopAutoUnlock, NULL);
     707    mainRunLoopAutoUnlockObserver = CFRunLoopObserverCreate(nullptr, kCFRunLoopBeforeWaiting | kCFRunLoopExit, YES, 3000001, MainRunLoopAutoUnlock, nullptr);
    741708
    742709    pthread_attr_t tattr;
     
    760727        FloatingPointEnvironment::singleton().saveMainThreadEnvironment();
    761728
    762         pthread_create(&webThread, &tattr, RunWebThread, NULL);
     729        pthread_create(&webThread, &tattr, RunWebThread, nullptr);
    763730        pthread_attr_destroy(&tattr);
    764731
     
    769736}
    770737
    771 static int WebTimedConditionLock (pthread_cond_t *condition, pthread_mutex_t *lock, CFAbsoluteTime interval)
     738static int WebTimedConditionLock(pthread_cond_t* condition, pthread_mutex_t* lock, CFAbsoluteTime interval)
    772739{
    773740    struct timespec time;
     
    795762       
    796763           
    797     int result;
    798764    bool busy = false;
    799765    if (shouldTry) {
    800         result = pthread_mutex_trylock(&webLock);
    801         if (result == EBUSY) {
     766        int result = pthread_mutex_trylock(&webLock);
     767        if (result == EBUSY)
    802768            busy = true;
    803         } else
    804             ASSERT_WITH_MESSAGE(result == 0, "try web lock failed with code:%d", result);
    805     }
    806     else {
    807         result = pthread_mutex_lock(&webLock);
    808         ASSERT_WITH_MESSAGE(result == 0, "web lock failed with code:%d", result);
     769        else
     770            ASSERT_WITH_MESSAGE(!result, "try web lock failed with code:%d", result);
     771    } else {
     772        int result = pthread_mutex_lock(&webLock);
     773        ASSERT_WITH_MESSAGE_UNUSED(result, !result, "web lock failed with code:%d", result);
    809774    }
    810775   
     
    890855    otherThreadLockCount--;
    891856   
    892     int result;
    893     result = pthread_mutex_unlock(&webLock);
    894     ASSERT_WITH_MESSAGE(result == 0, "web unlock failed with code:%d", result);
     857    int result = pthread_mutex_unlock(&webLock);
     858    ASSERT_WITH_MESSAGE_UNUSED(result, !result, "web unlock failed with code:%d", result);
    895859}
    896860
     
    899863    ASSERT(!WebThreadIsCurrent());
    900864
    901     CFRunLoopObserverRef mainRunLoopUnlockGuardObserver = CFRunLoopObserverCreate(NULL, kCFRunLoopEntry, YES, 0, MainRunLoopUnlockGuard, NULL);
     865    CFRunLoopObserverRef mainRunLoopUnlockGuardObserver = CFRunLoopObserverCreate(nullptr, kCFRunLoopEntry, YES, 0, MainRunLoopUnlockGuard, nullptr);
    902866    CFRunLoopAddObserver(CFRunLoopGetMain(), mainRunLoopUnlockGuardObserver, kCFRunLoopCommonModes);
    903867    CFRelease(mainRunLoopUnlockGuardObserver);
     
    914878   
    915879    if (!WebThreadIsCurrent()) {
    916         ASSERT(mainThreadLockCount != 0);
     880        ASSERT(mainThreadLockCount);
    917881        mainThreadLockCount--;
    918882    } else {   
     
    921885    }
    922886   
    923     int result;
    924     result = pthread_mutex_unlock(&webLock);
    925     ASSERT_WITH_MESSAGE(result == 0, "web unlock failed with code:%d", result);   
     887    int result = pthread_mutex_unlock(&webLock);
     888    ASSERT_WITH_MESSAGE_UNUSED(result, !result, "web unlock failed with code:%d", result);   
    926889}
    927890
     
    930893    if (WebThreadIsCurrent())
    931894        return webThreadLockCount;
    932     else if (pthread_main_np())
     895
     896    if (pthread_main_np())
    933897        return mainThreadLockCount;
    934     else
    935         return otherThreadLockCount;
     898
     899    return otherThreadLockCount;
    936900}
    937901
     
    956920   
    957921    ASSERT(WebThreadIsLocked());
    958     ASSERT(sMainThreadModalCount != 0);
     922    ASSERT(sMainThreadModalCount);
    959923    --sMainThreadModalCount;
    960924}
     
    980944}
    981945
    982 WebThreadContext *WebThreadCurrentContext(void)
     946WebThreadContext* WebThreadCurrentContext(void)
    983947{
    984948    return CurrentThreadContext();
     
    989953    RELEASE_ASSERT_WITH_MESSAGE(!WebCore::IOSApplication::isWebProcess(), "The WebProcess should never run a Web Thread");
    990954
    991     static pthread_once_t initControl = PTHREAD_ONCE_INIT;
    992     pthread_once(&initControl, StartWebThread);
     955    static std::once_flag flag;
     956    std::call_once(flag, StartWebThread);
    993957}
    994958
Note: See TracChangeset for help on using the changeset viewer.