2013-04-16 13:47:10 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 04:12:37 -07:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* JavaScript Debugging support - Locking and threading support
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2013-06-17 14:28:11 -07:00
|
|
|
* ifdef JSD_USE_NSPR_LOCKS then you must build and run against NSPR2.
|
2007-03-22 10:30:00 -07:00
|
|
|
* Otherwise, there are stubs that can be filled in with your own locking
|
|
|
|
* code. Also, note that these stubs include a jsd_CurrentThread()
|
|
|
|
* implementation that only works on Win32 - this is needed for the inprocess
|
|
|
|
* Java-based debugger.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "jsd.h"
|
|
|
|
|
2012-09-04 09:02:03 -07:00
|
|
|
#include "js/Utility.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef JSD_THREADSAFE
|
|
|
|
|
|
|
|
#ifdef JSD_USE_NSPR_LOCKS
|
|
|
|
|
|
|
|
#include "prlock.h"
|
|
|
|
#include "prthread.h"
|
|
|
|
|
|
|
|
#ifdef JSD_ATTACH_THREAD_HACK
|
|
|
|
#include "pprthred.h" /* need this as long as JS_AttachThread is needed */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct JSDStaticLock
|
|
|
|
{
|
|
|
|
void* owner;
|
|
|
|
PRLock* lock;
|
|
|
|
int count;
|
|
|
|
#ifdef DEBUG
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
uint16_t sig;
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This exists to wrap non-NSPR theads (e.g. Java threads) in NSPR wrappers.
|
|
|
|
* XXX We ignore the memory leak issue.
|
|
|
|
* It is claimed that future versions of NSPR will automatically wrap on
|
|
|
|
* the call to PR_GetCurrentThread.
|
|
|
|
*
|
|
|
|
* XXX We ignore the memory leak issue - i.e. we never call PR_DetachThread.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#undef _CURRENT_THREAD
|
|
|
|
#ifdef JSD_ATTACH_THREAD_HACK
|
|
|
|
#define _CURRENT_THREAD(out) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
out = (void*) PR_GetCurrentThread(); \
|
|
|
|
if(!out) \
|
2013-09-19 12:26:36 -07:00
|
|
|
out = (void*) JS_AttachThread(PR_USER_THREAD, PR_PRIORITY_NORMAL, \
|
|
|
|
nullptr); \
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ASSERT(out); \
|
|
|
|
JS_END_MACRO
|
|
|
|
#else
|
|
|
|
#define _CURRENT_THREAD(out) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
out = (void*) PR_GetCurrentThread(); \
|
|
|
|
JS_ASSERT(out); \
|
|
|
|
JS_END_MACRO
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define JSD_LOCK_SIG 0x10CC10CC
|
|
|
|
void ASSERT_VALID_LOCK(JSDStaticLock* lock)
|
|
|
|
{
|
|
|
|
JS_ASSERT(lock);
|
|
|
|
JS_ASSERT(lock->lock);
|
|
|
|
JS_ASSERT(lock->count >= 0);
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
JS_ASSERT(lock->sig == (uint16_t) JSD_LOCK_SIG);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define ASSERT_VALID_LOCK(x) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
2012-09-04 09:02:03 -07:00
|
|
|
JSDStaticLock*
|
2007-03-22 10:30:00 -07:00
|
|
|
jsd_CreateLock()
|
|
|
|
{
|
|
|
|
JSDStaticLock* lock;
|
|
|
|
|
2013-09-25 12:18:43 -07:00
|
|
|
if( ! (lock = js_pod_calloc<JSDStaticLock>()) ||
|
2007-03-22 10:30:00 -07:00
|
|
|
! (lock->lock = PR_NewLock()) )
|
|
|
|
{
|
|
|
|
if(lock)
|
|
|
|
{
|
|
|
|
free(lock);
|
2013-09-19 12:26:36 -07:00
|
|
|
lock = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
if(lock) lock->sig = (uint16_t) JSD_LOCK_SIG;
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
jsd_Lock(JSDStaticLock* lock)
|
|
|
|
{
|
|
|
|
void* me;
|
|
|
|
ASSERT_VALID_LOCK(lock);
|
2008-02-26 07:04:13 -08:00
|
|
|
_CURRENT_THREAD(me);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if(lock->owner == me)
|
2008-02-26 07:04:13 -08:00
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
lock->count++;
|
2008-02-26 07:04:13 -08:00
|
|
|
JS_ASSERT(lock->count > 1);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
PR_Lock(lock->lock); /* this can block... */
|
|
|
|
JS_ASSERT(lock->owner == 0);
|
2008-02-26 07:04:13 -08:00
|
|
|
JS_ASSERT(lock->count == 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
lock->count = 1;
|
|
|
|
lock->owner = me;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
jsd_Unlock(JSDStaticLock* lock)
|
|
|
|
{
|
|
|
|
void* me;
|
|
|
|
ASSERT_VALID_LOCK(lock);
|
|
|
|
_CURRENT_THREAD(me);
|
|
|
|
|
2008-02-26 07:04:13 -08:00
|
|
|
/* it's an error to unlock a lock you don't own */
|
|
|
|
JS_ASSERT(lock->owner == me);
|
2007-03-22 10:30:00 -07:00
|
|
|
if(lock->owner != me)
|
|
|
|
return;
|
2008-02-26 07:07:05 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if(--lock->count == 0)
|
|
|
|
{
|
2013-09-19 12:26:36 -07:00
|
|
|
lock->owner = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_Unlock(lock->lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
jsd_IsLocked(JSDStaticLock* lock)
|
|
|
|
{
|
|
|
|
void* me;
|
|
|
|
ASSERT_VALID_LOCK(lock);
|
|
|
|
_CURRENT_THREAD(me);
|
2008-02-26 07:04:13 -08:00
|
|
|
if (lock->owner != me)
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2008-02-26 07:04:13 -08:00
|
|
|
JS_ASSERT(lock->count > 0);
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
void*
|
|
|
|
jsd_CurrentThread()
|
|
|
|
{
|
|
|
|
void* me;
|
|
|
|
_CURRENT_THREAD(me);
|
|
|
|
return me;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#else /* ! JSD_USE_NSPR_LOCKS */
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#pragma message("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
|
|
|
|
#pragma message("!! you are compiling the stubbed version of jsd_lock.c !!")
|
|
|
|
#pragma message("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: 'Real' versions of these locks must be reentrant in the sense that
|
|
|
|
* they support nested calls to lock and unlock.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void*
|
|
|
|
jsd_CreateLock()
|
|
|
|
{
|
|
|
|
return (void*)1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
jsd_Lock(void* lock)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
jsd_Unlock(void* lock)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
jsd_IsLocked(void* lock)
|
|
|
|
{
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This Windows only thread id code is here to allow the Java-based
|
|
|
|
* JSDebugger to work with the single threaded js.c shell (even without
|
|
|
|
* real locking and threading support).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
/* bogus (but good enough) declaration*/
|
|
|
|
extern void* __stdcall GetCurrentThreadId(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void*
|
|
|
|
jsd_CurrentThread()
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
return GetCurrentThreadId();
|
|
|
|
#else
|
|
|
|
return (void*)1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* JSD_USE_NSPR_LOCKS */
|
|
|
|
|
|
|
|
#endif /* JSD_THREADSAFE */
|