1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef APR_ATOMIC_H
18#define APR_ATOMIC_H
19
20/**
21 * @file apr_atomic.h
22 * @brief APR Atomic Operations
23 */
24
25#include "apr.h"
26#include "apr_pools.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/**
33 * @defgroup apr_atomic Atomic Operations
34 * @ingroup APR
35 * @{
36 */
37
38/**
39 * this function is required on some platforms to initialize the
40 * atomic operation's internal structures
41 * @param p pool
42 * @return APR_SUCCESS on successful completion
43 * @remark Programs do NOT need to call this directly. APR will call this
44 *         automatically from apr_initialize().
45 * @internal
46 */
47APR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p);
48
49/*
50 * Atomic operations on 32-bit values
51 * Note: Each of these functions internally implements a memory barrier
52 * on platforms that require it
53 */
54
55/**
56 * atomically read an apr_uint32_t from memory
57 * @param mem the pointer
58 */
59APR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem);
60
61/**
62 * atomically set an apr_uint32_t in memory
63 * @param mem pointer to the object
64 * @param val value that the object will assume
65 */
66APR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val);
67
68/**
69 * atomically add 'val' to an apr_uint32_t
70 * @param mem pointer to the object
71 * @param val amount to add
72 * @return old value pointed to by mem
73 */
74APR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val);
75
76/**
77 * atomically subtract 'val' from an apr_uint32_t
78 * @param mem pointer to the object
79 * @param val amount to subtract
80 */
81APR_DECLARE(void) apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val);
82
83/**
84 * atomically increment an apr_uint32_t by 1
85 * @param mem pointer to the object
86 * @return old value pointed to by mem
87 */
88APR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem);
89
90/**
91 * atomically decrement an apr_uint32_t by 1
92 * @param mem pointer to the atomic value
93 * @return zero if the value becomes zero on decrement, otherwise non-zero
94 */
95APR_DECLARE(int) apr_atomic_dec32(volatile apr_uint32_t *mem);
96
97/**
98 * compare an apr_uint32_t's value with 'cmp'.
99 * If they are the same swap the value with 'with'
100 * @param mem pointer to the value
101 * @param with what to swap it with
102 * @param cmp the value to compare it to
103 * @return the old value of *mem
104 */
105APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with,
106                              apr_uint32_t cmp);
107
108/**
109 * exchange an apr_uint32_t's value with 'val'.
110 * @param mem pointer to the value
111 * @param val what to swap it with
112 * @return the old value of *mem
113 */
114APR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val);
115
116/*
117 * Atomic operations on 64-bit values
118 * Note: Each of these functions internally implements a memory barrier
119 * on platforms that require it
120 */
121
122/**
123 * atomically read an apr_uint64_t from memory
124 * @param mem the pointer
125 */
126APR_DECLARE(apr_uint64_t) apr_atomic_read64(volatile apr_uint64_t *mem);
127
128/**
129 * atomically set an apr_uint64_t in memory
130 * @param mem pointer to the object
131 * @param val value that the object will assume
132 */
133APR_DECLARE(void) apr_atomic_set64(volatile apr_uint64_t *mem, apr_uint64_t val);
134
135/**
136 * atomically add 'val' to an apr_uint64_t
137 * @param mem pointer to the object
138 * @param val amount to add
139 * @return old value pointed to by mem
140 */
141APR_DECLARE(apr_uint64_t) apr_atomic_add64(volatile apr_uint64_t *mem, apr_uint64_t val);
142
143/**
144 * atomically subtract 'val' from an apr_uint64_t
145 * @param mem pointer to the object
146 * @param val amount to subtract
147 */
148APR_DECLARE(void) apr_atomic_sub64(volatile apr_uint64_t *mem, apr_uint64_t val);
149
150/**
151 * atomically increment an apr_uint64_t by 1
152 * @param mem pointer to the object
153 * @return old value pointed to by mem
154 */
155APR_DECLARE(apr_uint64_t) apr_atomic_inc64(volatile apr_uint64_t *mem);
156
157/**
158 * atomically decrement an apr_uint64_t by 1
159 * @param mem pointer to the atomic value
160 * @return zero if the value becomes zero on decrement, otherwise non-zero
161 */
162APR_DECLARE(int) apr_atomic_dec64(volatile apr_uint64_t *mem);
163
164/**
165 * compare an apr_uint64_t's value with 'cmp'.
166 * If they are the same swap the value with 'with'
167 * @param mem pointer to the value
168 * @param with what to swap it with
169 * @param cmp the value to compare it to
170 * @return the old value of *mem
171 */
172APR_DECLARE(apr_uint64_t) apr_atomic_cas64(volatile apr_uint64_t *mem, apr_uint64_t with,
173                              apr_uint64_t cmp);
174
175/**
176 * exchange an apr_uint64_t's value with 'val'.
177 * @param mem pointer to the value
178 * @param val what to swap it with
179 * @return the old value of *mem
180 */
181APR_DECLARE(apr_uint64_t) apr_atomic_xchg64(volatile apr_uint64_t *mem, apr_uint64_t val);
182
183/**
184 * compare the pointer's value with cmp.
185 * If they are the same swap the value with 'with'
186 * @param mem pointer to the pointer
187 * @param with what to swap it with
188 * @param cmp the value to compare it to
189 * @return the old value of the pointer
190 */
191APR_DECLARE(void*) apr_atomic_casptr(volatile void **mem, void *with, const void *cmp);
192
193/**
194 * exchange a pair of pointer values
195 * @param mem pointer to the pointer
196 * @param with what to swap it with
197 * @return the old value of the pointer
198 */
199APR_DECLARE(void*) apr_atomic_xchgptr(volatile void **mem, void *with);
200
201/** @} */
202
203#ifdef __cplusplus
204}
205#endif
206
207#endif	/* !APR_ATOMIC_H */
208