1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is Mozilla Communicator client code, released
17  * March 31, 1998.
18  *
19  * The Initial Developer of the Original Code is
20  * Netscape Communications Corporation.
21  * Portions created by the Initial Developer are Copyright (C) 1998
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either of the GNU General Public License Version 2 or later (the "GPL"),
28  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39 #ifndef jslock_h__
40 #define jslock_h__
41 
42 #ifdef JS_THREADSAFE
43 
44 #include "jstypes.h"
45 #include "pratom.h"
46 #include "prlock.h"
47 #include "prcvar.h"
48 
49 #include "jsprvtd.h"    /* for JSScope, etc. */
50 #include "jspubtd.h"    /* for JSRuntime, etc. */
51 
52 #define Thin_GetWait(W) ((jsword)(W) & 0x1)
53 #define Thin_SetWait(W) ((jsword)(W) | 0x1)
54 #define Thin_RemoveWait(W) ((jsword)(W) & ~0x1)
55 
56 typedef struct JSFatLock JSFatLock;
57 
58 struct JSFatLock {
59     int         susp;
60     PRLock      *slock;
61     PRCondVar   *svar;
62     JSFatLock   *next;
63     JSFatLock   **prevp;
64 };
65 
66 typedef struct JSThinLock {
67     jsword      owner;
68     JSFatLock   *fat;
69 } JSThinLock;
70 
71 typedef PRLock JSLock;
72 
73 typedef struct JSFatLockTable {
74     JSFatLock   *free;
75     JSFatLock   *taken;
76 } JSFatLockTable;
77 
78 /*
79  * Atomic increment and decrement for a reference counter, given jsrefcount *p.
80  * NB: jsrefcount is int32, aka PRInt32, so that pratom.h functions work.
81  */
82 #define JS_ATOMIC_INCREMENT(p)      PR_AtomicIncrement((PRInt32 *)(p))
83 #define JS_ATOMIC_DECREMENT(p)      PR_AtomicDecrement((PRInt32 *)(p))
84 #define JS_ATOMIC_ADD(p,v)          PR_AtomicAdd((PRInt32 *)(p), (PRInt32)(v))
85 
86 #define CurrentThreadId()           (jsword)PR_GetCurrentThread()
87 #define JS_CurrentThreadId()        js_CurrentThreadId()
88 #define JS_NEW_LOCK()               PR_NewLock()
89 #define JS_DESTROY_LOCK(l)          PR_DestroyLock(l)
90 #define JS_ACQUIRE_LOCK(l)          PR_Lock(l)
91 #define JS_RELEASE_LOCK(l)          PR_Unlock(l)
92 #define JS_LOCK0(P,M)               js_Lock(P,M)
93 #define JS_UNLOCK0(P,M)             js_Unlock(P,M)
94 
95 #define JS_NEW_CONDVAR(l)           PR_NewCondVar(l)
96 #define JS_DESTROY_CONDVAR(cv)      PR_DestroyCondVar(cv)
97 #define JS_WAIT_CONDVAR(cv,to)      PR_WaitCondVar(cv,to)
98 #define JS_NO_TIMEOUT               PR_INTERVAL_NO_TIMEOUT
99 #define JS_NOTIFY_CONDVAR(cv)       PR_NotifyCondVar(cv)
100 #define JS_NOTIFY_ALL_CONDVAR(cv)   PR_NotifyAllCondVar(cv)
101 
102 /*
103  * Include jsscope.h so JS_LOCK_OBJ macro callers don't have to include it.
104  * Since there is a JSThinLock member in JSScope, we can't nest this include
105  * much earlier (see JSThinLock's typedef, above).  Yes, that means there is
106  * an #include cycle between jslock.h and jsscope.h: moderate-sized XXX here,
107  * to be fixed by moving JS_LOCK_SCOPE to jsscope.h, JS_LOCK_OBJ to jsobj.h,
108  * and so on.
109  *
110  * We also need jsscope.h #ifdef DEBUG for SET_OBJ_INFO and SET_SCOPE_INFO,
111  * but we do not want any nested includes that depend on DEBUG.  Those lead
112  * to build bustage when someone makes a change that depends in a subtle way
113  * on jsscope.h being included directly or indirectly, but does not test by
114  * building optimized as well as DEBUG.
115  */
116 #include "jsscope.h"
117 
118 #ifdef DEBUG
119 
120 #define SET_OBJ_INFO(obj_,file_,line_)                                        \
121     SET_SCOPE_INFO(OBJ_SCOPE(obj_),file_,line_)
122 
123 #define SET_SCOPE_INFO(scope_,file_,line_)                                    \
124     ((scope_)->ownercx ? (void)0 :                                            \
125      (JS_ASSERT((0 < (scope_)->u.count && (scope_)->u.count <= 4) ||          \
126                 SCOPE_IS_SEALED(scope_)),                                     \
127       (void)((scope_)->file[(scope_)->u.count-1] = (file_),                   \
128              (scope_)->line[(scope_)->u.count-1] = (line_))))
129 #endif /* DEBUG */
130 
131 #define JS_LOCK_RUNTIME(rt)         js_LockRuntime(rt)
132 #define JS_UNLOCK_RUNTIME(rt)       js_UnlockRuntime(rt)
133 
134 /*
135  * NB: The JS_LOCK_OBJ and JS_UNLOCK_OBJ macros work *only* on native objects
136  * (objects for which OBJ_IS_NATIVE returns true).  All uses of these macros in
137  * the engine are predicated on OBJ_IS_NATIVE or equivalent checks.  These uses
138  * are for optimizations above the JSObjectOps layer, under which object locks
139  * normally hide.
140  */
141 #define JS_LOCK_OBJ(cx,obj)         ((OBJ_SCOPE(obj)->ownercx == (cx))        \
142                                      ? (void)0                                \
143                                      : (js_LockObj(cx, obj),                  \
144                                         SET_OBJ_INFO(obj,__FILE__,__LINE__)))
145 #define JS_UNLOCK_OBJ(cx,obj)       ((OBJ_SCOPE(obj)->ownercx == (cx))        \
146                                      ? (void)0 : js_UnlockObj(cx, obj))
147 
148 #define JS_LOCK_SCOPE(cx,scope)     ((scope)->ownercx == (cx) ? (void)0 :     \
149                                      (js_LockScope(cx, scope),                \
150                                       SET_SCOPE_INFO(scope,__FILE__,__LINE__)))
151 #define JS_UNLOCK_SCOPE(cx,scope)   ((scope)->ownercx == (cx) ? (void)0 :     \
152                                      js_UnlockScope(cx, scope))
153 #define JS_TRANSFER_SCOPE_LOCK(cx, scope, newscope)                           \
154                                     js_TransferScopeLock(cx, scope, newscope)
155 
156 extern jsword js_CurrentThreadId();
157 extern void js_LockRuntime(JSRuntime *rt);
158 extern void js_UnlockRuntime(JSRuntime *rt);
159 extern void js_LockObj(JSContext *cx, JSObject *obj);
160 extern void js_UnlockObj(JSContext *cx, JSObject *obj);
161 extern void js_LockScope(JSContext *cx, JSScope *scope);
162 extern void js_UnlockScope(JSContext *cx, JSScope *scope);
163 extern int js_SetupLocks(int,int);
164 extern void js_CleanupLocks();
165 extern void js_InitContextForLocking(JSContext *);
166 extern void js_TransferScopeLock(JSContext *, JSScope *, JSScope *);
167 extern JS_FRIEND_API(jsval)
168 js_GetSlotThreadSafe(JSContext *, JSObject *, uint32);
169 extern void js_SetSlotThreadSafe(JSContext *, JSObject *, uint32, jsval);
170 extern void js_InitLock(JSThinLock *);
171 extern void js_FinishLock(JSThinLock *);
172 extern void js_FinishSharingScope(JSRuntime *rt, JSScope *scope);
173 
174 #ifdef DEBUG
175 
176 #define JS_IS_RUNTIME_LOCKED(rt)        js_IsRuntimeLocked(rt)
177 #define JS_IS_OBJ_LOCKED(cx,obj)        js_IsObjLocked(cx,obj)
178 #define JS_IS_SCOPE_LOCKED(cx,scope)    js_IsScopeLocked(cx,scope)
179 
180 extern JSBool js_IsRuntimeLocked(JSRuntime *rt);
181 extern JSBool js_IsObjLocked(JSContext *cx, JSObject *obj);
182 extern JSBool js_IsScopeLocked(JSContext *cx, JSScope *scope);
183 
184 #else
185 
186 #define JS_IS_RUNTIME_LOCKED(rt)        0
187 #define JS_IS_OBJ_LOCKED(cx,obj)        1
188 #define JS_IS_SCOPE_LOCKED(cx,scope)    1
189 
190 #endif /* DEBUG */
191 
192 #define JS_LOCK_OBJ_VOID(cx, obj, e)                                          \
193     JS_BEGIN_MACRO                                                            \
194         JS_LOCK_OBJ(cx, obj);                                                 \
195         e;                                                                    \
196         JS_UNLOCK_OBJ(cx, obj);                                               \
197     JS_END_MACRO
198 
199 #define JS_LOCK_VOID(cx, e)                                                   \
200     JS_BEGIN_MACRO                                                            \
201         JSRuntime *_rt = (cx)->runtime;                                       \
202         JS_LOCK_RUNTIME_VOID(_rt, e);                                         \
203     JS_END_MACRO
204 
205 #if defined(JS_USE_ONLY_NSPR_LOCKS) ||                                        \
206     !( (defined(_WIN32) && defined(_M_IX86)) ||                               \
207        (defined(__GNUC__) && defined(__i386__)) ||                            \
208        (defined(SOLARIS) && defined(sparc) && defined(ULTRA_SPARC)) ||        \
209        defined(AIX) )
210 
211 #define NSPR_LOCK 1
212 
213 #undef JS_LOCK0
214 #undef JS_UNLOCK0
215 #define JS_LOCK0(P,M)   (JS_ACQUIRE_LOCK(((JSLock*)(P)->fat)), (P)->owner = (M))
216 #define JS_UNLOCK0(P,M) ((P)->owner = 0, JS_RELEASE_LOCK(((JSLock*)(P)->fat)))
217 
218 #else  /* arch-tests */
219 
220 #undef NSPR_LOCK
221 
222 extern JS_INLINE void js_Lock(JSThinLock *tl, jsword me);
223 extern JS_INLINE void js_Unlock(JSThinLock *tl, jsword me);
224 
225 #endif /* arch-tests */
226 
227 #else  /* !JS_THREADSAFE */
228 
229 #define JS_ATOMIC_INCREMENT(p)      (++*(p))
230 #define JS_ATOMIC_DECREMENT(p)      (--*(p))
231 #define JS_ATOMIC_ADD(p,v)          (*(p) += (v))
232 
233 #define JS_CurrentThreadId() 0
234 #define JS_NEW_LOCK()               NULL
235 #define JS_DESTROY_LOCK(l)          ((void)0)
236 #define JS_ACQUIRE_LOCK(l)          ((void)0)
237 #define JS_RELEASE_LOCK(l)          ((void)0)
238 #define JS_LOCK0(P,M)               ((void)0)
239 #define JS_UNLOCK0(P,M)             ((void)0)
240 
241 #define JS_NEW_CONDVAR(l)           NULL
242 #define JS_DESTROY_CONDVAR(cv)      ((void)0)
243 #define JS_WAIT_CONDVAR(cv,to)      ((void)0)
244 #define JS_NOTIFY_CONDVAR(cv)       ((void)0)
245 #define JS_NOTIFY_ALL_CONDVAR(cv)   ((void)0)
246 
247 #define JS_LOCK_RUNTIME(rt)         ((void)0)
248 #define JS_UNLOCK_RUNTIME(rt)       ((void)0)
249 #define JS_LOCK_OBJ(cx,obj)         ((void)0)
250 #define JS_UNLOCK_OBJ(cx,obj)       ((void)0)
251 #define JS_LOCK_OBJ_VOID(cx,obj,e)  (e)
252 #define JS_LOCK_SCOPE(cx,scope)     ((void)0)
253 #define JS_UNLOCK_SCOPE(cx,scope)   ((void)0)
254 #define JS_TRANSFER_SCOPE_LOCK(c,o,n) ((void)0)
255 
256 #define JS_IS_RUNTIME_LOCKED(rt)        1
257 #define JS_IS_OBJ_LOCKED(cx,obj)        1
258 #define JS_IS_SCOPE_LOCKED(cx,scope)    1
259 #define JS_LOCK_VOID(cx, e)             JS_LOCK_RUNTIME_VOID((cx)->runtime, e)
260 
261 #endif /* !JS_THREADSAFE */
262 
263 #define JS_LOCK_RUNTIME_VOID(rt,e)                                            \
264     JS_BEGIN_MACRO                                                            \
265         JS_LOCK_RUNTIME(rt);                                                  \
266         e;                                                                    \
267         JS_UNLOCK_RUNTIME(rt);                                                \
268     JS_END_MACRO
269 
270 #define JS_LOCK_GC(rt)              JS_ACQUIRE_LOCK((rt)->gcLock)
271 #define JS_UNLOCK_GC(rt)            JS_RELEASE_LOCK((rt)->gcLock)
272 #define JS_LOCK_GC_VOID(rt,e)       (JS_LOCK_GC(rt), (e), JS_UNLOCK_GC(rt))
273 #define JS_AWAIT_GC_DONE(rt)        JS_WAIT_CONDVAR((rt)->gcDone, JS_NO_TIMEOUT)
274 #define JS_NOTIFY_GC_DONE(rt)       JS_NOTIFY_ALL_CONDVAR((rt)->gcDone)
275 #define JS_AWAIT_REQUEST_DONE(rt)   JS_WAIT_CONDVAR((rt)->requestDone,        \
276                                                     JS_NO_TIMEOUT)
277 #define JS_NOTIFY_REQUEST_DONE(rt)  JS_NOTIFY_CONDVAR((rt)->requestDone)
278 
279 #define JS_LOCK(P,CX)               JS_LOCK0(P,(CX)->thread)
280 #define JS_UNLOCK(P,CX)             JS_UNLOCK0(P,(CX)->thread)
281 
282 #ifndef SET_OBJ_INFO
283 #define SET_OBJ_INFO(obj,f,l)       ((void)0)
284 #endif
285 #ifndef SET_SCOPE_INFO
286 #define SET_SCOPE_INFO(scope,f,l)   ((void)0)
287 #endif
288 
289 #endif /* jslock_h___ */
290