Make JavaScriptGlue and JavaScriptCore API functions implicitly call initializeThreading
for the sake of non-WebKit clients.
JavaScriptCore:
* API/JSBase.cpp:
(JSGarbageCollect):
* API/JSContextRef.cpp:
(JSGlobalContextCreate):
These are the JavaScriptCore API bottlenecks. There are a few other JSStringRef
and JSClassRef functions that can be called earlier, but they do not do anything that
requires initializeThreading.
* kjs/InitializeThreading.cpp:
(KJS::doInitializeThreading):
(KJS::initializeThreading):
On Darwin, make the initialization happen under pthread_once, since there is no guarantee
that non-WebKit clients won't try to call this function re-entrantly.
* kjs/InitializeThreading.h:
* wtf/Threading.h:
Spell out initializeThreading contract.
* wtf/ThreadingPthreads.cpp: (WTF::isMainThread): Make sure that results are correct on
Darwin, even if threading was initialized from a secondary thread.
JavaScriptGlue:
* JavaScriptGlue.cpp:
(JSRunCreate):
(JSCollect):
(JSCreateJSArrayFromCFArray):
(JSLockInterpreter):
These are all possible JavaScriptGlue entry points.
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@32808
268f45cc-cd09-0410-ab3c-
d52691b4dbfc
#include "APICast.h"
#include <kjs/ExecState.h>
+#include <kjs/InitializeThreading.h>
+#include <kjs/interpreter.h>
#include <kjs/JSGlobalObject.h>
#include <kjs/JSLock.h>
-#include <kjs/interpreter.h>
#include <kjs/object.h>
using namespace KJS;
return true;
}
-void JSGarbageCollect(JSContextRef)
+void JSGarbageCollect(JSContextRef ctx)
{
+ // Unlikely, but it is legal to call JSGarbageCollect(0) before actually doing anything that would implicitly call initializeThreading().
+ if (!ctx)
+ initializeThreading();
+
JSLock lock;
// It might seem that we have a context passed to this function, and can use toJS(ctx)->heap(), but the parameter is likely to be NULL.
#include "JSContextRef.h"
#include "APICast.h"
+#include "InitializeThreading.h"
#include "JSCallbackObject.h"
#include "JSClassRef.h"
#include "JSGlobalObject.h"
JSGlobalContextRef JSGlobalContextCreate(JSClassRef globalObjectClass)
{
+ initializeThreading();
+
JSLock lock;
if (!globalObjectClass) {
2008-05-02 Alexey Proskuryakov <ap@webkit.org>
+ Reviewed by Darin.
+
+ Make JavaScriptGlue and JavaScriptCore API functions implicitly call initializeThreading
+ for the sake of non-WebKit clients.
+
+ * API/JSBase.cpp:
+ (JSGarbageCollect):
+ * API/JSContextRef.cpp:
+ (JSGlobalContextCreate):
+ These are the JavaScriptCore API bottlenecks. There are a few other JSStringRef
+ and JSClassRef functions that can be called earlier, but they do not do anything that
+ requires initializeThreading.
+
+ * kjs/InitializeThreading.cpp:
+ (KJS::doInitializeThreading):
+ (KJS::initializeThreading):
+ On Darwin, make the initialization happen under pthread_once, since there is no guarantee
+ that non-WebKit clients won't try to call this function re-entrantly.
+
+ * kjs/InitializeThreading.h:
+ * wtf/Threading.h:
+ Spell out initializeThreading contract.
+
+ * wtf/ThreadingPthreads.cpp: (WTF::isMainThread): Make sure that results are correct on
+ Darwin, even if threading was initialized from a secondary thread.
+
+2008-05-02 Alexey Proskuryakov <ap@webkit.org>
+
Reviewed by Geoffrey Garen.
https://bugs.webkit.org/show_bug.cgi?id=18826
namespace KJS {
-void initializeThreading()
+#if PLATFORM(DARWIN)
+static pthread_once_t initializeThreadingKeyOnce = PTHREAD_ONCE_INIT;
+#endif
+
+static void initializeThreadingOnce()
{
WTF::initializeThreading();
#if USE(MULTIPLE_THREADS)
- if (!s_dtoaP5Mutex) {
- s_dtoaP5Mutex = new Mutex;
- Heap::threadHeap();
- UString::null();
- Identifier::initializeIdentifierThreading();
- CommonIdentifiers::shared();
- lexer();
- initDateMath();
- JSGlobalObject::threadClassInfoHashTables();
- JSGlobalObject::head();
+ s_dtoaP5Mutex = new Mutex;
+ Heap::threadHeap();
+ UString::null();
+ Identifier::initializeIdentifierThreading();
+ CommonIdentifiers::shared();
+ lexer();
+ initDateMath();
+ JSGlobalObject::threadClassInfoHashTables();
+ JSGlobalObject::head();
+#endif
+}
+
+void initializeThreading()
+{
+#if PLATFORM(DARWIN)
+ pthread_once(&initializeThreadingKeyOnce, initializeThreadingOnce);
+#else
+ static bool initializedThreading = false;
+ if (!initializedThreading) {
+ initializeThreadingOnce();
+ initializedThreading = true;
}
#endif
}
namespace KJS {
+ // This function must be called from the main thread. It is safe to call it repeatedly.
+ // Darwin is an exception to this rule: it is OK to call this function from any thread, even reentrantly.
void initializeThreading();
}
#endif
};
+// This function must be called from the main thread. It is safe to call it repeatedly.
+// Darwin is an exception to this rule: it is OK to call it from any thread, the only requirement is that the calls are not reentrant.
void initializeThreading();
extern Mutex* atomicallyInitializedStaticMutex;
Mutex* atomicallyInitializedStaticMutex;
-static ThreadIdentifier mainThreadIdentifier;
+static ThreadIdentifier mainThreadIdentifier; // More precisely, the thread that was the first to call initializeThreading().
static Mutex& threadMapMutex()
{
bool isMainThread()
{
+#if PLATFORM(DARWIN)
+ return pthread_main_np();
+#else
return currentThread() == mainThreadIdentifier;
+#endif
}
Mutex::Mutex()
2008-05-02 Alexey Proskuryakov <ap@webkit.org>
+ Reviewed by Darin.
+
+ Make JavaScriptGlue and JavaScriptCore API functions implicitly call initializeThreading
+ for the sake of non-WebKit clients.
+
+ * JavaScriptGlue.cpp:
+ (JSRunCreate):
+ (JSCollect):
+ (JSCreateJSArrayFromCFArray):
+ (JSLockInterpreter):
+ These are all possible JavaScriptGlue entry points.
+
+2008-05-02 Alexey Proskuryakov <ap@webkit.org>
+
Reviewed by Geoffrey Garen.
https://bugs.webkit.org/show_bug.cgi?id=18826
#include "JSBase.h"
#include "JSObject.h"
#include "JSRun.h"
+#include <JavaScriptCore/InitializeThreading.h>
static CFTypeRef sJSCFNullRef = 0;
*/
JSRunRef JSRunCreate(CFStringRef jsSource, JSFlags inFlags)
{
+ initializeThreading();
+
JSRunRef result = 0;
if (jsSource)
{
/*
JSCollect - trigger garbage collection
*/
-void JSCollect(void)
+void JSCollect()
{
+ initializeThreading();
+
JSLock lock;
getThreadGlobalExecState()->heap()->collect();
}
CFMutableArrayRef JSCreateJSArrayFromCFArray(CFArrayRef array)
{
+ initializeThreading();
+
CFIndex count = array ? CFArrayGetCount(array) : 0;
CFArrayCallBacks arrayCallbacks;
CFMutableArrayRef jsArray;
void JSLockInterpreter()
{
+ initializeThreading();
JSLock::lock();
}