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/. */
5
6/* GLOBAL FUNCTIONS:
7** DESCRIPTION:
8** PR Atomic operations
9*/
10
11#ifndef pratom_h___
12#define pratom_h___
13
14#include "prtypes.h"
15#include "prlock.h"
16
17PR_BEGIN_EXTERN_C
18
19/*
20** FUNCTION: PR_AtomicIncrement
21** DESCRIPTION:
22** Atomically increment a 32 bit value.
23** INPUTS:
24** val: a pointer to the value to increment
25** RETURN:
26** the returned value is the result of the increment
27*/
28NSPR_API(PRInt32) PR_AtomicIncrement(PRInt32 *val);
29
30/*
31** FUNCTION: PR_AtomicDecrement
32** DESCRIPTION:
33** Atomically decrement a 32 bit value.
34** INPUTS:
35** val: a pointer to the value to decrement
36** RETURN:
37** the returned value is the result of the decrement
38*/
39NSPR_API(PRInt32) PR_AtomicDecrement(PRInt32 *val);
40
41/*
42** FUNCTION: PR_AtomicSet
43** DESCRIPTION:
44** Atomically set a 32 bit value.
45** INPUTS:
46** val: A pointer to a 32 bit value to be set
47** newval: The newvalue to assign to val
48** RETURN:
49** Returns the prior value
50*/
51NSPR_API(PRInt32) PR_AtomicSet(PRInt32 *val, PRInt32 newval);
52
53/*
54** FUNCTION: PR_AtomicAdd
55** DESCRIPTION:
56** Atomically add a 32 bit value.
57** INPUTS:
58** ptr: a pointer to the value to increment
59** val: value to be added
60** RETURN:
61** the returned value is the result of the addition
62*/
63NSPR_API(PRInt32) PR_AtomicAdd(PRInt32 *ptr, PRInt32 val);
64
65/*
66** MACRO: PR_ATOMIC_INCREMENT
67** MACRO: PR_ATOMIC_DECREMENT
68** MACRO: PR_ATOMIC_SET
69** MACRO: PR_ATOMIC_ADD
70** DESCRIPTION:
71** Macro versions of the atomic operations. They may be implemented
72** as compiler intrinsics.
73**
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
79** interchangeably.
80*/
81#if defined(_WIN32) && !defined(_WIN32_WCE) && \
82 (!defined(_MSC_VER) || (_MSC_VER >= 1310))
83
84#include <intrin.h>
85
86#ifdef _MSC_VER
87#pragma intrinsic(_InterlockedIncrement)
88#pragma intrinsic(_InterlockedDecrement)
89#pragma intrinsic(_InterlockedExchange)
90#pragma intrinsic(_InterlockedExchangeAdd)
91#endif
92
93#define PR_ATOMIC_INCREMENT(val) _InterlockedIncrement((long volatile *)(val))
94#define PR_ATOMIC_DECREMENT(val) _InterlockedDecrement((long volatile *)(val))
95#define PR_ATOMIC_SET(val, newval) \
96 _InterlockedExchange((long volatile *)(val), (long)(newval))
97#define PR_ATOMIC_ADD(ptr, val) \
98 (_InterlockedExchangeAdd((long volatile *)(ptr), (long)(val)) + (val))
99
100#elif ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) && \
101 ((defined(__APPLE__) && \
102 (defined(__ppc__) || defined(__i386__) || defined(__x86_64__))) || \
103 (defined(__linux__) && \
104 ((defined(__i386__) && \
105 defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)) || \
106 defined(__ia64__) || defined(__x86_64__) || \
107 defined(__powerpc__) || \
108 (defined(__arm__) && \
109 defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)) || \
110 defined(__aarch64__) || defined(__alpha) || \
111 (defined(__mips__) && \
112 defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)))))
113
114/*
115 * Because the GCC manual warns that some processors may support
116 * reduced functionality of __sync_lock_test_and_set, we test for the
117 * processors that we believe support a full atomic exchange operation.
118 */
119
120#define PR_ATOMIC_INCREMENT(val) __sync_add_and_fetch(val, 1)
121#define PR_ATOMIC_DECREMENT(val) __sync_sub_and_fetch(val, 1)
122#define PR_ATOMIC_SET(val, newval) __sync_lock_test_and_set(val, newval)
123#define PR_ATOMIC_ADD(ptr, val) __sync_add_and_fetch(ptr, val)
124
125#else
126
127#define PR_ATOMIC_INCREMENT(val) PR_AtomicIncrement(val)
128#define PR_ATOMIC_DECREMENT(val) PR_AtomicDecrement(val)
129#define PR_ATOMIC_SET(val, newval) PR_AtomicSet(val, newval)
130#define PR_ATOMIC_ADD(ptr, val) PR_AtomicAdd(ptr, val)
131
132#endif
133
134/*
135** LIFO linked-list (stack)
136*/
137typedef struct PRStackElemStr PRStackElem;
138
139struct PRStackElemStr {
140 PRStackElem *prstk_elem_next; /* next pointer MUST be at offset 0;
141 assembly language code relies on this */
142};
143
144typedef struct PRStackStr PRStack;
145
146/*
147** FUNCTION: PR_CreateStack
148** DESCRIPTION:
149** Create a stack, a LIFO linked list
150** INPUTS:
151** stack_name: a pointer to string containing the name of the stack
152** RETURN:
153** A pointer to the created stack, if successful, else NULL.
154*/
155NSPR_API(PRStack *) PR_CreateStack(const char *stack_name);
156
157/*
158** FUNCTION: PR_StackPush
159** DESCRIPTION:
160** Push an element on the top of the stack
161** INPUTS:
162** stack: pointer to the stack
163** stack_elem: pointer to the stack element
164** RETURN:
165** None
166*/
167NSPR_API(void) PR_StackPush(PRStack *stack, PRStackElem *stack_elem);
168
169/*
170** FUNCTION: PR_StackPop
171** DESCRIPTION:
172** Remove the element on the top of the stack
173** INPUTS:
174** stack: pointer to the stack
175** RETURN:
176** A pointer to the stack element removed from the top of the stack,
177** if non-empty,
178** else NULL
179*/
180NSPR_API(PRStackElem *) PR_StackPop(PRStack *stack);
181
182/*
183** FUNCTION: PR_DestroyStack
184** DESCRIPTION:
185** Destroy the stack
186** INPUTS:
187** stack: pointer to the stack
188** RETURN:
189** PR_SUCCESS - if successfully deleted
190** PR_FAILURE - if the stack is not empty
191** PR_GetError will return
192** PR_INVALID_STATE_ERROR - stack is not empty
193*/
194NSPR_API(PRStatus) PR_DestroyStack(PRStack *stack);
195
196PR_END_EXTERN_C
197
198#endif /* pratom_h___ */
199

source code of include/nspr/pratom.h