Back to index

lightning-sunbird  0.9+nobinonly
jslock.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 #ifndef jslock_h__
00040 #define jslock_h__
00041 
00042 #ifdef JS_THREADSAFE
00043 
00044 #include "jstypes.h"
00045 #include "pratom.h"
00046 #include "prlock.h"
00047 #include "prcvar.h"
00048 #include "prthread.h"
00049 
00050 #include "jsprvtd.h"    /* for JSScope, etc. */
00051 #include "jspubtd.h"    /* for JSRuntime, etc. */
00052 
00053 #define Thin_GetWait(W) ((jsword)(W) & 0x1)
00054 #define Thin_SetWait(W) ((jsword)(W) | 0x1)
00055 #define Thin_RemoveWait(W) ((jsword)(W) & ~0x1)
00056 
00057 typedef struct JSFatLock JSFatLock;
00058 
00059 struct JSFatLock {
00060     int         susp;
00061     PRLock      *slock;
00062     PRCondVar   *svar;
00063     JSFatLock   *next;
00064     JSFatLock   **prevp;
00065 };
00066 
00067 typedef struct JSThinLock {
00068     jsword      owner;
00069     JSFatLock   *fat;
00070 } JSThinLock;
00071 
00072 #define CX_THINLOCK_ID(cx)       ((jsword)(cx)->thread)
00073 #define CURRENT_THREAD_IS_ME(me) (((JSThread *)me)->id == js_CurrentThreadId())
00074 
00075 typedef PRLock JSLock;
00076 
00077 typedef struct JSFatLockTable {
00078     JSFatLock   *free;
00079     JSFatLock   *taken;
00080 } JSFatLockTable;
00081 
00082 /*
00083  * Atomic increment and decrement for a reference counter, given jsrefcount *p.
00084  * NB: jsrefcount is int32, aka PRInt32, so that pratom.h functions work.
00085  */
00086 #define JS_ATOMIC_INCREMENT(p)      PR_AtomicIncrement((PRInt32 *)(p))
00087 #define JS_ATOMIC_DECREMENT(p)      PR_AtomicDecrement((PRInt32 *)(p))
00088 #define JS_ATOMIC_ADD(p,v)          PR_AtomicAdd((PRInt32 *)(p), (PRInt32)(v))
00089 
00090 #define js_CurrentThreadId()        (jsword)PR_GetCurrentThread()
00091 #define JS_NEW_LOCK()               PR_NewLock()
00092 #define JS_DESTROY_LOCK(l)          PR_DestroyLock(l)
00093 #define JS_ACQUIRE_LOCK(l)          PR_Lock(l)
00094 #define JS_RELEASE_LOCK(l)          PR_Unlock(l)
00095 #define JS_LOCK0(P,M)               js_Lock(P,M)
00096 #define JS_UNLOCK0(P,M)             js_Unlock(P,M)
00097 
00098 #define JS_NEW_CONDVAR(l)           PR_NewCondVar(l)
00099 #define JS_DESTROY_CONDVAR(cv)      PR_DestroyCondVar(cv)
00100 #define JS_WAIT_CONDVAR(cv,to)      PR_WaitCondVar(cv,to)
00101 #define JS_NO_TIMEOUT               PR_INTERVAL_NO_TIMEOUT
00102 #define JS_NOTIFY_CONDVAR(cv)       PR_NotifyCondVar(cv)
00103 #define JS_NOTIFY_ALL_CONDVAR(cv)   PR_NotifyAllCondVar(cv)
00104 
00105 /*
00106  * Include jsscope.h so JS_LOCK_OBJ macro callers don't have to include it.
00107  * Since there is a JSThinLock member in JSScope, we can't nest this include
00108  * much earlier (see JSThinLock's typedef, above).  Yes, that means there is
00109  * an #include cycle between jslock.h and jsscope.h: moderate-sized XXX here,
00110  * to be fixed by moving JS_LOCK_SCOPE to jsscope.h, JS_LOCK_OBJ to jsobj.h,
00111  * and so on.
00112  */
00113 #include "jsscope.h"
00114 
00115 #define JS_LOCK_RUNTIME(rt)         js_LockRuntime(rt)
00116 #define JS_UNLOCK_RUNTIME(rt)       js_UnlockRuntime(rt)
00117 
00118 /*
00119  * NB: The JS_LOCK_OBJ and JS_UNLOCK_OBJ macros work *only* on native objects
00120  * (objects for which OBJ_IS_NATIVE returns true).  All uses of these macros in
00121  * the engine are predicated on OBJ_IS_NATIVE or equivalent checks.  These uses
00122  * are for optimizations above the JSObjectOps layer, under which object locks
00123  * normally hide.
00124  */
00125 #define JS_LOCK_OBJ(cx,obj)         ((OBJ_SCOPE(obj)->ownercx == (cx))        \
00126                                      ? (void)0                                \
00127                                      : (js_LockObj(cx, obj)))
00128 #define JS_UNLOCK_OBJ(cx,obj)       ((OBJ_SCOPE(obj)->ownercx == (cx))        \
00129                                      ? (void)0 : js_UnlockObj(cx, obj))
00130 
00131 #define JS_LOCK_SCOPE(cx,scope)     ((scope)->ownercx == (cx) ? (void)0       \
00132                                      : js_LockScope(cx, scope))
00133 #define JS_UNLOCK_SCOPE(cx,scope)   ((scope)->ownercx == (cx) ? (void)0       \
00134                                      : js_UnlockScope(cx, scope))
00135 #define JS_TRANSFER_SCOPE_LOCK(cx, scope, newscope)                           \
00136                                     js_TransferScopeLock(cx, scope, newscope)
00137 
00138 extern void js_LockRuntime(JSRuntime *rt);
00139 extern void js_UnlockRuntime(JSRuntime *rt);
00140 extern void js_LockObj(JSContext *cx, JSObject *obj);
00141 extern void js_UnlockObj(JSContext *cx, JSObject *obj);
00142 extern void js_LockScope(JSContext *cx, JSScope *scope);
00143 extern void js_UnlockScope(JSContext *cx, JSScope *scope);
00144 extern int js_SetupLocks(int,int);
00145 extern void js_CleanupLocks();
00146 extern void js_TransferScopeLock(JSContext *, JSScope *, JSScope *);
00147 extern JS_FRIEND_API(jsval)
00148 js_GetSlotThreadSafe(JSContext *, JSObject *, uint32);
00149 extern void js_SetSlotThreadSafe(JSContext *, JSObject *, uint32, jsval);
00150 extern void js_InitLock(JSThinLock *);
00151 extern void js_FinishLock(JSThinLock *);
00152 extern void js_FinishSharingScope(JSRuntime *rt, JSScope *scope);
00153 
00154 #ifdef DEBUG
00155 
00156 #define JS_IS_RUNTIME_LOCKED(rt)        js_IsRuntimeLocked(rt)
00157 #define JS_IS_OBJ_LOCKED(cx,obj)        js_IsObjLocked(cx,obj)
00158 #define JS_IS_SCOPE_LOCKED(cx,scope)    js_IsScopeLocked(cx,scope)
00159 
00160 extern JSBool js_IsRuntimeLocked(JSRuntime *rt);
00161 extern JSBool js_IsObjLocked(JSContext *cx, JSObject *obj);
00162 extern JSBool js_IsScopeLocked(JSContext *cx, JSScope *scope);
00163 
00164 #else
00165 
00166 #define JS_IS_RUNTIME_LOCKED(rt)        0
00167 #define JS_IS_OBJ_LOCKED(cx,obj)        1
00168 #define JS_IS_SCOPE_LOCKED(cx,scope)    1
00169 
00170 #endif /* DEBUG */
00171 
00172 #define JS_LOCK_OBJ_VOID(cx, obj, e)                                          \
00173     JS_BEGIN_MACRO                                                            \
00174         JS_LOCK_OBJ(cx, obj);                                                 \
00175         e;                                                                    \
00176         JS_UNLOCK_OBJ(cx, obj);                                               \
00177     JS_END_MACRO
00178 
00179 #define JS_LOCK_VOID(cx, e)                                                   \
00180     JS_BEGIN_MACRO                                                            \
00181         JSRuntime *_rt = (cx)->runtime;                                       \
00182         JS_LOCK_RUNTIME_VOID(_rt, e);                                         \
00183     JS_END_MACRO
00184 
00185 #if defined(JS_USE_ONLY_NSPR_LOCKS) ||                                        \
00186     !( (defined(_WIN32) && defined(_M_IX86)) ||                               \
00187        (defined(__GNUC__) && defined(__i386__)) ||                            \
00188        ((defined(__USLC__) || defined(_SCO_DS)) && defined(i386)) ||          \
00189        (defined(SOLARIS) && defined(sparc) && defined(ULTRA_SPARC)) ||        \
00190        defined(AIX) )
00191 
00192 #define NSPR_LOCK 1
00193 
00194 #undef JS_LOCK0
00195 #undef JS_UNLOCK0
00196 #define JS_LOCK0(P,M)   (JS_ACQUIRE_LOCK(((JSLock*)(P)->fat)), (P)->owner = (M))
00197 #define JS_UNLOCK0(P,M) ((P)->owner = 0, JS_RELEASE_LOCK(((JSLock*)(P)->fat)))
00198 
00199 #else  /* arch-tests */
00200 
00201 #undef NSPR_LOCK
00202 
00203 extern JS_INLINE void js_Lock(JSThinLock *tl, jsword me);
00204 extern JS_INLINE void js_Unlock(JSThinLock *tl, jsword me);
00205 
00206 #endif /* arch-tests */
00207 
00208 #else  /* !JS_THREADSAFE */
00209 
00210 #define JS_ATOMIC_INCREMENT(p)      (++*(p))
00211 #define JS_ATOMIC_DECREMENT(p)      (--*(p))
00212 #define JS_ATOMIC_ADD(p,v)          (*(p) += (v))
00213 
00214 #define JS_CurrentThreadId() 0
00215 #define JS_NEW_LOCK()               NULL
00216 #define JS_DESTROY_LOCK(l)          ((void)0)
00217 #define JS_ACQUIRE_LOCK(l)          ((void)0)
00218 #define JS_RELEASE_LOCK(l)          ((void)0)
00219 #define JS_LOCK0(P,M)               ((void)0)
00220 #define JS_UNLOCK0(P,M)             ((void)0)
00221 
00222 #define JS_NEW_CONDVAR(l)           NULL
00223 #define JS_DESTROY_CONDVAR(cv)      ((void)0)
00224 #define JS_WAIT_CONDVAR(cv,to)      ((void)0)
00225 #define JS_NOTIFY_CONDVAR(cv)       ((void)0)
00226 #define JS_NOTIFY_ALL_CONDVAR(cv)   ((void)0)
00227 
00228 #define JS_LOCK_RUNTIME(rt)         ((void)0)
00229 #define JS_UNLOCK_RUNTIME(rt)       ((void)0)
00230 #define JS_LOCK_OBJ(cx,obj)         ((void)0)
00231 #define JS_UNLOCK_OBJ(cx,obj)       ((void)0)
00232 #define JS_LOCK_OBJ_VOID(cx,obj,e)  (e)
00233 #define JS_LOCK_SCOPE(cx,scope)     ((void)0)
00234 #define JS_UNLOCK_SCOPE(cx,scope)   ((void)0)
00235 #define JS_TRANSFER_SCOPE_LOCK(c,o,n) ((void)0)
00236 
00237 #define JS_IS_RUNTIME_LOCKED(rt)        1
00238 #define JS_IS_OBJ_LOCKED(cx,obj)        1
00239 #define JS_IS_SCOPE_LOCKED(cx,scope)    1
00240 #define JS_LOCK_VOID(cx, e)             JS_LOCK_RUNTIME_VOID((cx)->runtime, e)
00241 
00242 #endif /* !JS_THREADSAFE */
00243 
00244 #define JS_LOCK_RUNTIME_VOID(rt,e)                                            \
00245     JS_BEGIN_MACRO                                                            \
00246         JS_LOCK_RUNTIME(rt);                                                  \
00247         e;                                                                    \
00248         JS_UNLOCK_RUNTIME(rt);                                                \
00249     JS_END_MACRO
00250 
00251 #define JS_LOCK_GC(rt)              JS_ACQUIRE_LOCK((rt)->gcLock)
00252 #define JS_UNLOCK_GC(rt)            JS_RELEASE_LOCK((rt)->gcLock)
00253 #define JS_LOCK_GC_VOID(rt,e)       (JS_LOCK_GC(rt), (e), JS_UNLOCK_GC(rt))
00254 #define JS_AWAIT_GC_DONE(rt)        JS_WAIT_CONDVAR((rt)->gcDone, JS_NO_TIMEOUT)
00255 #define JS_NOTIFY_GC_DONE(rt)       JS_NOTIFY_ALL_CONDVAR((rt)->gcDone)
00256 #define JS_AWAIT_REQUEST_DONE(rt)   JS_WAIT_CONDVAR((rt)->requestDone,        \
00257                                                     JS_NO_TIMEOUT)
00258 #define JS_NOTIFY_REQUEST_DONE(rt)  JS_NOTIFY_CONDVAR((rt)->requestDone)
00259 
00260 #define JS_LOCK(P,CX)               JS_LOCK0(P, CX_THINLOCK_ID(CX))
00261 #define JS_UNLOCK(P,CX)             JS_UNLOCK0(P, CX_THINLOCK_ID(CX))
00262 
00263 #endif /* jslock_h___ */