+2005-11-21 Maciej Stachowiak <mjs@apple.com>
+
+ Reviewed by Geoff.
+
+ <rdar://problem/4139620> Seed: WebKit: hang when sending XMLHttpRequest if automatic proxy config is used
+
+ Also factored locking code completely into a separate class, and
+ added a convenient packaged way to temporarily drop locks.
+
+ * JavaScriptCore.xcodeproj/project.pbxproj:
+ * kjs/JSLock.cpp: Added.
+ (KJS::initializeInterpreterLock):
+ (KJS::InterpreterLock::lock):
+ (KJS::InterpreterLock::unlock):
+ (KJS::InterpreterLock::lockCount):
+ (KJS::InterpreterLock::DropAllLocks::DropAllLocks):
+ (KJS::InterpreterLock::DropAllLocks::~DropAllLocks):
+ * kjs/JSLock.h: Added.
+ (KJS::InterpreterLock::InterpreterLock):
+ (KJS::InterpreterLock::~InterpreterLock):
+ * kjs/internal.cpp:
+ * kjs/internal.h:
+ * kjs/interpreter.cpp:
+ * kjs/interpreter.h:
+ * kjs/protect.h:
+ * kjs/testkjs.cpp:
+ (TestFunctionImp::callAsFunction):
+
2005-11-21 Eric Seidel <eseidel@apple.com>
Rubber-stamped by hyatt.
65E217BE08E7EECC0023E5F6 /* Assertions.mm in Sources */ = {isa = PBXBuildFile; fileRef = 65E217B808E7EECC0023E5F6 /* Assertions.mm */; };
65E217BF08E7EECC0023E5F6 /* FastMalloc.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 65E217B908E7EECC0023E5F6 /* FastMalloc.cpp */; };
65E217C008E7EECC0023E5F6 /* FastMalloc.h in Headers */ = {isa = PBXBuildFile; fileRef = 65E217BA08E7EECC0023E5F6 /* FastMalloc.h */; settings = {ATTRIBUTES = (Private, ); }; };
+ 65EA4C9B092AF9E20093D800 /* JSLock.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 65EA4C99092AF9E20093D800 /* JSLock.cpp */; };
+ 65EA4C9C092AF9E20093D800 /* JSLock.h in Headers */ = {isa = PBXBuildFile; fileRef = 65EA4C9A092AF9E20093D800 /* JSLock.h */; settings = {ATTRIBUTES = (Private, ); }; };
930754C108B0F68000AB3056 /* pcre_compile.c in Sources */ = {isa = PBXBuildFile; fileRef = 930754BF08B0F68000AB3056 /* pcre_compile.c */; };
930754D008B0F74600AB3056 /* pcre_tables.c in Sources */ = {isa = PBXBuildFile; fileRef = 930754CE08B0F74500AB3056 /* pcre_tables.c */; };
930754D308B0F76300AB3056 /* pcre_globals.c in Sources */ = {isa = PBXBuildFile; fileRef = 930754D108B0F76200AB3056 /* pcre_globals.c */; };
65E217B808E7EECC0023E5F6 /* Assertions.mm */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.objcpp; path = Assertions.mm; sourceTree = "<group>"; };
65E217B908E7EECC0023E5F6 /* FastMalloc.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = FastMalloc.cpp; sourceTree = "<group>"; };
65E217BA08E7EECC0023E5F6 /* FastMalloc.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = FastMalloc.h; sourceTree = "<group>"; };
+ 65EA4C99092AF9E20093D800 /* JSLock.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = JSLock.cpp; sourceTree = "<group>"; };
+ 65EA4C9A092AF9E20093D800 /* JSLock.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = JSLock.h; sourceTree = "<group>"; };
700DA117065984CE00747C0B /* WebScriptObjectPrivate.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = WebScriptObjectPrivate.h; path = bindings/objc/WebScriptObjectPrivate.h; sourceTree = "<group>"; };
704FD35305697E6D003DBED9 /* bool_object.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = bool_object.h; sourceTree = "<group>"; };
704FD44505698F17003DBED9 /* runtime.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = runtime.cpp; path = bindings/runtime.cpp; sourceTree = "<group>"; };
08FB77AEFE84172EC02AAC07 /* Classes */ = {
isa = PBXGroup;
children = (
+ 65EA4C99092AF9E20093D800 /* JSLock.cpp */,
+ 65EA4C9A092AF9E20093D800 /* JSLock.h */,
65621E6B089E859700760F35 /* property_slot.cpp */,
65621E6C089E859700760F35 /* property_slot.h */,
938772E5038BFE19008635CE /* array_instance.h */,
65DFC93508EA173A00F7300B /* HashTraits.h in Headers */,
6557E8F808EA5D4D0049CDFC /* HashMapPtrSpec.h in Headers */,
65D7D19C08F10B5B0015ABD8 /* FastMallocInternal.h in Headers */,
+ 65EA4C9C092AF9E20093D800 /* JSLock.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
65E217BF08E7EECC0023E5F6 /* FastMalloc.cpp in Sources */,
6541BD7408E80A17002CBEE7 /* TCSystemAlloc.cpp in Sources */,
65DFC93308EA173A00F7300B /* HashTable.cpp in Sources */,
+ 65EA4C9B092AF9E20093D800 /* JSLock.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
--- /dev/null
+// -*- mode: c++; c-basic-offset: 4 -*-
+/*
+ * This file is part of the KDE libraries
+ * Copyright (C) 2005 Apple Computer, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB. If not, write to
+ * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ */
+
+#include "config.h"
+#include "JSLock.h"
+
+#include "collector.h"
+
+namespace KJS {
+
+#if KJS_MULTIPLE_THREADS
+
+static pthread_once_t interpreterLockOnce = PTHREAD_ONCE_INIT;
+static pthread_mutex_t interpreterLock;
+static int interpreterLockCount = 0;
+
+static void initializeInterpreterLock()
+{
+ pthread_mutexattr_t attr;
+
+ pthread_mutexattr_init(&attr);
+ pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
+
+ pthread_mutex_init(&interpreterLock, &attr);
+}
+
+void InterpreterLock::lock()
+{
+ pthread_once(&interpreterLockOnce, initializeInterpreterLock);
+ pthread_mutex_lock(&interpreterLock);
+ interpreterLockCount++;
+ Collector::registerThread();
+
+ // FIXME: Hack-o-rama. To prevent construction of a global object with a null prototype (4342216),
+ // we need to intialize our constants before the first object is constructed. InterpreterImp::lock()
+ // is a good place to do this because you have to call it before doing any allocations. Once we change our
+ // implementation to use immediate values, we should remove this code.
+ ConstantValues::initIfNeeded();
+}
+
+void InterpreterLock::unlock()
+{
+ interpreterLockCount--;
+ pthread_mutex_unlock(&interpreterLock);
+}
+
+#else
+
+// If threading support is off, set the lock count to a constant value of 1 so assertions
+// that the lock is held don't fail
+const int interpreterLockCount = 1;
+
+void InterpreterLock::lock()
+{
+}
+
+void InterpreterLock::unlock()
+{
+}
+
+#endif
+
+int InterpreterLock::lockCount()
+{
+ return interpreterLockCount;
+}
+
+InterpreterLock::DropAllLocks::DropAllLocks()
+{
+ int lockCount = InterpreterLock::lockCount();
+ for (int i = 0; i < lockCount; i++) {
+ InterpreterLock::unlock();
+ }
+ m_lockCount = lockCount;
+}
+
+InterpreterLock::DropAllLocks::~DropAllLocks()
+{
+ int lockCount = m_lockCount;
+ for (int i = 0; i < lockCount; i++) {
+ InterpreterLock::lock();
+ }
+}
+
+}
--- /dev/null
+// -*- mode: c++; c-basic-offset: 4 -*-
+/*
+ * This file is part of the KDE libraries
+ * Copyright (C) 2005 Apple Computer, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB. If not, write to
+ * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ */
+
+#ifndef KJS_INTERPRETER_LOCK_H
+#define KJS_INTERPRETER_LOCK_H
+
+namespace KJS {
+
+ // to make it safe to use JavaScript on multiple threads, it is
+ // important to lock before doing anything that allocates a
+ // garbage-collected object or which may affect other shared state
+ // such as the protect count hash table. The simplest way to do
+ // this is by having a local InterpreterLock object for the scope
+ // where the lock must be held. The lock is recursive so nesting
+ // is ok.
+
+ // Sometimes it is necessary to temporarily release the lock -
+ // since it is recursive you have to actually release all locks
+ // held by your thread. This is safe to do if you are executing
+ // code that doesn't require the lock, and reacquire the right
+ // number of locks at the end. You can do this by constructing a
+ // locally scoped InterpreterLock::DropAllLocks object.
+
+ class InterpreterLock
+ {
+ public:
+ InterpreterLock()
+ {
+ lock();
+ }
+ ~InterpreterLock() {
+ unlock();
+ }
+
+ static void lock();
+ static void unlock();
+ static int lockCount();
+
+ class DropAllLocks {
+ public:
+ DropAllLocks();
+ ~DropAllLocks();
+ private:
+ int m_lockCount;
+
+ DropAllLocks(const DropAllLocks&);
+ DropAllLocks& operator=(const DropAllLocks&);
+ };
+
+ private:
+ InterpreterLock(const InterpreterLock&);
+ InterpreterLock& operator=(const InterpreterLock&);
+ };
+
+} // namespace
+
+#endif // KJS_INTERPRETER_LOCK_H
void* Collector::allocate(size_t s)
{
- assert(Interpreter::lockCount() > 0);
+ assert(InterpreterLock::lockCount() > 0);
// collect if needed
size_t numLiveObjects = heap.numLiveObjects;
bool Collector::collect()
{
- assert(Interpreter::lockCount() > 0);
+ assert(InterpreterLock::lockCount() > 0);
if (InterpreterImp::s_hook) {
InterpreterImp *scr = InterpreterImp::s_hook;
#define HAVE_SYS_TIME_H 1
#define HAVE_SYS_TIMEB_H 1
+#define KJS_MULTIPLE_THREADS 1
+
#elif WIN32
#define HAVE_FLOAT_H 1
#endif // APPLE_CHANGES
-#if defined(KJS_MULTIPLE_THREADS) && KJS_MULTIPLE_THREADS
-
-static pthread_once_t interpreterLockOnce = PTHREAD_ONCE_INIT;
-static pthread_mutex_t interpreterLock;
-static int interpreterLockCount = 0;
-
-static void initializeInterpreterLock()
-{
- pthread_mutexattr_t attr;
-
- pthread_mutexattr_init(&attr);
- pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
-
- pthread_mutex_init(&interpreterLock, &attr);
-}
-
-static inline void lockInterpreter()
-{
- pthread_once(&interpreterLockOnce, initializeInterpreterLock);
- pthread_mutex_lock(&interpreterLock);
- interpreterLockCount++;
- Collector::registerThread();
-}
-
-static inline void unlockInterpreter()
-{
- interpreterLockCount--;
- pthread_mutex_unlock(&interpreterLock);
-}
-
-#else
-
-static inline void initializeInterpreterLock() { }
-static inline void lockInterpreter() { }
-static inline void unlockInterpreter() { }
-
-const int interpreterLockCount = 1;
-
-#endif
-
// ------------------------------ UndefinedImp ---------------------------------
ValueImp *UndefinedImp::toPrimitive(ExecState *, Type) const
recursion = 0;
}
-void InterpreterImp::lock()
-{
- lockInterpreter();
-
- // FIXME: Hack-o-rama. To prevent construction of a global object with a null prototype (4342216),
- // we need to intialize our constants before the first object is constructed. InterpreterImp::lock()
- // is a good place to do this because you have to call it before doing any allocations. Once we change our
- // implementation to use immediate values, we should remove this code.
- ConstantValues::initIfNeeded();
-}
-
-int InterpreterImp::lockCount()
-{
- return interpreterLockCount;
-}
-
-void InterpreterImp::unlock()
-{
- unlockInterpreter();
-}
-
void InterpreterImp::initGlobalObject()
{
Identifier::init();
#include "scope_chain.h"
#include <kxmlcore/SharedPtr.h>
-#if __APPLE__
-#define KJS_MULTIPLE_THREADS 1
-#endif
-
#define I18N_NOOP(s) s
namespace KJS {
Interpreter *interpreter() const { return m_interpreter; }
void initGlobalObject();
- static void lock();
- static void unlock();
- static int lockCount();
void mark();
rep->initGlobalObject();
}
-void Interpreter::lock()
-{
- InterpreterImp::lock();
-}
-
-void Interpreter::unlock()
-{
- InterpreterImp::unlock();
-}
-
-int Interpreter::lockCount()
-{
- return InterpreterImp::lockCount();
-}
-
ExecState *Interpreter::globalExec()
{
return rep->globalExec();
void initGlobalObject();
- static void lock();
- static void unlock();
- static int lockCount();
-
/**
* Returns the execution state object which can be used to execute
* scripts using this interpreter at a the "global" level, i.e. one
ValueImp *_exception;
};
- class InterpreterLock
- {
- public:
- InterpreterLock() { Interpreter::lock(); }
- ~InterpreterLock() { Interpreter::unlock(); }
- private:
- InterpreterLock(const InterpreterLock &);
- InterpreterLock &operator =(const InterpreterLock &);
- };
-
} // namespace
#endif // _KJS_INTERPRETER_H_
#include "reference.h"
#include "value.h"
#include "protected_values.h"
-#include "interpreter.h"
+#include "JSLock.h"
namespace KJS {
int ProtectedValues::getProtectCount(ValueImp *k)
{
assert(k);
- assert(InterpreterImp::lockCount() > 0);
+ assert(InterpreterLock::lockCount() > 0);
if (!_table)
return 0;
void ProtectedValues::increaseProtectCount(ValueImp *k)
{
assert(k);
- assert(InterpreterImp::lockCount() > 0);
+ assert(InterpreterLock::lockCount() > 0);
if (SimpleNumber::is(k))
return;
void ProtectedValues::decreaseProtectCount(ValueImp *k)
{
assert(k);
- assert(InterpreterImp::lockCount() > 0);
+ assert(InterpreterLock::lockCount() > 0);
if (SimpleNumber::is(k))
return;
#include "types.h"
#include "interpreter.h"
#include "collector.h"
+#include "JSLock.h"
using namespace KJS;
exit(0);
return Undefined();
case GC:
- Interpreter::lock();
+ {
+ InterpreterLock lock;
Collector::collect();
- Interpreter::unlock();
+ }
break;
default:
break;
+2005-11-16 Maciej Stachowiak <mjs@apple.com>
+
+ Reviewed by Geoff.
+
+ <rdar://problem/4139620> Seed: WebKit: hang when sending XMLHttpRequest if automatic proxy config is used
+
+ * khtml/ecma/kjs_events.cpp:
+ (KJS::JSLazyEventListener::parseCode):
+ * khtml/ecma/xmlhttprequest.cpp:
+ (KJS::XMLHttpRequest::send):
+ * kwq/WebCoreJavaScript.mm:
+
2005-11-22 eseidel <eseidel@apple.com>
No review required, svg target only.
// Add the event's home element to the scope
// (and the document, and the form - see HTMLElement::eventHandlerScope)
ScopeChain scope = listener->scope();
-
- Interpreter::lock();
- ObjectImp *thisObj = static_cast<ObjectImp *>(getDOMNode(exec, originalNode));
- Interpreter::unlock();
-
+
+ ObjectImp *thisObj;
+ { // scope
+ InterpreterLock lock;
+ thisObj = static_cast<ObjectImp *>(getDOMNode(exec, originalNode));
+ }
+
if (thisObj) {
static_cast<DOMNode*>(thisObj)->pushEventHandlerScope(exec, scope);
listener->setScope(scope);
KURL finalURL;
QString headers;
- data = KWQServeSynchronousRequest(khtml::Cache::loader(), doc->docLoader(), job, finalURL, headers);
+ { // scope
+ // avoid deadlock in case the loader wants to use JS on a background thread
+ InterpreterLock::DropAllLocks dropLocks;
+
+ data = KWQServeSynchronousRequest(khtml::Cache::loader(), doc->docLoader(), job, finalURL, headers);
+ }
+
job = 0;
processSyncLoadResults(data, finalURL, headers);
#import <JavaScriptCore/collector.h>
#import <JavaScriptCore/interpreter.h>
+#import <JavaScriptCore/JSLock.h>
using KJS::Collector;
using KJS::Interpreter;