michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * JavaScript Debugging support - Locking and threading support michael@0: */ michael@0: michael@0: /* michael@0: * ifdef JSD_USE_NSPR_LOCKS then you must build and run against NSPR2. michael@0: * Otherwise, there are stubs that can be filled in with your own locking michael@0: * code. Also, note that these stubs include a jsd_CurrentThread() michael@0: * implementation that only works on Win32 - this is needed for the inprocess michael@0: * Java-based debugger. michael@0: */ michael@0: michael@0: #include "jsd.h" michael@0: michael@0: #include "js/Utility.h" michael@0: michael@0: #ifdef JSD_THREADSAFE michael@0: michael@0: #ifdef JSD_USE_NSPR_LOCKS michael@0: michael@0: #include "prlock.h" michael@0: #include "prthread.h" michael@0: michael@0: #ifdef JSD_ATTACH_THREAD_HACK michael@0: #include "pprthred.h" /* need this as long as JS_AttachThread is needed */ michael@0: #endif michael@0: michael@0: struct JSDStaticLock michael@0: { michael@0: void* owner; michael@0: PRLock* lock; michael@0: int count; michael@0: #ifdef DEBUG michael@0: uint16_t sig; michael@0: #endif michael@0: }; michael@0: michael@0: /* michael@0: * This exists to wrap non-NSPR theads (e.g. Java threads) in NSPR wrappers. michael@0: * XXX We ignore the memory leak issue. michael@0: * It is claimed that future versions of NSPR will automatically wrap on michael@0: * the call to PR_GetCurrentThread. michael@0: * michael@0: * XXX We ignore the memory leak issue - i.e. we never call PR_DetachThread. michael@0: * michael@0: */ michael@0: #undef _CURRENT_THREAD michael@0: #ifdef JSD_ATTACH_THREAD_HACK michael@0: #define _CURRENT_THREAD(out) \ michael@0: JS_BEGIN_MACRO \ michael@0: out = (void*) PR_GetCurrentThread(); \ michael@0: if(!out) \ michael@0: out = (void*) JS_AttachThread(PR_USER_THREAD, PR_PRIORITY_NORMAL, \ michael@0: nullptr); \ michael@0: MOZ_ASSERT(out); \ michael@0: JS_END_MACRO michael@0: #else michael@0: #define _CURRENT_THREAD(out) \ michael@0: JS_BEGIN_MACRO \ michael@0: out = (void*) PR_GetCurrentThread(); \ michael@0: MOZ_ASSERT(out); \ michael@0: JS_END_MACRO michael@0: #endif michael@0: michael@0: #ifdef DEBUG michael@0: #define JSD_LOCK_SIG 0x10CC10CC michael@0: void ASSERT_VALID_LOCK(JSDStaticLock* lock) michael@0: { michael@0: MOZ_ASSERT(lock); michael@0: MOZ_ASSERT(lock->lock); michael@0: MOZ_ASSERT(lock->count >= 0); michael@0: MOZ_ASSERT(lock->sig == (uint16_t) JSD_LOCK_SIG); michael@0: } michael@0: #else michael@0: #define ASSERT_VALID_LOCK(x) ((void)0) michael@0: #endif michael@0: michael@0: JSDStaticLock* michael@0: jsd_CreateLock() michael@0: { michael@0: JSDStaticLock* lock; michael@0: michael@0: if( ! (lock = js_pod_calloc()) || michael@0: ! (lock->lock = PR_NewLock()) ) michael@0: { michael@0: if(lock) michael@0: { michael@0: free(lock); michael@0: lock = nullptr; michael@0: } michael@0: } michael@0: #ifdef DEBUG michael@0: if(lock) lock->sig = (uint16_t) JSD_LOCK_SIG; michael@0: #endif michael@0: return lock; michael@0: } michael@0: michael@0: void michael@0: jsd_Lock(JSDStaticLock* lock) michael@0: { michael@0: void* me; michael@0: ASSERT_VALID_LOCK(lock); michael@0: _CURRENT_THREAD(me); michael@0: michael@0: if(lock->owner == me) michael@0: { michael@0: lock->count++; michael@0: MOZ_ASSERT(lock->count > 1); michael@0: } michael@0: else michael@0: { michael@0: PR_Lock(lock->lock); /* this can block... */ michael@0: MOZ_ASSERT(lock->owner == 0); michael@0: MOZ_ASSERT(lock->count == 0); michael@0: lock->count = 1; michael@0: lock->owner = me; michael@0: } michael@0: } michael@0: michael@0: void michael@0: jsd_Unlock(JSDStaticLock* lock) michael@0: { michael@0: void* me; michael@0: ASSERT_VALID_LOCK(lock); michael@0: _CURRENT_THREAD(me); michael@0: michael@0: /* it's an error to unlock a lock you don't own */ michael@0: MOZ_ASSERT(lock->owner == me); michael@0: if(lock->owner != me) michael@0: return; michael@0: michael@0: if(--lock->count == 0) michael@0: { michael@0: lock->owner = nullptr; michael@0: PR_Unlock(lock->lock); michael@0: } michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: bool michael@0: jsd_IsLocked(JSDStaticLock* lock) michael@0: { michael@0: void* me; michael@0: ASSERT_VALID_LOCK(lock); michael@0: _CURRENT_THREAD(me); michael@0: if (lock->owner != me) michael@0: return false; michael@0: MOZ_ASSERT(lock->count > 0); michael@0: return true; michael@0: } michael@0: #endif /* DEBUG */ michael@0: michael@0: void* michael@0: jsd_CurrentThread() michael@0: { michael@0: void* me; michael@0: _CURRENT_THREAD(me); michael@0: return me; michael@0: } michael@0: michael@0: michael@0: #else /* ! JSD_USE_NSPR_LOCKS */ michael@0: michael@0: #ifdef WIN32 michael@0: #pragma message("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") michael@0: #pragma message("!! you are compiling the stubbed version of jsd_lock.c !!") michael@0: #pragma message("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") michael@0: #endif michael@0: michael@0: /* michael@0: * NOTE: 'Real' versions of these locks must be reentrant in the sense that michael@0: * they support nested calls to lock and unlock. michael@0: */ michael@0: michael@0: void* michael@0: jsd_CreateLock() michael@0: { michael@0: return (void*)1; michael@0: } michael@0: michael@0: void michael@0: jsd_Lock(void* lock) michael@0: { michael@0: } michael@0: michael@0: void michael@0: jsd_Unlock(void* lock) michael@0: { michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: bool michael@0: jsd_IsLocked(void* lock) michael@0: { michael@0: return true; michael@0: } michael@0: #endif /* DEBUG */ michael@0: michael@0: /* michael@0: * This Windows only thread id code is here to allow the Java-based michael@0: * JSDebugger to work with the single threaded js.c shell (even without michael@0: * real locking and threading support). michael@0: */ michael@0: michael@0: #ifdef WIN32 michael@0: /* bogus (but good enough) declaration*/ michael@0: extern void* __stdcall GetCurrentThreadId(void); michael@0: #endif michael@0: michael@0: void* michael@0: jsd_CurrentThread() michael@0: { michael@0: #ifdef WIN32 michael@0: return GetCurrentThreadId(); michael@0: #else michael@0: return (void*)1; michael@0: #endif michael@0: } michael@0: michael@0: #endif /* JSD_USE_NSPR_LOCKS */ michael@0: michael@0: #endif /* JSD_THREADSAFE */