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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * @brief APR Atomic Operations
26 #include "apr_pools.h"
33 * @defgroup apr_atomic Atomic Operations
39 * this function is required on some platforms to initialize the
40 * atomic operation's internal structures
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().
47 APR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p);
50 * Atomic operations on 32-bit values
51 * Note: Each of these functions internally implements a memory barrier
52 * on platforms that require it
56 * atomically read an apr_uint32_t from memory
57 * @param mem the pointer
59 APR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem);
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
66 APR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val);
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
74 APR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val);
77 * atomically subtract 'val' from an apr_uint32_t
78 * @param mem pointer to the object
79 * @param val amount to subtract
81 APR_DECLARE(void) apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val);
84 * atomically increment an apr_uint32_t by 1
85 * @param mem pointer to the object
86 * @return old value pointed to by mem
88 APR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem);
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
95 APR_DECLARE(int) apr_atomic_dec32(volatile apr_uint32_t *mem);
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
105 APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with,
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
114 APR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val);
117 * Atomic operations on 64-bit values
118 * Note: Each of these functions internally implements a memory barrier
119 * on platforms that require it
123 * atomically read an apr_uint64_t from memory
124 * @param mem the pointer
126 APR_DECLARE(apr_uint64_t) apr_atomic_read64(volatile apr_uint64_t *mem);
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
133 APR_DECLARE(void) apr_atomic_set64(volatile apr_uint64_t *mem, apr_uint64_t val);
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
141 APR_DECLARE(apr_uint64_t) apr_atomic_add64(volatile apr_uint64_t *mem, apr_uint64_t val);
144 * atomically subtract 'val' from an apr_uint64_t
145 * @param mem pointer to the object
146 * @param val amount to subtract
148 APR_DECLARE(void) apr_atomic_sub64(volatile apr_uint64_t *mem, apr_uint64_t val);
151 * atomically increment an apr_uint64_t by 1
152 * @param mem pointer to the object
153 * @return old value pointed to by mem
155 APR_DECLARE(apr_uint64_t) apr_atomic_inc64(volatile apr_uint64_t *mem);
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
162 APR_DECLARE(int) apr_atomic_dec64(volatile apr_uint64_t *mem);
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
172 APR_DECLARE(apr_uint64_t) apr_atomic_cas64(volatile apr_uint64_t *mem, apr_uint64_t with,
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
181 APR_DECLARE(apr_uint64_t) apr_atomic_xchg64(volatile apr_uint64_t *mem, apr_uint64_t val);
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
191 APR_DECLARE(void*) apr_atomic_casptr(volatile void **mem, void *with, const void *cmp);
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
199 APR_DECLARE(void*) apr_atomic_xchgptr(volatile void **mem, void *with);
207 #endif /* !APR_ATOMIC_H */