Merge, simplify names, and police style.
authorBrendan Eich <brendan@mozilla.org>
Tue, 03 Jun 2008 16:21:31 -0700
changeset 17216 bee80ed3540fd4086a0b8d59691c87dfab774639
parent 17215 48ffc3e5564b38c2e67a12f5b9889acd91bee787 (current diff)
parent 17206 8b55e6657beace519053785a5598a3723e50c214 (diff)
child 17217 15fb0c1efd77e38d1499822ab500e8169ceaec01
push idunknown
push userunknown
push dateunknown
milestone1.9.1a1pre
Merge, simplify names, and police style.
js/src/jslock.h
--- a/js/src/jslock.cpp
+++ b/js/src/jslock.cpp
@@ -79,116 +79,16 @@ js_LockGlobal(void *id)
 
 static void
 js_UnlockGlobal(void *id)
 {
     uint32 i = GLOBAL_LOCK_INDEX(id);
     PR_Unlock(global_locks[i]);
 }
 
-/* Exclude Alpha NT. */
-#if defined(_WIN32) && defined(_M_IX86)
-#pragma warning( disable : 4035 )
-JS_BEGIN_EXTERN_C
-extern long __cdecl
-_InterlockedCompareExchange(long *volatile dest, long exchange, long comp);
-JS_END_EXTERN_C
-#pragma intrinsic(_InterlockedCompareExchange)
-
-static JS_INLINE int
-js_CompareAndSwapHelper(jsword *w, jsword ov, jsword nv)
-{
-    _InterlockedCompareExchange(w, nv, ov);
-    __asm {
-        sete al
-    }
-}
-
-static JS_INLINE int
-js_CompareAndSwap(jsword *w, jsword ov, jsword nv)
-{
-    return (js_CompareAndSwapHelper(w, ov, nv) & 1);
-}
-
-#elif defined(XP_MACOSX) || defined(DARWIN)
-
-#include <libkern/OSAtomic.h>
-
-static JS_INLINE int
-js_CompareAndSwap(jsword *w, jsword ov, jsword nv)
-{
-    /* Details on these functions available in the manpage for atomic */
-#if JS_BYTES_PER_WORD == 8 && JS_BYTES_PER_LONG != 8
-    return OSAtomicCompareAndSwap64Barrier(ov, nv, (int64_t*) w);
-#else
-    return OSAtomicCompareAndSwap32Barrier(ov, nv, (int32_t*) w);
-#endif
-}
-
-#elif defined(__GNUC__) && defined(__i386__)
-
-/* Note: This fails on 386 cpus, cmpxchgl is a >= 486 instruction */
-static JS_INLINE int
-js_CompareAndSwap(jsword *w, jsword ov, jsword nv)
-{
-    unsigned int res;
-
-    __asm__ __volatile__ (
-                          "lock\n"
-                          "cmpxchgl %2, (%1)\n"
-                          "sete %%al\n"
-                          "andl $1, %%eax\n"
-                          : "=a" (res)
-                          : "r" (w), "r" (nv), "a" (ov)
-                          : "cc", "memory");
-    return (int)res;
-}
-
-#elif defined(SOLARIS) && defined(sparc) && defined(ULTRA_SPARC)
-
-static JS_INLINE int
-js_CompareAndSwap(jsword *w, jsword ov, jsword nv)
-{
-#if defined(__GNUC__)
-    unsigned int res;
-    JS_ASSERT(ov != nv);
-    asm volatile ("\
-stbar\n\
-cas [%1],%2,%3\n\
-cmp %2,%3\n\
-be,a 1f\n\
-mov 1,%0\n\
-mov 0,%0\n\
-1:"
-                  : "=r" (res)
-                  : "r" (w), "r" (ov), "r" (nv));
-    return (int)res;
-#else /* !__GNUC__ */
-    extern int compare_and_swap(jsword*, jsword, jsword);
-    JS_ASSERT(ov != nv);
-    return compare_and_swap(w, ov, nv);
-#endif
-}
-
-#elif defined(AIX)
-
-#include <sys/atomic_op.h>
-
-static JS_INLINE int
-js_CompareAndSwap(jsword *w, jsword ov, jsword nv)
-{
-    return !_check_lock((atomic_p)w, ov, nv);
-}
-
-#else
-
-#error "Define NSPR_LOCK if your platform lacks a compare-and-swap instruction."
-
-#endif /* arch-tests */
-
 #endif /* !NSPR_LOCK */
 
 void
 js_InitLock(JSThinLock *tl)
 {
 #ifdef NSPR_LOCK
     tl->owner = 0;
     tl->fat = (JSFatLock*)JS_NEW_LOCK();
--- a/js/src/jslock.h
+++ b/js/src/jslock.h
@@ -34,26 +34,29 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 #ifndef jslock_h__
 #define jslock_h__
 
+#include "jspubtd.h"    /* for jsword, JSRuntime, etc. */
+
 #ifdef JS_THREADSAFE
 
 #include "jstypes.h"
 #include "pratom.h"
 #include "prlock.h"
 #include "prcvar.h"
 #include "prthread.h"
 
+#ifdef JS_DEBUG_TITLE_LOCKS
 #include "jsprvtd.h"    /* for JSScope, etc. */
-#include "jspubtd.h"    /* for JSRuntime, etc. */
+#endif
 
 JS_BEGIN_EXTERN_C
 
 #define Thin_GetWait(W) ((jsword)(W) & 0x1)
 #define Thin_SetWait(W) ((jsword)(W) | 0x1)
 #define Thin_RemoveWait(W) ((jsword)(W) & ~0x1)
 
 typedef struct JSFatLock JSFatLock;
@@ -89,17 +92,17 @@ struct JSTitle {
     union {                             /* union lockful and lock-free state: */
         jsrefcount  count;              /* lock entry count for reentrancy */
         JSTitle     *link;              /* next link in rt->titleSharingTodo */
     } u;
 #ifdef JS_DEBUG_TITLE_LOCKS
     const char      *file[4];           /* file where lock was (re-)taken */
     unsigned int    line[4];            /* line where lock was (re-)taken */
 #endif
-};    
+};
 
 /*
  * Title structures must be immediately preceded by JSObjectMap structures for
  * maps that use titles for threadsafety.  This is enforced by assertion in
  * jsscope.h; see bug 408416 for future remedies to this somewhat fragile
  * architecture.
  */
 
@@ -110,16 +113,120 @@ struct JSTitle {
  * Atomic increment and decrement for a reference counter, given jsrefcount *p.
  * NB: jsrefcount is int32, aka PRInt32, so that pratom.h functions work.
  */
 #define JS_ATOMIC_INCREMENT(p)      PR_AtomicIncrement((PRInt32 *)(p))
 #define JS_ATOMIC_DECREMENT(p)      PR_AtomicDecrement((PRInt32 *)(p))
 #define JS_ATOMIC_ADD(p,v)          PR_AtomicAdd((PRInt32 *)(p), (PRInt32)(v))
 #define JS_ATOMIC_SET(p,v)          PR_AtomicSet((PRInt32 *)(p), (PRInt32)(v))
 
+/*
+ * Compare-And-Swap (CAS) inline function, ifdef'ed for different platforms.
+ */
+
+/* Exclude Alpha NT. */
+#if defined(_WIN32) && defined(_M_IX86)
+#pragma warning( disable : 4035 )
+JS_BEGIN_EXTERN_C
+extern long __cdecl
+_InterlockedCompareExchange(long *volatile dest, long exchange, long comp);
+JS_END_EXTERN_C
+#pragma intrinsic(_InterlockedCompareExchange)
+
+static JS_INLINE int
+js_CompareAndSwapHelper(jsword *w, jsword ov, jsword nv)
+{
+    _InterlockedCompareExchange(w, nv, ov);
+    __asm {
+        sete al
+    }
+}
+
+static JS_INLINE int
+js_CompareAndSwap(jsword *w, jsword ov, jsword nv)
+{
+    return (js_CompareAndSwapHelper(w, ov, nv) & 1);
+}
+
+#elif defined(XP_MACOSX) || defined(DARWIN)
+
+#include <libkern/OSAtomic.h>
+
+static JS_INLINE int
+js_CompareAndSwap(jsword *w, jsword ov, jsword nv)
+{
+    /* Details on these functions available in the manpage for atomic */
+#if JS_BYTES_PER_WORD == 8 && JS_BYTES_PER_LONG != 8
+    return OSAtomicCompareAndSwap64Barrier(ov, nv, (int64_t*) w);
+#else
+    return OSAtomicCompareAndSwap32Barrier(ov, nv, (int32_t*) w);
+#endif
+}
+
+#elif defined(__GNUC__) && defined(__i386__)
+
+/* Note: This fails on 386 cpus, cmpxchgl is a >= 486 instruction */
+static JS_INLINE int
+js_CompareAndSwap(jsword *w, jsword ov, jsword nv)
+{
+    unsigned int res;
+
+    __asm__ __volatile__ (
+                          "lock\n"
+                          "cmpxchgl %2, (%1)\n"
+                          "sete %%al\n"
+                          "andl $1, %%eax\n"
+                          : "=a" (res)
+                          : "r" (w), "r" (nv), "a" (ov)
+                          : "cc", "memory");
+    return (int)res;
+}
+
+#elif defined(SOLARIS) && defined(sparc) && defined(ULTRA_SPARC)
+
+static JS_INLINE int
+js_CompareAndSwap(jsword *w, jsword ov, jsword nv)
+{
+#if defined(__GNUC__)
+    unsigned int res;
+    JS_ASSERT(ov != nv);
+    asm volatile ("\
+stbar\n\
+cas [%1],%2,%3\n\
+cmp %2,%3\n\
+be,a 1f\n\
+mov 1,%0\n\
+mov 0,%0\n\
+1:"
+                  : "=r" (res)
+                  : "r" (w), "r" (ov), "r" (nv));
+    return (int)res;
+#else /* !__GNUC__ */
+    extern int compare_and_swap(jsword*, jsword, jsword);
+    JS_ASSERT(ov != nv);
+    return compare_and_swap(w, ov, nv);
+#endif
+}
+
+#elif defined(AIX)
+
+#include <sys/atomic_op.h>
+
+static JS_INLINE int
+js_CompareAndSwap(jsword *w, jsword ov, jsword nv)
+{
+    return !_check_lock((atomic_p)w, ov, nv);
+}
+
+#else
+
+#error "Your platform lacks a compare-and-swap instruction."
+
+#endif /* arch-tests */
+
 #define js_CurrentThreadId()        (jsword)PR_GetCurrentThread()
 #define JS_NEW_LOCK()               PR_NewLock()
 #define JS_DESTROY_LOCK(l)          PR_DestroyLock(l)
 #define JS_ACQUIRE_LOCK(l)          PR_Lock(l)
 #define JS_RELEASE_LOCK(l)          PR_Unlock(l)
 #define JS_LOCK0(P,M)               js_Lock(P,M)
 #define JS_UNLOCK0(P,M)             js_Unlock(P,M)
 
@@ -244,26 +351,37 @@ extern void js_Lock(JSThinLock *tl, jswo
 extern void js_Unlock(JSThinLock *tl, jsword me);
 
 #endif /* arch-tests */
 
 #else  /* !JS_THREADSAFE */
 
 JS_BEGIN_EXTERN_C
 
-static inline int32 js_NotThreadsafeAtomicAdd(int32* p, int32 v) {
+static inline int32
+js_AtomicAdd(int32* p, int32 v)
+{
     int32 r = *p;
     *p = v;
     return r;
 }
 
 #define JS_ATOMIC_INCREMENT(p)      (++*(p))
 #define JS_ATOMIC_DECREMENT(p)      (--*(p))
 #define JS_ATOMIC_ADD(p,v)          (*(p) += (v))
-#define JS_ATOMIC_SET(p,v)          (js_NotThreadsafeAtomicAdd((int32*)p, (int32)v))
+#define JS_ATOMIC_SET(p,v)          (js_AtomicAdd((int32*)p, (int32)v))
+
+static inline int
+js_CompareAndSwap(jsword *w, jsword ov, jsword nv)
+{
+    if (*w != ov)
+        return 0;
+    *w = nv;
+    return 1;
+}
 
 #define JS_CurrentThreadId() 0
 #define JS_NEW_LOCK()               NULL
 #define JS_DESTROY_LOCK(l)          ((void)0)
 #define JS_ACQUIRE_LOCK(l)          ((void)0)
 #define JS_RELEASE_LOCK(l)          ((void)0)
 #define JS_LOCK0(P,M)               ((void)0)
 #define JS_UNLOCK0(P,M)             ((void)0)
@@ -303,17 +421,17 @@ static inline int32 js_NotThreadsafeAtom
 #define JS_AWAIT_GC_DONE(rt)        JS_WAIT_CONDVAR((rt)->gcDone, JS_NO_TIMEOUT)
 #define JS_NOTIFY_GC_DONE(rt)       JS_NOTIFY_ALL_CONDVAR((rt)->gcDone)
 #define JS_AWAIT_REQUEST_DONE(rt)   JS_WAIT_CONDVAR((rt)->requestDone,        \
                                                     JS_NO_TIMEOUT)
 #define JS_NOTIFY_REQUEST_DONE(rt)  JS_NOTIFY_CONDVAR((rt)->requestDone)
 
 #define JS_LOCK(P,CX)               JS_LOCK0(P, CX_THINLOCK_ID(CX))
 #define JS_UNLOCK(P,CX)             JS_UNLOCK0(P, CX_THINLOCK_ID(CX))
- 
+
 #ifndef SET_OBJ_INFO
 #define SET_OBJ_INFO(obj,f,l)       ((void)0)
 #endif
 #ifndef SET_TITLE_INFO
 #define SET_TITLE_INFO(title,f,l)   ((void)0)
 #endif
 
 JS_END_EXTERN_C