Bug 465497, Wait for AUS to notice new snippets before testing them, r=bhearsum
[mozilla-1.9.git] / js / jsd / jsd_lock.c
blobff5e6950f935d1cbfbb7e27af7e46639e4ea6fee
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
13 * License.
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.
22 * Contributor(s):
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
42 /*
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.
48 */
50 #include "jsd.h"
52 #ifdef JSD_THREADSAFE
54 #ifdef JSD_USE_NSPR_LOCKS
56 #include "prlock.h"
57 #include "prthread.h"
59 #ifdef JSD_ATTACH_THREAD_HACK
60 #include "pprthred.h" /* need this as long as JS_AttachThread is needed */
61 #endif
63 struct JSDStaticLock
65 void* owner;
66 PRLock* lock;
67 int count;
68 #ifdef DEBUG
69 uint16 sig;
70 #endif
73 /*
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) \
85 JS_BEGIN_MACRO \
86 out = (void*) PR_GetCurrentThread(); \
87 if(!out) \
88 out = (void*) JS_AttachThread(PR_USER_THREAD,PR_PRIORITY_NORMAL,NULL);\
89 JS_ASSERT(out); \
90 JS_END_MACRO
91 #else
92 #define _CURRENT_THREAD(out) \
93 JS_BEGIN_MACRO \
94 out = (void*) PR_GetCurrentThread(); \
95 JS_ASSERT(out); \
96 JS_END_MACRO
97 #endif
99 #ifdef DEBUG
100 #define JSD_LOCK_SIG 0x10CC10CC
101 void ASSERT_VALID_LOCK(JSDStaticLock* lock)
103 JS_ASSERT(lock);
104 JS_ASSERT(lock->lock);
105 JS_ASSERT(lock->count >= 0);
106 JS_ASSERT(lock->sig == (uint16) JSD_LOCK_SIG);
108 #else
109 #define ASSERT_VALID_LOCK(x) ((void)0)
110 #endif
112 void*
113 jsd_CreateLock()
115 JSDStaticLock* lock;
117 if( ! (lock = calloc(1, sizeof(JSDStaticLock))) ||
118 ! (lock->lock = PR_NewLock()) )
120 if(lock)
122 free(lock);
123 lock = NULL;
126 #ifdef DEBUG
127 if(lock) lock->sig = (uint16) JSD_LOCK_SIG;
128 #endif
129 return lock;
132 void
133 jsd_Lock(JSDStaticLock* lock)
135 void* me;
136 ASSERT_VALID_LOCK(lock);
137 _CURRENT_THREAD(me);
139 if(lock->owner == me)
141 lock->count++;
142 JS_ASSERT(lock->count > 1);
144 else
146 PR_Lock(lock->lock); /* this can block... */
147 JS_ASSERT(lock->owner == 0);
148 JS_ASSERT(lock->count == 0);
149 lock->count = 1;
150 lock->owner = me;
154 void
155 jsd_Unlock(JSDStaticLock* lock)
157 void* me;
158 ASSERT_VALID_LOCK(lock);
159 _CURRENT_THREAD(me);
161 /* it's an error to unlock a lock you don't own */
162 JS_ASSERT(lock->owner == me);
163 if(lock->owner != me)
164 return;
166 if(--lock->count == 0)
168 lock->owner = NULL;
169 PR_Unlock(lock->lock);
173 #ifdef DEBUG
174 JSBool
175 jsd_IsLocked(JSDStaticLock* lock)
177 void* me;
178 ASSERT_VALID_LOCK(lock);
179 _CURRENT_THREAD(me);
180 if (lock->owner != me)
181 return JS_FALSE;
182 JS_ASSERT(lock->count > 0);
183 return JS_TRUE;
185 #endif /* DEBUG */
187 void*
188 jsd_CurrentThread()
190 void* me;
191 _CURRENT_THREAD(me);
192 return me;
196 #else /* ! JSD_USE_NSPR_LOCKS */
198 #ifdef WIN32
199 #pragma message("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
200 #pragma message("!! you are compiling the stubbed version of jsd_lock.c !!")
201 #pragma message("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
202 #endif
205 * NOTE: 'Real' versions of these locks must be reentrant in the sense that
206 * they support nested calls to lock and unlock.
209 void*
210 jsd_CreateLock()
212 return (void*)1;
215 void
216 jsd_Lock(void* lock)
220 void
221 jsd_Unlock(void* lock)
225 #ifdef DEBUG
226 JSBool
227 jsd_IsLocked(void* lock)
229 return JS_TRUE;
231 #endif /* DEBUG */
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).
239 #ifdef WIN32
240 /* bogus (but good enough) declaration*/
241 extern void* __stdcall GetCurrentThreadId(void);
242 #endif
244 void*
245 jsd_CurrentThread()
247 #ifdef WIN32
248 return GetCurrentThreadId();
249 #else
250 return (void*)1;
251 #endif
254 #endif /* JSD_USE_NSPR_LOCKS */
256 #endif /* JSD_THREADSAFE */