1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 ** PR Atomic operations
20 ** FUNCTION: PR_AtomicIncrement
22 ** Atomically increment a 32 bit value.
24 ** val: a pointer to the value to increment
26 ** the returned value is the result of the increment
28 NSPR_API(PRInt32
) PR_AtomicIncrement(PRInt32
*val
);
31 ** FUNCTION: PR_AtomicDecrement
33 ** Atomically decrement a 32 bit value.
35 ** val: a pointer to the value to decrement
37 ** the returned value is the result of the decrement
39 NSPR_API(PRInt32
) PR_AtomicDecrement(PRInt32
*val
);
42 ** FUNCTION: PR_AtomicSet
44 ** Atomically set a 32 bit value.
46 ** val: A pointer to a 32 bit value to be set
47 ** newval: The newvalue to assign to val
49 ** Returns the prior value
51 NSPR_API(PRInt32
) PR_AtomicSet(PRInt32
*val
, PRInt32 newval
);
54 ** FUNCTION: PR_AtomicAdd
56 ** Atomically add a 32 bit value.
58 ** ptr: a pointer to the value to increment
59 ** val: value to be added
61 ** the returned value is the result of the addition
63 NSPR_API(PRInt32
) PR_AtomicAdd(PRInt32
*ptr
, PRInt32 val
);
66 ** MACRO: PR_ATOMIC_INCREMENT
67 ** MACRO: PR_ATOMIC_DECREMENT
68 ** MACRO: PR_ATOMIC_SET
69 ** MACRO: PR_ATOMIC_ADD
71 ** Macro versions of the atomic operations. They may be implemented
72 ** as compiler intrinsics.
74 ** IMPORTANT NOTE TO NSPR MAINTAINERS:
75 ** Implement these macros with compiler intrinsics only on platforms
76 ** where the PR_AtomicXXX functions are truly atomic (i.e., where the
77 ** configuration macro _PR_HAVE_ATOMIC_OPS is defined). Otherwise,
78 ** the macros and functions won't be compatible and can't be used
81 #if defined(_WIN32) && !defined(_WIN32_WCE) && \
82 (!defined(_MSC_VER) || (_MSC_VER >= 1310))
84 long __cdecl
_InterlockedIncrement(long volatile *Addend
);
85 long __cdecl
_InterlockedDecrement(long volatile *Addend
);
86 long __cdecl
_InterlockedExchange(long volatile *Target
, long Value
);
87 long __cdecl
_InterlockedExchangeAdd(long volatile *Addend
, long Value
);
90 #pragma intrinsic(_InterlockedIncrement)
91 #pragma intrinsic(_InterlockedDecrement)
92 #pragma intrinsic(_InterlockedExchange)
93 #pragma intrinsic(_InterlockedExchangeAdd)
96 #define PR_ATOMIC_INCREMENT(val) _InterlockedIncrement((long volatile *)(val))
97 #define PR_ATOMIC_DECREMENT(val) _InterlockedDecrement((long volatile *)(val))
98 #define PR_ATOMIC_SET(val, newval) \
99 _InterlockedExchange((long volatile *)(val), (long)(newval))
100 #define PR_ATOMIC_ADD(ptr, val) \
101 (_InterlockedExchangeAdd((long volatile *)(ptr), (long)(val)) + (val))
103 #elif ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) && \
104 ((defined(__APPLE__) && \
105 (defined(__ppc__) || defined(__i386__) || defined(__x86_64__))) || \
106 (defined(__linux__) && \
107 ((defined(__i386__) && \
108 defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)) || \
109 defined(__ia64__) || defined(__x86_64__) || \
110 (defined(__powerpc__) && !defined(__powerpc64__)) || \
111 (defined(__arm__) && \
112 defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)) || \
116 * Because the GCC manual warns that some processors may support
117 * reduced functionality of __sync_lock_test_and_set, we test for the
118 * processors that we believe support a full atomic exchange operation.
121 #define PR_ATOMIC_INCREMENT(val) __sync_add_and_fetch(val, 1)
122 #define PR_ATOMIC_DECREMENT(val) __sync_sub_and_fetch(val, 1)
123 #define PR_ATOMIC_SET(val, newval) __sync_lock_test_and_set(val, newval)
124 #define PR_ATOMIC_ADD(ptr, val) __sync_add_and_fetch(ptr, val)
128 #define PR_ATOMIC_INCREMENT(val) PR_AtomicIncrement(val)
129 #define PR_ATOMIC_DECREMENT(val) PR_AtomicDecrement(val)
130 #define PR_ATOMIC_SET(val, newval) PR_AtomicSet(val, newval)
131 #define PR_ATOMIC_ADD(ptr, val) PR_AtomicAdd(ptr, val)
136 ** LIFO linked-list (stack)
138 typedef struct PRStackElemStr PRStackElem
;
140 struct PRStackElemStr
{
141 PRStackElem
*prstk_elem_next
; /* next pointer MUST be at offset 0;
142 assembly language code relies on this */
145 typedef struct PRStackStr PRStack
;
148 ** FUNCTION: PR_CreateStack
150 ** Create a stack, a LIFO linked list
152 ** stack_name: a pointer to string containing the name of the stack
154 ** A pointer to the created stack, if successful, else NULL.
156 NSPR_API(PRStack
*) PR_CreateStack(const char *stack_name
);
159 ** FUNCTION: PR_StackPush
161 ** Push an element on the top of the stack
163 ** stack: pointer to the stack
164 ** stack_elem: pointer to the stack element
168 NSPR_API(void) PR_StackPush(PRStack
*stack
, PRStackElem
*stack_elem
);
171 ** FUNCTION: PR_StackPop
173 ** Remove the element on the top of the stack
175 ** stack: pointer to the stack
177 ** A pointer to the stack element removed from the top of the stack,
181 NSPR_API(PRStackElem
*) PR_StackPop(PRStack
*stack
);
184 ** FUNCTION: PR_DestroyStack
188 ** stack: pointer to the stack
190 ** PR_SUCCESS - if successfully deleted
191 ** PR_FAILURE - if the stack is not empty
192 ** PR_GetError will return
193 ** PR_INVALID_STATE_ERROR - stack is not empty
195 NSPR_API(PRStatus
) PR_DestroyStack(PRStack
*stack
);
199 #endif /* pratom_h___ */