1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
16 * The Original Code is Mozilla Communicator client code, released
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.
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.
38 * ***** END LICENSE BLOCK ***** */
50 # include "prthread.h"
57 #if (defined(_WIN32) && defined(_M_IX86)) || \
58 (defined(_WIN64) && (defined(_M_AMD64) || defined(_M_X64))) || \
59 (defined(__i386) && (defined(__GNUC__) || defined(__SUNPRO_CC))) || \
60 (defined(__x86_64) && (defined(__GNUC__) || defined(__SUNPRO_CC))) || \
61 (defined(__sparc) && (defined(__GNUC__) || defined(__SUNPRO_CC))) || \
63 defined(USE_ARM_KUSER)
64 # define JS_HAS_NATIVE_COMPARE_AND_SWAP 1
66 # define JS_HAS_NATIVE_COMPARE_AND_SWAP 0
69 #if defined(JS_USE_ONLY_NSPR_LOCKS) || !JS_HAS_NATIVE_COMPARE_AND_SWAP
75 #define Thin_GetWait(W) ((jsword)(W) & 0x1)
76 #define Thin_SetWait(W) ((jsword)(W) | 0x1)
77 #define Thin_RemoveWait(W) ((jsword)(W) & ~0x1)
79 typedef struct JSFatLock JSFatLock;
81 typedef struct JSThinLock {
86 #define CX_THINLOCK_ID(cx) ((jsword)(cx)->thread)
87 #define CURRENT_THREAD_IS_ME(me) (((JSThread *)me)->id == js_CurrentThreadId())
89 typedef PRLock JSLock;
92 * Atomic increment and decrement for a reference counter, given jsrefcount *p.
93 * NB: jsrefcount is int32, aka PRInt32, so that pratom.h functions work.
95 #define JS_ATOMIC_INCREMENT(p) PR_AtomicIncrement((PRInt32 *)(p))
96 #define JS_ATOMIC_DECREMENT(p) PR_AtomicDecrement((PRInt32 *)(p))
97 #define JS_ATOMIC_ADD(p,v) PR_AtomicAdd((PRInt32 *)(p), (PRInt32)(v))
98 #define JS_ATOMIC_SET(p,v) PR_AtomicSet((PRInt32 *)(p), (PRInt32)(v))
100 #define js_CurrentThreadId() PR_GetCurrentThread()
101 #define JS_NEW_LOCK() PR_NewLock()
102 #define JS_DESTROY_LOCK(l) PR_DestroyLock(l)
103 #define JS_ACQUIRE_LOCK(l) PR_Lock(l)
104 #define JS_RELEASE_LOCK(l) PR_Unlock(l)
106 #define JS_NEW_CONDVAR(l) PR_NewCondVar(l)
107 #define JS_DESTROY_CONDVAR(cv) PR_DestroyCondVar(cv)
108 #define JS_WAIT_CONDVAR(cv,to) PR_WaitCondVar(cv,to)
109 #define JS_NO_TIMEOUT PR_INTERVAL_NO_TIMEOUT
110 #define JS_NOTIFY_CONDVAR(cv) PR_NotifyCondVar(cv)
111 #define JS_NOTIFY_ALL_CONDVAR(cv) PR_NotifyAllCondVar(cv)
113 #define JS_LOCK(cx, tl) js_Lock(cx, tl)
114 #define JS_UNLOCK(cx, tl) js_Unlock(cx, tl)
116 #define JS_LOCK_RUNTIME(rt) js_LockRuntime(rt)
117 #define JS_UNLOCK_RUNTIME(rt) js_UnlockRuntime(rt)
119 extern void js_Lock(JSContext *cx, JSThinLock *tl);
120 extern void js_Unlock(JSContext *cx, JSThinLock *tl);
121 extern void js_LockRuntime(JSRuntime *rt);
122 extern void js_UnlockRuntime(JSRuntime *rt);
123 extern int js_SetupLocks(int,int);
124 extern void js_CleanupLocks();
125 extern void js_InitLock(JSThinLock *);
126 extern void js_FinishLock(JSThinLock *);
130 #define JS_IS_RUNTIME_LOCKED(rt) js_IsRuntimeLocked(rt)
132 extern JSBool js_IsRuntimeLocked(JSRuntime *rt);
136 #define JS_IS_RUNTIME_LOCKED(rt) 0
140 #else /* !JS_THREADSAFE */
142 #define JS_ATOMIC_INCREMENT(p) (++*(p))
143 #define JS_ATOMIC_DECREMENT(p) (--*(p))
144 #define JS_ATOMIC_ADD(p,v) (*(p) += (v))
145 #define JS_ATOMIC_SET(p,v) (*(p) = (v))
147 #define JS_CurrentThreadId() 0
148 #define JS_NEW_LOCK() NULL
149 #define JS_DESTROY_LOCK(l) ((void)0)
150 #define JS_ACQUIRE_LOCK(l) ((void)0)
151 #define JS_RELEASE_LOCK(l) ((void)0)
152 #define JS_LOCK(cx, tl) ((void)0)
153 #define JS_UNLOCK(cx, tl) ((void)0)
155 #define JS_NEW_CONDVAR(l) NULL
156 #define JS_DESTROY_CONDVAR(cv) ((void)0)
157 #define JS_WAIT_CONDVAR(cv,to) ((void)0)
158 #define JS_NOTIFY_CONDVAR(cv) ((void)0)
159 #define JS_NOTIFY_ALL_CONDVAR(cv) ((void)0)
161 #define JS_LOCK_RUNTIME(rt) ((void)0)
162 #define JS_UNLOCK_RUNTIME(rt) ((void)0)
164 #define JS_IS_RUNTIME_LOCKED(rt) 1
166 #endif /* !JS_THREADSAFE */
168 #define JS_LOCK_RUNTIME_VOID(rt,e) \
170 JS_LOCK_RUNTIME(rt); \
172 JS_UNLOCK_RUNTIME(rt); \
175 #define JS_LOCK_GC(rt) JS_ACQUIRE_LOCK((rt)->gcLock)
176 #define JS_UNLOCK_GC(rt) JS_RELEASE_LOCK((rt)->gcLock)
177 #define JS_AWAIT_GC_DONE(rt) JS_WAIT_CONDVAR((rt)->gcDone, JS_NO_TIMEOUT)
178 #define JS_NOTIFY_GC_DONE(rt) JS_NOTIFY_ALL_CONDVAR((rt)->gcDone)
179 #define JS_AWAIT_REQUEST_DONE(rt) JS_WAIT_CONDVAR((rt)->requestDone, \
181 #define JS_NOTIFY_REQUEST_DONE(rt) JS_NOTIFY_CONDVAR((rt)->requestDone)
183 #ifndef JS_SET_OBJ_INFO
184 #define JS_SET_OBJ_INFO(obj,f,l) ((void)0)
186 #ifndef JS_SET_TITLE_INFO
187 #define JS_SET_TITLE_INFO(title,f,l) ((void)0)
193 js_CompareAndSwap(volatile jsword *w, jsword ov, jsword nv);
195 /* Atomically bitwise-or the mask into the word *w using compare and swap. */
197 js_AtomicSetMask(volatile jsword *w, jsword mask);
200 * Atomically bitwise-and the complement of the mask into the word *w using
204 js_AtomicClearMask(volatile jsword *w, jsword mask);
206 #define JS_ATOMIC_SET_MASK(w, mask) js_AtomicSetMask(w, mask)
207 #define JS_ATOMIC_CLEAR_MASK(w, mask) js_AtomicClearMask(w, mask)
212 js_CompareAndSwap(jsword *w, jsword ov, jsword nv)
214 return (*w == ov) ? *w = nv, JS_TRUE : JS_FALSE;
217 #define JS_ATOMIC_SET_MASK(w, mask) (*(w) |= (mask))
218 #define JS_ATOMIC_CLEAR_MASK(w, mask) (*(w) &= ~(mask))
220 #endif /* JS_THREADSAFE */
224 #if defined JS_THREADSAFE && defined __cplusplus
232 AutoLock(JSLock *lock) : lock(lock) { JS_ACQUIRE_LOCK(lock); }
233 ~AutoLock() { JS_RELEASE_LOCK(lock); }
239 #endif /* jslock_h___ */