JavaScriptCore:

        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):

WebCore:

        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:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@11284 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/JavaScriptCore/ChangeLog b/JavaScriptCore/ChangeLog
index 1a9b312..b726fd1 100644
--- a/JavaScriptCore/ChangeLog
+++ b/JavaScriptCore/ChangeLog
@@ -1,3 +1,31 @@
+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.
diff --git a/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj b/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj
index 465aa39..a0bc01f 100644
--- a/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj
+++ b/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj
@@ -45,6 +45,8 @@
 		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 */; };
@@ -348,6 +350,8 @@
 		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>"; };
@@ -521,6 +525,8 @@
 		08FB77AEFE84172EC02AAC07 /* Classes */ = {
 			isa = PBXGroup;
 			children = (
+				65EA4C99092AF9E20093D800 /* JSLock.cpp */,
+				65EA4C9A092AF9E20093D800 /* JSLock.h */,
 				65621E6B089E859700760F35 /* property_slot.cpp */,
 				65621E6C089E859700760F35 /* property_slot.h */,
 				938772E5038BFE19008635CE /* array_instance.h */,
@@ -840,6 +846,7 @@
 				65DFC93508EA173A00F7300B /* HashTraits.h in Headers */,
 				6557E8F808EA5D4D0049CDFC /* HashMapPtrSpec.h in Headers */,
 				65D7D19C08F10B5B0015ABD8 /* FastMallocInternal.h in Headers */,
+				65EA4C9C092AF9E20093D800 /* JSLock.h in Headers */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -1222,6 +1229,7 @@
 				65E217BF08E7EECC0023E5F6 /* FastMalloc.cpp in Sources */,
 				6541BD7408E80A17002CBEE7 /* TCSystemAlloc.cpp in Sources */,
 				65DFC93308EA173A00F7300B /* HashTable.cpp in Sources */,
+				65EA4C9B092AF9E20093D800 /* JSLock.cpp in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
diff --git a/JavaScriptCore/kjs/JSLock.cpp b/JavaScriptCore/kjs/JSLock.cpp
new file mode 100644
index 0000000..67290f3
--- /dev/null
+++ b/JavaScriptCore/kjs/JSLock.cpp
@@ -0,0 +1,104 @@
+// -*- 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();
+    }
+}
+
+}
diff --git a/JavaScriptCore/kjs/JSLock.h b/JavaScriptCore/kjs/JSLock.h
new file mode 100644
index 0000000..762a219
--- /dev/null
+++ b/JavaScriptCore/kjs/JSLock.h
@@ -0,0 +1,76 @@
+// -*- 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
diff --git a/JavaScriptCore/kjs/collector.cpp b/JavaScriptCore/kjs/collector.cpp
index 3168e04..1d642f9f 100644
--- a/JavaScriptCore/kjs/collector.cpp
+++ b/JavaScriptCore/kjs/collector.cpp
@@ -106,7 +106,7 @@
 
 void* Collector::allocate(size_t s)
 {
-  assert(Interpreter::lockCount() > 0);
+  assert(InterpreterLock::lockCount() > 0);
 
   // collect if needed
   size_t numLiveObjects = heap.numLiveObjects;
@@ -393,7 +393,7 @@
 
 bool Collector::collect()
 {
-  assert(Interpreter::lockCount() > 0);
+  assert(InterpreterLock::lockCount() > 0);
 
   if (InterpreterImp::s_hook) {
     InterpreterImp *scr = InterpreterImp::s_hook;
diff --git a/JavaScriptCore/kjs/config.h b/JavaScriptCore/kjs/config.h
index 6293ded..8c38725 100644
--- a/JavaScriptCore/kjs/config.h
+++ b/JavaScriptCore/kjs/config.h
@@ -10,6 +10,8 @@
 #define HAVE_SYS_TIME_H 1
 #define HAVE_SYS_TIMEB_H 1
 
+#define KJS_MULTIPLE_THREADS 1
+
 #elif WIN32
 
 #define HAVE_FLOAT_H 1
diff --git a/JavaScriptCore/kjs/internal.cpp b/JavaScriptCore/kjs/internal.cpp
index cad5db2..a70234b 100644
--- a/JavaScriptCore/kjs/internal.cpp
+++ b/JavaScriptCore/kjs/internal.cpp
@@ -74,46 +74,6 @@
  
 #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
@@ -473,27 +433,6 @@
   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();
diff --git a/JavaScriptCore/kjs/internal.h b/JavaScriptCore/kjs/internal.h
index 58c69df..ee5c3a1 100644
--- a/JavaScriptCore/kjs/internal.h
+++ b/JavaScriptCore/kjs/internal.h
@@ -33,10 +33,6 @@
 #include "scope_chain.h"
 #include <kxmlcore/SharedPtr.h>
 
-#if __APPLE__
-#define KJS_MULTIPLE_THREADS 1
-#endif
-
 #define I18N_NOOP(s) s
 
 namespace KJS {
@@ -253,9 +249,6 @@
     Interpreter *interpreter() const { return m_interpreter; }
 
     void initGlobalObject();
-    static void lock();
-    static void unlock();
-    static int lockCount();
 
     void mark();
 
diff --git a/JavaScriptCore/kjs/interpreter.cpp b/JavaScriptCore/kjs/interpreter.cpp
index 067c06d..0cd81b2 100644
--- a/JavaScriptCore/kjs/interpreter.cpp
+++ b/JavaScriptCore/kjs/interpreter.cpp
@@ -99,21 +99,6 @@
   rep->initGlobalObject();
 }
 
-void Interpreter::lock()
-{
-  InterpreterImp::lock();
-}
-
-void Interpreter::unlock()
-{
-  InterpreterImp::unlock();
-}
-
-int Interpreter::lockCount()
-{
-  return InterpreterImp::lockCount();
-}
-
 ExecState *Interpreter::globalExec()
 {
   return rep->globalExec();
diff --git a/JavaScriptCore/kjs/interpreter.h b/JavaScriptCore/kjs/interpreter.h
index 20e4f52..965cd24 100644
--- a/JavaScriptCore/kjs/interpreter.h
+++ b/JavaScriptCore/kjs/interpreter.h
@@ -167,10 +167,6 @@
 
     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
@@ -482,16 +478,6 @@
     ValueImp *_exception;
   };
 
-    class InterpreterLock
-    {
-    public:
-        InterpreterLock() { Interpreter::lock(); }
-        ~InterpreterLock() { Interpreter::unlock(); }
-    private:
-        InterpreterLock(const InterpreterLock &);
-        InterpreterLock &operator =(const InterpreterLock &);
-    };
-
 } // namespace
 
 #endif // _KJS_INTERPRETER_H_
diff --git a/JavaScriptCore/kjs/protect.h b/JavaScriptCore/kjs/protect.h
index 30897d6..8cdab36 100644
--- a/JavaScriptCore/kjs/protect.h
+++ b/JavaScriptCore/kjs/protect.h
@@ -27,7 +27,7 @@
 #include "reference.h"
 #include "value.h"
 #include "protected_values.h"
-#include "interpreter.h"
+#include "JSLock.h"
 
 namespace KJS {
 
diff --git a/JavaScriptCore/kjs/protected_values.cpp b/JavaScriptCore/kjs/protected_values.cpp
index d5bc29a..30f2bb7 100644
--- a/JavaScriptCore/kjs/protected_values.cpp
+++ b/JavaScriptCore/kjs/protected_values.cpp
@@ -42,7 +42,7 @@
 int ProtectedValues::getProtectCount(ValueImp *k)
 {
     assert(k);
-    assert(InterpreterImp::lockCount() > 0);
+    assert(InterpreterLock::lockCount() > 0);
 
     if (!_table)
 	return 0;
@@ -71,7 +71,7 @@
 void ProtectedValues::increaseProtectCount(ValueImp *k)
 {
     assert(k);
-    assert(InterpreterImp::lockCount() > 0);
+    assert(InterpreterLock::lockCount() > 0);
 
     if (SimpleNumber::is(k))
       return;
@@ -121,7 +121,7 @@
 void ProtectedValues::decreaseProtectCount(ValueImp *k)
 {
     assert(k);
-    assert(InterpreterImp::lockCount() > 0);
+    assert(InterpreterLock::lockCount() > 0);
 
     if (SimpleNumber::is(k))
       return;
diff --git a/JavaScriptCore/kjs/testkjs.cpp b/JavaScriptCore/kjs/testkjs.cpp
index d749f61..9f36195 100644
--- a/JavaScriptCore/kjs/testkjs.cpp
+++ b/JavaScriptCore/kjs/testkjs.cpp
@@ -31,6 +31,7 @@
 #include "types.h"
 #include "interpreter.h"
 #include "collector.h"
+#include "JSLock.h"
 
 using namespace KJS;
 
@@ -62,9 +63,10 @@
     exit(0);
     return Undefined();
   case GC:
-    Interpreter::lock();
+  {
+    InterpreterLock lock;
     Collector::collect();
-    Interpreter::unlock();
+  }
     break;
   default:
     break;