1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * JavaScript Debugging support - Locking and threading support
43 * ifdef JSD_USE_NSPR_LOCKS then you musat build and run against NSPR2.
44 * Otherwise, there are stubs that can be filled in with your own locking
45 * code. Also, note that these stubs include a jsd_CurrentThread()
46 * implementation that only works on Win32 - this is needed for the inprocess
47 * Java-based debugger.
54 #ifdef JSD_USE_NSPR_LOCKS
59 #ifdef JSD_ATTACH_THREAD_HACK
60 #include "pprthred.h" /* need this as long as JS_AttachThread is needed */
74 * This exists to wrap non-NSPR theads (e.g. Java threads) in NSPR wrappers.
75 * XXX We ignore the memory leak issue.
76 * It is claimed that future versions of NSPR will automatically wrap on
77 * the call to PR_GetCurrentThread.
79 * XXX We ignore the memory leak issue - i.e. we never call PR_DetachThread.
82 #undef _CURRENT_THREAD
83 #ifdef JSD_ATTACH_THREAD_HACK
84 #define _CURRENT_THREAD(out) \
86 out = (void*) PR_GetCurrentThread(); \
88 out = (void*) JS_AttachThread(PR_USER_THREAD,PR_PRIORITY_NORMAL,NULL);\
92 #define _CURRENT_THREAD(out) \
94 out = (void*) PR_GetCurrentThread(); \
100 #define JSD_LOCK_SIG 0x10CC10CC
101 void ASSERT_VALID_LOCK(JSDStaticLock
* lock
)
104 JS_ASSERT(lock
->lock
);
105 JS_ASSERT(lock
->count
>= 0);
106 JS_ASSERT(lock
->sig
== (uint16
) JSD_LOCK_SIG
);
109 #define ASSERT_VALID_LOCK(x) ((void)0)
117 if( ! (lock
= calloc(1, sizeof(JSDStaticLock
))) ||
118 ! (lock
->lock
= PR_NewLock()) )
127 if(lock
) lock
->sig
= (uint16
) JSD_LOCK_SIG
;
133 jsd_Lock(JSDStaticLock
* lock
)
136 ASSERT_VALID_LOCK(lock
);
139 if(lock
->owner
== me
)
142 JS_ASSERT(lock
->count
> 1);
146 PR_Lock(lock
->lock
); /* this can block... */
147 JS_ASSERT(lock
->owner
== 0);
148 JS_ASSERT(lock
->count
== 0);
155 jsd_Unlock(JSDStaticLock
* lock
)
158 ASSERT_VALID_LOCK(lock
);
161 /* it's an error to unlock a lock you don't own */
162 JS_ASSERT(lock
->owner
== me
);
163 if(lock
->owner
!= me
)
166 if(--lock
->count
== 0)
169 PR_Unlock(lock
->lock
);
175 jsd_IsLocked(JSDStaticLock
* lock
)
178 ASSERT_VALID_LOCK(lock
);
180 if (lock
->owner
!= me
)
182 JS_ASSERT(lock
->count
> 0);
196 #else /* ! JSD_USE_NSPR_LOCKS */
199 #pragma message("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
200 #pragma message("!! you are compiling the stubbed version of jsd_lock.c !!")
201 #pragma message("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
205 * NOTE: 'Real' versions of these locks must be reentrant in the sense that
206 * they support nested calls to lock and unlock.
221 jsd_Unlock(void* lock
)
227 jsd_IsLocked(void* lock
)
234 * This Windows only thread id code is here to allow the Java-based
235 * JSDebugger to work with the single threaded js.c shell (even without
236 * real locking and threading support).
240 /* bogus (but good enough) declaration*/
241 extern void* __stdcall
GetCurrentThreadId(void);
248 return GetCurrentThreadId();
254 #endif /* JSD_USE_NSPR_LOCKS */
256 #endif /* JSD_THREADSAFE */