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 #include "prthread.h" 49 50 #include "jsprvtd.h" /* for JSScope, etc. */ 51 #include "jspubtd.h" /* for JSRuntime, etc. */ 52 53 #define Thin_GetWait(W) ((jsword)(W) & 0x1) 54 #define Thin_SetWait(W) ((jsword)(W) | 0x1) 55 #define Thin_RemoveWait(W) ((jsword)(W) & ~0x1) 56 57 typedef struct JSFatLock JSFatLock; 58 59 struct JSFatLock { 60 int susp; 61 PRLock *slock; 62 PRCondVar *svar; 63 JSFatLock *next; 64 JSFatLock **prevp; 65 }; 66 67 typedef struct JSThinLock { 68 jsword owner; 69 JSFatLock *fat; 70 } JSThinLock; 71 72 #define CX_THINLOCK_ID(cx) ((jsword)(cx)->thread) 73 #define CURRENT_THREAD_IS_ME(me) (((JSThread *)me)->id == js_CurrentThreadId()) 74 75 typedef PRLock JSLock; 76 77 typedef struct JSFatLockTable { 78 JSFatLock *free; 79 JSFatLock *taken; 80 } JSFatLockTable; 81 82 /* 83 * Atomic increment and decrement for a reference counter, given jsrefcount *p. 84 * NB: jsrefcount is int32, aka PRInt32, so that pratom.h functions work. 85 */ 86 #define JS_ATOMIC_INCREMENT(p) PR_AtomicIncrement((PRInt32 *)(p)) 87 #define JS_ATOMIC_DECREMENT(p) PR_AtomicDecrement((PRInt32 *)(p)) 88 #define JS_ATOMIC_ADD(p,v) PR_AtomicAdd((PRInt32 *)(p), (PRInt32)(v)) 89 90 #define js_CurrentThreadId() (jsword)PR_GetCurrentThread() 91 #define JS_NEW_LOCK() PR_NewLock() 92 #define JS_DESTROY_LOCK(l) PR_DestroyLock(l) 93 #define JS_ACQUIRE_LOCK(l) PR_Lock(l) 94 #define JS_RELEASE_LOCK(l) PR_Unlock(l) 95 #define JS_LOCK0(P,M) js_Lock(P,M) 96 #define JS_UNLOCK0(P,M) js_Unlock(P,M) 97 98 #define JS_NEW_CONDVAR(l) PR_NewCondVar(l) 99 #define JS_DESTROY_CONDVAR(cv) PR_DestroyCondVar(cv) 100 #define JS_WAIT_CONDVAR(cv,to) PR_WaitCondVar(cv,to) 101 #define JS_NO_TIMEOUT PR_INTERVAL_NO_TIMEOUT 102 #define JS_NOTIFY_CONDVAR(cv) PR_NotifyCondVar(cv) 103 #define JS_NOTIFY_ALL_CONDVAR(cv) PR_NotifyAllCondVar(cv) 104 105 /* 106 * Include jsscope.h so JS_LOCK_OBJ macro callers don't have to include it. 107 * Since there is a JSThinLock member in JSScope, we can't nest this include 108 * much earlier (see JSThinLock's typedef, above). Yes, that means there is 109 * an #include cycle between jslock.h and jsscope.h: moderate-sized XXX here, 110 * to be fixed by moving JS_LOCK_SCOPE to jsscope.h, JS_LOCK_OBJ to jsobj.h, 111 * and so on. 112 */ 113 #include "jsscope.h" 114 115 #define JS_LOCK_RUNTIME(rt) js_LockRuntime(rt) 116 #define JS_UNLOCK_RUNTIME(rt) js_UnlockRuntime(rt) 117 118 /* 119 * NB: The JS_LOCK_OBJ and JS_UNLOCK_OBJ macros work *only* on native objects 120 * (objects for which OBJ_IS_NATIVE returns true). All uses of these macros in 121 * the engine are predicated on OBJ_IS_NATIVE or equivalent checks. These uses 122 * are for optimizations above the JSObjectOps layer, under which object locks 123 * normally hide. 124 */ 125 #define JS_LOCK_OBJ(cx,obj) ((OBJ_SCOPE(obj)->ownercx == (cx)) \ 126 ? (void)0 \ 127 : (js_LockObj(cx, obj))) 128 #define JS_UNLOCK_OBJ(cx,obj) ((OBJ_SCOPE(obj)->ownercx == (cx)) \ 129 ? (void)0 : js_UnlockObj(cx, obj)) 130 131 #define JS_LOCK_SCOPE(cx,scope) ((scope)->ownercx == (cx) ? (void)0 \ 132 : js_LockScope(cx, scope)) 133 #define JS_UNLOCK_SCOPE(cx,scope) ((scope)->ownercx == (cx) ? (void)0 \ 134 : js_UnlockScope(cx, scope)) 135 #define JS_TRANSFER_SCOPE_LOCK(cx, scope, newscope) \ 136 js_TransferScopeLock(cx, scope, newscope) 137 138 extern void js_LockRuntime(JSRuntime *rt); 139 extern void js_UnlockRuntime(JSRuntime *rt); 140 extern void js_LockObj(JSContext *cx, JSObject *obj); 141 extern void js_UnlockObj(JSContext *cx, JSObject *obj); 142 extern void js_LockScope(JSContext *cx, JSScope *scope); 143 extern void js_UnlockScope(JSContext *cx, JSScope *scope); 144 extern int js_SetupLocks(int,int); 145 extern void js_CleanupLocks(); 146 extern void js_TransferScopeLock(JSContext *, JSScope *, JSScope *); 147 extern JS_FRIEND_API(jsval) 148 js_GetSlotThreadSafe(JSContext *, JSObject *, uint32); 149 extern void js_SetSlotThreadSafe(JSContext *, JSObject *, uint32, jsval); 150 extern void js_InitLock(JSThinLock *); 151 extern void js_FinishLock(JSThinLock *); 152 extern void js_FinishSharingScope(JSRuntime *rt, JSScope *scope); 153 154 #ifdef DEBUG 155 156 #define JS_IS_RUNTIME_LOCKED(rt) js_IsRuntimeLocked(rt) 157 #define JS_IS_OBJ_LOCKED(cx,obj) js_IsObjLocked(cx,obj) 158 #define JS_IS_SCOPE_LOCKED(cx,scope) js_IsScopeLocked(cx,scope) 159 160 extern JSBool js_IsRuntimeLocked(JSRuntime *rt); 161 extern JSBool js_IsObjLocked(JSContext *cx, JSObject *obj); 162 extern JSBool js_IsScopeLocked(JSContext *cx, JSScope *scope); 163 164 #else 165 166 #define JS_IS_RUNTIME_LOCKED(rt) 0 167 #define JS_IS_OBJ_LOCKED(cx,obj) 1 168 #define JS_IS_SCOPE_LOCKED(cx,scope) 1 169 170 #endif /* DEBUG */ 171 172 #define JS_LOCK_OBJ_VOID(cx, obj, e) \ 173 JS_BEGIN_MACRO \ 174 JS_LOCK_OBJ(cx, obj); \ 175 e; \ 176 JS_UNLOCK_OBJ(cx, obj); \ 177 JS_END_MACRO 178 179 #define JS_LOCK_VOID(cx, e) \ 180 JS_BEGIN_MACRO \ 181 JSRuntime *_rt = (cx)->runtime; \ 182 JS_LOCK_RUNTIME_VOID(_rt, e); \ 183 JS_END_MACRO 184 185 /* FIXME: bug 353962 hackaround */ 186 #define JS_USE_ONLY_NSPR_LOCKS 1 187 188 #if defined(JS_USE_ONLY_NSPR_LOCKS) || \ 189 !( (defined(_WIN32) && defined(_M_IX86)) || \ 190 (defined(__GNUC__) && defined(__i386__)) || \ 191 ((defined(__USLC__) || defined(_SCO_DS)) && defined(i386)) || \ 192 (defined(SOLARIS) && defined(sparc) && defined(ULTRA_SPARC)) || \ 193 defined(AIX) ) 194 195 #define NSPR_LOCK 1 196 197 #undef JS_LOCK0 198 #undef JS_UNLOCK0 199 #define JS_LOCK0(P,M) (JS_ACQUIRE_LOCK(((JSLock*)(P)->fat)), (P)->owner = (M)) 200 #define JS_UNLOCK0(P,M) ((P)->owner = 0, JS_RELEASE_LOCK(((JSLock*)(P)->fat))) 201 202 #else /* arch-tests */ 203 204 #undef NSPR_LOCK 205 206 extern JS_INLINE void js_Lock(JSThinLock *tl, jsword me); 207 extern JS_INLINE void js_Unlock(JSThinLock *tl, jsword me); 208 209 #endif /* arch-tests */ 210 211 #else /* !JS_THREADSAFE */ 212 213 #define JS_ATOMIC_INCREMENT(p) (++*(p)) 214 #define JS_ATOMIC_DECREMENT(p) (--*(p)) 215 #define JS_ATOMIC_ADD(p,v) (*(p) += (v)) 216 217 #define JS_CurrentThreadId() 0 218 #define JS_NEW_LOCK() NULL 219 #define JS_DESTROY_LOCK(l) ((void)0) 220 #define JS_ACQUIRE_LOCK(l) ((void)0) 221 #define JS_RELEASE_LOCK(l) ((void)0) 222 #define JS_LOCK0(P,M) ((void)0) 223 #define JS_UNLOCK0(P,M) ((void)0) 224 225 #define JS_NEW_CONDVAR(l) NULL 226 #define JS_DESTROY_CONDVAR(cv) ((void)0) 227 #define JS_WAIT_CONDVAR(cv,to) ((void)0) 228 #define JS_NOTIFY_CONDVAR(cv) ((void)0) 229 #define JS_NOTIFY_ALL_CONDVAR(cv) ((void)0) 230 231 #define JS_LOCK_RUNTIME(rt) ((void)0) 232 #define JS_UNLOCK_RUNTIME(rt) ((void)0) 233 #define JS_LOCK_OBJ(cx,obj) ((void)0) 234 #define JS_UNLOCK_OBJ(cx,obj) ((void)0) 235 #define JS_LOCK_OBJ_VOID(cx,obj,e) (e) 236 #define JS_LOCK_SCOPE(cx,scope) ((void)0) 237 #define JS_UNLOCK_SCOPE(cx,scope) ((void)0) 238 #define JS_TRANSFER_SCOPE_LOCK(c,o,n) ((void)0) 239 240 #define JS_IS_RUNTIME_LOCKED(rt) 1 241 #define JS_IS_OBJ_LOCKED(cx,obj) 1 242 #define JS_IS_SCOPE_LOCKED(cx,scope) 1 243 #define JS_LOCK_VOID(cx, e) JS_LOCK_RUNTIME_VOID((cx)->runtime, e) 244 245 #endif /* !JS_THREADSAFE */ 246 247 #define JS_LOCK_RUNTIME_VOID(rt,e) \ 248 JS_BEGIN_MACRO \ 249 JS_LOCK_RUNTIME(rt); \ 250 e; \ 251 JS_UNLOCK_RUNTIME(rt); \ 252 JS_END_MACRO 253 254 #define JS_LOCK_GC(rt) JS_ACQUIRE_LOCK((rt)->gcLock) 255 #define JS_UNLOCK_GC(rt) JS_RELEASE_LOCK((rt)->gcLock) 256 #define JS_LOCK_GC_VOID(rt,e) (JS_LOCK_GC(rt), (e), JS_UNLOCK_GC(rt)) 257 #define JS_AWAIT_GC_DONE(rt) JS_WAIT_CONDVAR((rt)->gcDone, JS_NO_TIMEOUT) 258 #define JS_NOTIFY_GC_DONE(rt) JS_NOTIFY_ALL_CONDVAR((rt)->gcDone) 259 #define JS_AWAIT_REQUEST_DONE(rt) JS_WAIT_CONDVAR((rt)->requestDone, \ 260 JS_NO_TIMEOUT) 261 #define JS_NOTIFY_REQUEST_DONE(rt) JS_NOTIFY_CONDVAR((rt)->requestDone) 262 263 #define JS_LOCK(P,CX) JS_LOCK0(P, CX_THINLOCK_ID(CX)) 264 #define JS_UNLOCK(P,CX) JS_UNLOCK0(P, CX_THINLOCK_ID(CX)) 265 266 #endif /* jslock_h___ */ 267