Title: [221083] trunk/Source/WebCore
Revision
221083
Author
[email protected]
Date
2017-08-23 10:41:39 -0700 (Wed, 23 Aug 2017)

Log Message

Race condition in StartWebThread causing crash
https://bugs.webkit.org/show_bug.cgi?id=175852

Reviewed by Mark Lam.

When starting web thread, the main thread waits for completion of web thread initialization
by using pthread_cond_t. However, the main thread may be woken up due to the existence of
the spurious wake up of pthread_cond_t.

Instead, we should use WTF::Lock and WTF::Condition. Since our StartWebThread already calls
WTF::initializeThreading, it is safe to use WTF::Lock and WTF::Condition. And our WTF::Condition
does not have the spurious wake up problem as described in Condition.h.

* platform/ios/wak/WebCoreThread.mm:
(RunWebThread):
(StartWebThread):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (221082 => 221083)


--- trunk/Source/WebCore/ChangeLog	2017-08-23 17:16:51 UTC (rev 221082)
+++ trunk/Source/WebCore/ChangeLog	2017-08-23 17:41:39 UTC (rev 221083)
@@ -1,3 +1,22 @@
+2017-08-23  Yusuke Suzuki  <[email protected]>
+
+        Race condition in StartWebThread causing crash
+        https://bugs.webkit.org/show_bug.cgi?id=175852
+
+        Reviewed by Mark Lam.
+
+        When starting web thread, the main thread waits for completion of web thread initialization
+        by using pthread_cond_t. However, the main thread may be woken up due to the existence of
+        the spurious wake up of pthread_cond_t.
+
+        Instead, we should use WTF::Lock and WTF::Condition. Since our StartWebThread already calls
+        WTF::initializeThreading, it is safe to use WTF::Lock and WTF::Condition. And our WTF::Condition
+        does not have the spurious wake up problem as described in Condition.h.
+
+        * platform/ios/wak/WebCoreThread.mm:
+        (RunWebThread):
+        (StartWebThread):
+
 2017-08-23  Brent Fulgham  <[email protected]>
 
         Ensure media controls host exists before using it

Modified: trunk/Source/WebCore/platform/ios/wak/WebCoreThread.mm (221082 => 221083)


--- trunk/Source/WebCore/platform/ios/wak/WebCoreThread.mm	2017-08-23 17:16:51 UTC (rev 221082)
+++ trunk/Source/WebCore/platform/ios/wak/WebCoreThread.mm	2017-08-23 17:41:39 UTC (rev 221083)
@@ -127,8 +127,8 @@
 
 static CFRunLoopObserverRef mainRunLoopAutoUnlockObserver;
 
-static pthread_mutex_t startupLock = PTHREAD_MUTEX_INITIALIZER;
-static pthread_cond_t startupCondition = PTHREAD_COND_INITIALIZER;
+static StaticLock startupLock;
+static StaticCondition startupCondition;
 
 static WebThreadContext *webThreadContext;
 static pthread_key_t threadContextKey;
@@ -681,15 +681,11 @@
     WebThreadReleaseSource = CFRunLoopSourceCreate(NULL, -1, &ReleaseSourceContext);
     CFRunLoopAddSource(webThreadRunLoop, WebThreadReleaseSource, kCFRunLoopDefaultMode);
 
-    int result = pthread_mutex_lock(&startupLock);
-    ASSERT_WITH_MESSAGE(result == 0, "startup lock failed with code:%d", result);
+    {
+        LockHolder locker(startupLock);
+        startupCondition.notifyOne();
+    }
 
-    result = pthread_cond_signal(&startupCondition);
-    ASSERT_WITH_MESSAGE(result == 0, "startup signal failed with code:%d", result);
-
-    result = pthread_mutex_unlock(&startupLock);
-    ASSERT_WITH_MESSAGE(result == 0, "startup unlock failed with code:%d", result);
-
     while (1)
         CFRunLoopRunInMode(kCFRunLoopDefaultMode, DistantFuture, true);
 
@@ -758,21 +754,18 @@
     pthread_attr_setschedparam(&tattr, &param);
 
     // Wait for the web thread to startup completely before we continue.
-    int result = pthread_mutex_lock(&startupLock);
-    ASSERT_WITH_MESSAGE(result == 0, "startup lock failed with code:%d", result);
+    {
+        LockHolder locker(startupLock);
 
-    // Propagate the mainThread's fenv to workers & the web thread.
-    FloatingPointEnvironment::singleton().saveMainThreadEnvironment();
+        // Propagate the mainThread's fenv to workers & the web thread.
+        FloatingPointEnvironment::singleton().saveMainThreadEnvironment();
 
-    pthread_create(&webThread, &tattr, RunWebThread, NULL);
-    pthread_attr_destroy(&tattr);
+        pthread_create(&webThread, &tattr, RunWebThread, NULL);
+        pthread_attr_destroy(&tattr);
 
-    result = pthread_cond_wait(&startupCondition, &startupLock);
-    ASSERT_WITH_MESSAGE(result == 0, "startup wait failed with code:%d", result);
+        startupCondition.wait(startupLock);
+    }
 
-    result = pthread_mutex_unlock(&startupLock);
-    ASSERT_WITH_MESSAGE(result == 0, "startup unlock failed with code:%d", result);
-
     initializeApplicationUIThreadIdentifier();
 }
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to