2013-04-16 20:47:10 +00: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 11:12:37 +00: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/. */
|
1998-03-28 02:44:41 +00:00
|
|
|
|
|
|
|
/*
|
1998-11-05 08:57:24 +00:00
|
|
|
* JavaScript Debugging support - Locking and threading support
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2013-06-17 21:28:11 +00:00
|
|
|
* ifdef JSD_USE_NSPR_LOCKS then you must build and run against NSPR2.
|
1998-11-05 08:57:24 +00: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 16:02:03 +00:00
|
|
|
#include "js/Utility.h"
|
|
|
|
|
1998-11-05 08:57:24 +00: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
|
|
|
|
|
1999-09-06 00:19:14 +00:00
|
|
|
struct JSDStaticLock
|
1998-11-05 08:57:24 +00:00
|
|
|
{
|
|
|
|
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-09 03:54:10 +00:00
|
|
|
uint16_t sig;
|
1998-11-05 08:57:24 +00:00
|
|
|
#endif
|
1999-09-06 00:19:14 +00:00
|
|
|
};
|
1998-11-05 08:57:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 19:26:36 +00:00
|
|
|
out = (void*) JS_AttachThread(PR_USER_THREAD, PR_PRIORITY_NORMAL, \
|
|
|
|
nullptr); \
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(out); \
|
1998-11-05 08:57:24 +00:00
|
|
|
JS_END_MACRO
|
|
|
|
#else
|
|
|
|
#define _CURRENT_THREAD(out) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
out = (void*) PR_GetCurrentThread(); \
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(out); \
|
1998-11-05 08:57:24 +00:00
|
|
|
JS_END_MACRO
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define JSD_LOCK_SIG 0x10CC10CC
|
|
|
|
void ASSERT_VALID_LOCK(JSDStaticLock* lock)
|
|
|
|
{
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(lock);
|
|
|
|
MOZ_ASSERT(lock->lock);
|
|
|
|
MOZ_ASSERT(lock->count >= 0);
|
|
|
|
MOZ_ASSERT(lock->sig == (uint16_t) JSD_LOCK_SIG);
|
1998-11-05 08:57:24 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define ASSERT_VALID_LOCK(x) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
2012-09-04 16:02:03 +00:00
|
|
|
JSDStaticLock*
|
1998-11-05 08:57:24 +00:00
|
|
|
jsd_CreateLock()
|
|
|
|
{
|
|
|
|
JSDStaticLock* lock;
|
|
|
|
|
2013-09-25 19:18:43 +00:00
|
|
|
if( ! (lock = js_pod_calloc<JSDStaticLock>()) ||
|
1998-11-05 08:57:24 +00:00
|
|
|
! (lock->lock = PR_NewLock()) )
|
|
|
|
{
|
|
|
|
if(lock)
|
|
|
|
{
|
|
|
|
free(lock);
|
2013-09-19 19:26:36 +00:00
|
|
|
lock = nullptr;
|
1998-11-05 08:57:24 +00: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-09 03:54:10 +00:00
|
|
|
if(lock) lock->sig = (uint16_t) JSD_LOCK_SIG;
|
1998-11-05 08:57:24 +00:00
|
|
|
#endif
|
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
jsd_Lock(JSDStaticLock* lock)
|
|
|
|
{
|
|
|
|
void* me;
|
|
|
|
ASSERT_VALID_LOCK(lock);
|
2008-02-26 15:04:13 +00:00
|
|
|
_CURRENT_THREAD(me);
|
1998-11-05 08:57:24 +00:00
|
|
|
|
|
|
|
if(lock->owner == me)
|
2008-02-26 15:04:13 +00:00
|
|
|
{
|
1998-11-05 08:57:24 +00:00
|
|
|
lock->count++;
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(lock->count > 1);
|
2008-02-26 15:04:13 +00:00
|
|
|
}
|
1998-11-05 08:57:24 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
PR_Lock(lock->lock); /* this can block... */
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(lock->owner == 0);
|
|
|
|
MOZ_ASSERT(lock->count == 0);
|
1998-11-05 08:57:24 +00:00
|
|
|
lock->count = 1;
|
|
|
|
lock->owner = me;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
jsd_Unlock(JSDStaticLock* lock)
|
|
|
|
{
|
|
|
|
void* me;
|
|
|
|
ASSERT_VALID_LOCK(lock);
|
|
|
|
_CURRENT_THREAD(me);
|
|
|
|
|
2008-02-26 15:04:13 +00:00
|
|
|
/* it's an error to unlock a lock you don't own */
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(lock->owner == me);
|
1998-11-05 08:57:24 +00:00
|
|
|
if(lock->owner != me)
|
|
|
|
return;
|
2008-02-26 15:07:05 +00:00
|
|
|
|
1998-11-05 08:57:24 +00:00
|
|
|
if(--lock->count == 0)
|
|
|
|
{
|
2013-09-19 19:26:36 +00:00
|
|
|
lock->owner = nullptr;
|
1998-11-05 08:57:24 +00:00
|
|
|
PR_Unlock(lock->lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2013-08-08 22:53:04 +00:00
|
|
|
bool
|
1998-11-05 08:57:24 +00:00
|
|
|
jsd_IsLocked(JSDStaticLock* lock)
|
|
|
|
{
|
|
|
|
void* me;
|
|
|
|
ASSERT_VALID_LOCK(lock);
|
|
|
|
_CURRENT_THREAD(me);
|
2008-02-26 15:04:13 +00:00
|
|
|
if (lock->owner != me)
|
2013-08-07 06:59:54 +00:00
|
|
|
return false;
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(lock->count > 0);
|
2013-08-07 06:59:54 +00:00
|
|
|
return true;
|
1998-11-05 08:57:24 +00: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 22:53:04 +00:00
|
|
|
bool
|
1998-11-05 08:57:24 +00:00
|
|
|
jsd_IsLocked(void* lock)
|
|
|
|
{
|
2013-08-07 06:59:54 +00:00
|
|
|
return true;
|
1998-11-05 08:57:24 +00: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 */
|