Move JavaScriptCore thread testing code to pthread specific directory
[WebKit-https.git] / WebKitTools / DumpRenderTree / pthreads / JavaScriptThreadingPthreads.cpp
similarity index 68%
rename from WebKitTools/DumpRenderTree/mac/JavaScriptThreading.cpp
rename to WebKitTools/DumpRenderTree/pthreads/JavaScriptThreadingPthreads.cpp
index c382203dabe818b449d72d6c2236b04d66c24268..a305414a339a06996f385303ab4c9d23287524f1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005, 2006, 2007 Apple, Inc.  All rights reserved.
+ * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved.
  *           (C) 2007 Graham Dennis (graham.dennis@gmail.com)
  *           (C) 2007 Eric Seidel <eric@webkit.org>
  *
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
  
-#import "JavaScriptThreading.h"
+#include "JavaScriptThreading.h"
 
-#import <CoreFoundation/CoreFoundation.h>
-#import <JavaScriptCore/JavaScriptCore.h>
-#import <pthread.h>
-#import <wtf/Assertions.h>
+#include <CoreFoundation/CoreFoundation.h>
+#include <JavaScriptCore/JavaScriptCore.h>
+#include <pthread.h>
+#include <wtf/Assertions.h>
+#include <wtf/HashSet.h>
 
 static pthread_mutex_t javaScriptThreadsMutex = PTHREAD_MUTEX_INITIALIZER;
 static bool javaScriptThreadsShouldTerminate;
 
 static const int javaScriptThreadsCount = 4;
-static CFMutableDictionaryRef javaScriptThreads()
+
+typedef HashSet<pthread_t> ThreadSet;
+
+static ThreadSet* javaScriptThreads()
 {
-    assert(pthread_mutex_trylock(&javaScriptThreadsMutex) == EBUSY);
-    static CFMutableDictionaryRef staticJavaScriptThreads;
-    if (!staticJavaScriptThreads)
-        staticJavaScriptThreads = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, NULL);
-    return staticJavaScriptThreads;
+    ASSERT(pthread_mutex_trylock(&javaScriptThreadsMutex) == EBUSY);
+    static ThreadSet staticJavaScriptThreads;
+    return &staticJavaScriptThreads;
 }
 
 // Loops forever, running a script and randomly respawning, until 
@@ -58,14 +60,14 @@ void* runJavaScriptThread(void* arg)
         "    array.push(String(i));"
         "}";
 
-    while(1) {
-        JSGlobalContextRef ctx = JSGlobalContextCreate(NULL);
+    while (1) {
+        JSGlobalContextRef ctx = JSGlobalContextCreate(0);
         JSStringRef scriptRef = JSStringCreateWithUTF8CString(script);
 
-        JSValueRef exception = NULL;
-        JSEvaluateScript(ctx, scriptRef, NULL, NULL, 0, &exception);
-        assert(!exception);
-        
+        JSValueRef exception = 0;
+        JSEvaluateScript(ctx, scriptRef, 0, 0, 0, &exception);
+        ASSERT(!exception);
+
         JSGlobalContextRelease(ctx);
         JSStringRelease(scriptRef);
         
@@ -82,11 +84,11 @@ void* runJavaScriptThread(void* arg)
         // Respawn probabilistically.
         if (random() % 5 == 0) {
             pthread_t pthread;
-            pthread_create(&pthread, NULL, &runJavaScriptThread, NULL);
+            pthread_create(&pthread, 0, &runJavaScriptThread, 0);
             pthread_detach(pthread);
 
-            CFDictionaryRemoveValue(javaScriptThreads(), pthread_self());
-            CFDictionaryAddValue(javaScriptThreads(), pthread, NULL);
+            javaScriptThreads()->remove(pthread_self());
+            javaScriptThreads()->add(pthread);
 
             pthread_mutex_unlock(&javaScriptThreadsMutex);
             return 0;
@@ -102,9 +104,9 @@ void startJavaScriptThreads()
 
     for (int i = 0; i < javaScriptThreadsCount; i++) {
         pthread_t pthread;
-        pthread_create(&pthread, NULL, &runJavaScriptThread, NULL);
+        pthread_create(&pthread, 0, &runJavaScriptThread, 0);
         pthread_detach(pthread);
-        CFDictionaryAddValue(javaScriptThreads(), pthread, NULL);
+        javaScriptThreads()->add(pthread);
     }
 
     pthread_mutex_unlock(&javaScriptThreadsMutex);
@@ -116,15 +118,13 @@ void stopJavaScriptThreads()
 
     javaScriptThreadsShouldTerminate = true;
 
-    pthread_t* pthreads[javaScriptThreadsCount] = { 0 };
-    ASSERT(CFDictionaryGetCount(javaScriptThreads()) == javaScriptThreadsCount);
-    CFDictionaryGetKeysAndValues(javaScriptThreads(), (const void**)pthreads, 0);
+    ASSERT(javaScriptThreads()->size() == javaScriptThreadsCount);
 
     pthread_mutex_unlock(&javaScriptThreadsMutex);
 
-    for (int i = 0; i < javaScriptThreadsCount; i++) {
-        pthread_t* pthread = pthreads[i];
-        pthread_join(*pthread, 0);
-        free(pthread);
+    ThreadSet::iterator end = javaScriptThreads()->end();
+    for (ThreadSet::iterator it = javaScriptThreads()->begin(); it != end; ++it) {
+        pthread_t pthread = *it;
+        pthread_join(pthread, 0);
     }
 }