PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/jslock.h

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C++ Header | 311 lines | 200 code | 56 blank | 55 comment | 17 complexity | 45c16fe39fd2b6888e0c5c9f690d8eb4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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. #include "jstypes.h"
  42. #include "jsprvtd.h" /* for JSScope, etc. */
  43. #include "jspubtd.h" /* for JSRuntime, etc. */
  44. #ifdef JS_THREADSAFE
  45. # include "pratom.h"
  46. # include "prlock.h"
  47. # include "prcvar.h"
  48. # include "prthread.h"
  49. #endif
  50. JS_BEGIN_EXTERN_C
  51. #ifdef JS_THREADSAFE
  52. #if (defined(_WIN32) && defined(_M_IX86)) || \
  53. (defined(__GNUC__) && defined(__i386__)) || \
  54. (defined(__GNUC__) && defined(__x86_64__)) || \
  55. (defined(SOLARIS) && defined(sparc) && defined(ULTRA_SPARC)) || \
  56. defined(AIX) || \
  57. defined(USE_ARM_KUSER)
  58. # define JS_HAS_NATIVE_COMPARE_AND_SWAP 1
  59. #else
  60. # define JS_HAS_NATIVE_COMPARE_AND_SWAP 0
  61. #endif
  62. #if defined(JS_USE_ONLY_NSPR_LOCKS) || !JS_HAS_NATIVE_COMPARE_AND_SWAP
  63. # define NSPR_LOCK 1
  64. #else
  65. # undef NSPR_LOCK
  66. #endif
  67. #define Thin_GetWait(W) ((jsword)(W) & 0x1)
  68. #define Thin_SetWait(W) ((jsword)(W) | 0x1)
  69. #define Thin_RemoveWait(W) ((jsword)(W) & ~0x1)
  70. typedef struct JSFatLock JSFatLock;
  71. typedef struct JSThinLock {
  72. jsword owner;
  73. JSFatLock *fat;
  74. } JSThinLock;
  75. #define CX_THINLOCK_ID(cx) ((jsword)(cx)->thread)
  76. #define CURRENT_THREAD_IS_ME(me) (((JSThread *)me)->id == js_CurrentThreadId())
  77. typedef PRLock JSLock;
  78. typedef struct JSTitle JSTitle;
  79. struct JSTitle {
  80. JSContext *ownercx; /* creating context, NULL if shared */
  81. JSThinLock lock; /* binary semaphore protecting title */
  82. union { /* union lockful and lock-free state: */
  83. jsrefcount count; /* lock entry count for reentrancy */
  84. JSTitle *link; /* next link in rt->titleSharingTodo */
  85. } u;
  86. #ifdef JS_DEBUG_TITLE_LOCKS
  87. const char *file[4]; /* file where lock was (re-)taken */
  88. unsigned int line[4]; /* line where lock was (re-)taken */
  89. #endif
  90. };
  91. /*
  92. * Title structures must be immediately preceded by JSObjectMap structures for
  93. * maps that use titles for threadsafety. This is enforced by assertion in
  94. * jsscope.h; see bug 408416 for future remedies to this somewhat fragile
  95. * architecture.
  96. */
  97. #define TITLE_TO_MAP(title) \
  98. ((JSObjectMap *)((char *)(title) - sizeof(JSObjectMap)))
  99. /*
  100. * Atomic increment and decrement for a reference counter, given jsrefcount *p.
  101. * NB: jsrefcount is int32, aka PRInt32, so that pratom.h functions work.
  102. */
  103. #define JS_ATOMIC_INCREMENT(p) PR_AtomicIncrement((PRInt32 *)(p))
  104. #define JS_ATOMIC_DECREMENT(p) PR_AtomicDecrement((PRInt32 *)(p))
  105. #define JS_ATOMIC_ADD(p,v) PR_AtomicAdd((PRInt32 *)(p), (PRInt32)(v))
  106. #define js_CurrentThreadId() (jsword)PR_GetCurrentThread()
  107. #define JS_NEW_LOCK() PR_NewLock()
  108. #define JS_DESTROY_LOCK(l) PR_DestroyLock(l)
  109. #define JS_ACQUIRE_LOCK(l) PR_Lock(l)
  110. #define JS_RELEASE_LOCK(l) PR_Unlock(l)
  111. #define JS_NEW_CONDVAR(l) PR_NewCondVar(l)
  112. #define JS_DESTROY_CONDVAR(cv) PR_DestroyCondVar(cv)
  113. #define JS_WAIT_CONDVAR(cv,to) PR_WaitCondVar(cv,to)
  114. #define JS_NO_TIMEOUT PR_INTERVAL_NO_TIMEOUT
  115. #define JS_NOTIFY_CONDVAR(cv) PR_NotifyCondVar(cv)
  116. #define JS_NOTIFY_ALL_CONDVAR(cv) PR_NotifyAllCondVar(cv)
  117. #ifdef JS_DEBUG_TITLE_LOCKS
  118. #define SET_OBJ_INFO(obj_, file_, line_) \
  119. SET_SCOPE_INFO(OBJ_SCOPE(obj_), file_, line_)
  120. #define SET_SCOPE_INFO(scope_, file_, line_) \
  121. js_SetScopeInfo(scope_, file_, line_)
  122. #endif
  123. #define JS_LOCK(cx, tl) js_Lock(cx, tl)
  124. #define JS_UNLOCK(cx, tl) js_Unlock(cx, tl)
  125. #define JS_LOCK_RUNTIME(rt) js_LockRuntime(rt)
  126. #define JS_UNLOCK_RUNTIME(rt) js_UnlockRuntime(rt)
  127. /*
  128. * NB: The JS_LOCK_OBJ and JS_UNLOCK_OBJ macros work *only* on native objects
  129. * (objects for which OBJ_IS_NATIVE returns true). All uses of these macros in
  130. * the engine are predicated on OBJ_IS_NATIVE or equivalent checks. These uses
  131. * are for optimizations above the JSObjectOps layer, under which object locks
  132. * normally hide.
  133. */
  134. #define JS_LOCK_OBJ(cx,obj) ((OBJ_SCOPE(obj)->title.ownercx == (cx)) \
  135. ? (void)0 \
  136. : (js_LockObj(cx, obj), \
  137. SET_OBJ_INFO(obj,__FILE__,__LINE__)))
  138. #define JS_UNLOCK_OBJ(cx,obj) ((OBJ_SCOPE(obj)->title.ownercx == (cx)) \
  139. ? (void)0 : js_UnlockObj(cx, obj))
  140. #define JS_LOCK_TITLE(cx,title) \
  141. ((title)->ownercx == (cx) ? (void)0 \
  142. : (js_LockTitle(cx, (title)), \
  143. SET_TITLE_INFO(title,__FILE__,__LINE__)))
  144. #define JS_UNLOCK_TITLE(cx,title) ((title)->ownercx == (cx) ? (void)0 \
  145. : js_UnlockTitle(cx, title))
  146. #define JS_LOCK_SCOPE(cx,scope) JS_LOCK_TITLE(cx,&(scope)->title)
  147. #define JS_UNLOCK_SCOPE(cx,scope) JS_UNLOCK_TITLE(cx,&(scope)->title)
  148. #define JS_TRANSFER_SCOPE_LOCK(cx, scope, newscope) \
  149. js_TransferTitle(cx, &scope->title, &newscope->title)
  150. extern void js_Lock(JSContext *cx, JSThinLock *tl);
  151. extern void js_Unlock(JSContext *cx, JSThinLock *tl);
  152. extern void js_LockRuntime(JSRuntime *rt);
  153. extern void js_UnlockRuntime(JSRuntime *rt);
  154. extern void js_LockObj(JSContext *cx, JSObject *obj);
  155. extern void js_UnlockObj(JSContext *cx, JSObject *obj);
  156. extern void js_InitTitle(JSContext *cx, JSTitle *title);
  157. extern void js_FinishTitle(JSContext *cx, JSTitle *title);
  158. extern void js_LockTitle(JSContext *cx, JSTitle *title);
  159. extern void js_UnlockTitle(JSContext *cx, JSTitle *title);
  160. extern int js_SetupLocks(int,int);
  161. extern void js_CleanupLocks();
  162. extern void js_TransferTitle(JSContext *, JSTitle *, JSTitle *);
  163. extern JS_FRIEND_API(jsval)
  164. js_GetSlotThreadSafe(JSContext *, JSObject *, uint32);
  165. extern void js_SetSlotThreadSafe(JSContext *, JSObject *, uint32, jsval);
  166. extern void js_InitLock(JSThinLock *);
  167. extern void js_FinishLock(JSThinLock *);
  168. extern void js_FinishSharingTitle(JSContext *cx, JSTitle *title);
  169. #ifdef DEBUG
  170. #define JS_IS_RUNTIME_LOCKED(rt) js_IsRuntimeLocked(rt)
  171. #define JS_IS_OBJ_LOCKED(cx,obj) js_IsObjLocked(cx,obj)
  172. #define JS_IS_TITLE_LOCKED(cx,title) js_IsTitleLocked(cx,title)
  173. extern JSBool js_IsRuntimeLocked(JSRuntime *rt);
  174. extern JSBool js_IsObjLocked(JSContext *cx, JSObject *obj);
  175. extern JSBool js_IsTitleLocked(JSContext *cx, JSTitle *title);
  176. #ifdef JS_DEBUG_TITLE_LOCKS
  177. extern void js_SetScopeInfo(JSScope *scope, const char *file, int line);
  178. #endif
  179. #else
  180. #define JS_IS_RUNTIME_LOCKED(rt) 0
  181. #define JS_IS_OBJ_LOCKED(cx,obj) 1
  182. #define JS_IS_TITLE_LOCKED(cx,title) 1
  183. #endif /* DEBUG */
  184. #define JS_LOCK_OBJ_VOID(cx, obj, e) \
  185. JS_BEGIN_MACRO \
  186. JS_LOCK_OBJ(cx, obj); \
  187. e; \
  188. JS_UNLOCK_OBJ(cx, obj); \
  189. JS_END_MACRO
  190. #define JS_LOCK_VOID(cx, e) \
  191. JS_BEGIN_MACRO \
  192. JSRuntime *_rt = (cx)->runtime; \
  193. JS_LOCK_RUNTIME_VOID(_rt, e); \
  194. JS_END_MACRO
  195. #else /* !JS_THREADSAFE */
  196. #define JS_ATOMIC_INCREMENT(p) (++*(p))
  197. #define JS_ATOMIC_DECREMENT(p) (--*(p))
  198. #define JS_ATOMIC_ADD(p,v) (*(p) += (v))
  199. #define JS_CurrentThreadId() 0
  200. #define JS_NEW_LOCK() NULL
  201. #define JS_DESTROY_LOCK(l) ((void)0)
  202. #define JS_ACQUIRE_LOCK(l) ((void)0)
  203. #define JS_RELEASE_LOCK(l) ((void)0)
  204. #define JS_LOCK(cx, tl) ((void)0)
  205. #define JS_UNLOCK(cx, tl) ((void)0)
  206. #define JS_NEW_CONDVAR(l) NULL
  207. #define JS_DESTROY_CONDVAR(cv) ((void)0)
  208. #define JS_WAIT_CONDVAR(cv,to) ((void)0)
  209. #define JS_NOTIFY_CONDVAR(cv) ((void)0)
  210. #define JS_NOTIFY_ALL_CONDVAR(cv) ((void)0)
  211. #define JS_LOCK_RUNTIME(rt) ((void)0)
  212. #define JS_UNLOCK_RUNTIME(rt) ((void)0)
  213. #define JS_LOCK_OBJ(cx,obj) ((void)0)
  214. #define JS_UNLOCK_OBJ(cx,obj) ((void)0)
  215. #define JS_LOCK_OBJ_VOID(cx,obj,e) (e)
  216. #define JS_LOCK_SCOPE(cx,scope) ((void)0)
  217. #define JS_UNLOCK_SCOPE(cx,scope) ((void)0)
  218. #define JS_TRANSFER_SCOPE_LOCK(c,o,n) ((void)0)
  219. #define JS_IS_RUNTIME_LOCKED(rt) 1
  220. #define JS_IS_OBJ_LOCKED(cx,obj) 1
  221. #define JS_IS_TITLE_LOCKED(cx,title) 1
  222. #define JS_LOCK_VOID(cx, e) JS_LOCK_RUNTIME_VOID((cx)->runtime, e)
  223. #endif /* !JS_THREADSAFE */
  224. #define JS_LOCK_RUNTIME_VOID(rt,e) \
  225. JS_BEGIN_MACRO \
  226. JS_LOCK_RUNTIME(rt); \
  227. e; \
  228. JS_UNLOCK_RUNTIME(rt); \
  229. JS_END_MACRO
  230. #define JS_LOCK_GC(rt) JS_ACQUIRE_LOCK((rt)->gcLock)
  231. #define JS_UNLOCK_GC(rt) JS_RELEASE_LOCK((rt)->gcLock)
  232. #define JS_LOCK_GC_VOID(rt,e) (JS_LOCK_GC(rt), (e), JS_UNLOCK_GC(rt))
  233. #define JS_AWAIT_GC_DONE(rt) JS_WAIT_CONDVAR((rt)->gcDone, JS_NO_TIMEOUT)
  234. #define JS_NOTIFY_GC_DONE(rt) JS_NOTIFY_ALL_CONDVAR((rt)->gcDone)
  235. #define JS_AWAIT_REQUEST_DONE(rt) JS_WAIT_CONDVAR((rt)->requestDone, \
  236. JS_NO_TIMEOUT)
  237. #define JS_NOTIFY_REQUEST_DONE(rt) JS_NOTIFY_CONDVAR((rt)->requestDone)
  238. #ifndef SET_OBJ_INFO
  239. #define SET_OBJ_INFO(obj,f,l) ((void)0)
  240. #endif
  241. #ifndef SET_TITLE_INFO
  242. #define SET_TITLE_INFO(title,f,l) ((void)0)
  243. #endif
  244. #ifdef JS_THREADSAFE
  245. extern JSBool
  246. js_CompareAndSwap(jsword *w, jsword ov, jsword nv);
  247. #else
  248. static inline JSBool
  249. js_CompareAndSwap(jsword *w, jsword ov, jsword nv)
  250. {
  251. return (*w == ov) ? *w = nv, JS_TRUE : JS_FALSE;
  252. }
  253. #endif
  254. JS_END_EXTERN_C
  255. #endif /* jslock_h___ */