1251875Speter/* Licensed to the Apache Software Foundation (ASF) under one or more
2251875Speter * contributor license agreements.  See the NOTICE file distributed with
3251875Speter * this work for additional information regarding copyright ownership.
4251875Speter * The ASF licenses this file to You under the Apache License, Version 2.0
5251875Speter * (the "License"); you may not use this file except in compliance with
6251875Speter * the License.  You may obtain a copy of the License at
7251875Speter *
8251875Speter *     http://www.apache.org/licenses/LICENSE-2.0
9251875Speter *
10251875Speter * Unless required by applicable law or agreed to in writing, software
11251875Speter * distributed under the License is distributed on an "AS IS" BASIS,
12251875Speter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13251875Speter * See the License for the specific language governing permissions and
14251875Speter * limitations under the License.
15251875Speter */
16251875Speter
17251875Speter#ifndef APR_ATOMIC_H
18251875Speter#define APR_ATOMIC_H
19251875Speter
20251875Speter/**
21251875Speter * @file apr_atomic.h
22251875Speter * @brief APR Atomic Operations
23251875Speter */
24251875Speter
25251875Speter#include "apr.h"
26251875Speter#include "apr_pools.h"
27251875Speter
28251875Speter#ifdef __cplusplus
29251875Speterextern "C" {
30251875Speter#endif
31251875Speter
32251875Speter/**
33251875Speter * @defgroup apr_atomic Atomic Operations
34251875Speter * @ingroup APR
35251875Speter * @{
36251875Speter */
37251875Speter
38251875Speter/**
39251875Speter * this function is required on some platforms to initialize the
40251875Speter * atomic operation's internal structures
41251875Speter * @param p pool
42251875Speter * @return APR_SUCCESS on successful completion
43251875Speter * @remark Programs do NOT need to call this directly. APR will call this
44362181Sdim *         automatically from apr_initialize().
45251875Speter * @internal
46251875Speter */
47251875SpeterAPR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p);
48251875Speter
49251875Speter/*
50251875Speter * Atomic operations on 32-bit values
51251875Speter * Note: Each of these functions internally implements a memory barrier
52251875Speter * on platforms that require it
53251875Speter */
54251875Speter
55251875Speter/**
56251875Speter * atomically read an apr_uint32_t from memory
57251875Speter * @param mem the pointer
58251875Speter */
59251875SpeterAPR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem);
60251875Speter
61251875Speter/**
62251875Speter * atomically set an apr_uint32_t in memory
63251875Speter * @param mem pointer to the object
64251875Speter * @param val value that the object will assume
65251875Speter */
66251875SpeterAPR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val);
67251875Speter
68251875Speter/**
69251875Speter * atomically add 'val' to an apr_uint32_t
70251875Speter * @param mem pointer to the object
71251875Speter * @param val amount to add
72251875Speter * @return old value pointed to by mem
73251875Speter */
74251875SpeterAPR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val);
75251875Speter
76251875Speter/**
77251875Speter * atomically subtract 'val' from an apr_uint32_t
78251875Speter * @param mem pointer to the object
79251875Speter * @param val amount to subtract
80251875Speter */
81251875SpeterAPR_DECLARE(void) apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val);
82251875Speter
83251875Speter/**
84251875Speter * atomically increment an apr_uint32_t by 1
85251875Speter * @param mem pointer to the object
86251875Speter * @return old value pointed to by mem
87251875Speter */
88251875SpeterAPR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem);
89251875Speter
90251875Speter/**
91251875Speter * atomically decrement an apr_uint32_t by 1
92251875Speter * @param mem pointer to the atomic value
93251875Speter * @return zero if the value becomes zero on decrement, otherwise non-zero
94251875Speter */
95251875SpeterAPR_DECLARE(int) apr_atomic_dec32(volatile apr_uint32_t *mem);
96251875Speter
97251875Speter/**
98251875Speter * compare an apr_uint32_t's value with 'cmp'.
99251875Speter * If they are the same swap the value with 'with'
100251875Speter * @param mem pointer to the value
101251875Speter * @param with what to swap it with
102251875Speter * @param cmp the value to compare it to
103251875Speter * @return the old value of *mem
104251875Speter */
105251875SpeterAPR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with,
106251875Speter                              apr_uint32_t cmp);
107251875Speter
108251875Speter/**
109251875Speter * exchange an apr_uint32_t's value with 'val'.
110251875Speter * @param mem pointer to the value
111251875Speter * @param val what to swap it with
112251875Speter * @return the old value of *mem
113251875Speter */
114251875SpeterAPR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val);
115251875Speter
116362181Sdim/*
117362181Sdim * Atomic operations on 64-bit values
118362181Sdim * Note: Each of these functions internally implements a memory barrier
119362181Sdim * on platforms that require it
120362181Sdim */
121362181Sdim
122251875Speter/**
123362181Sdim * atomically read an apr_uint64_t from memory
124362181Sdim * @param mem the pointer
125362181Sdim */
126362181SdimAPR_DECLARE(apr_uint64_t) apr_atomic_read64(volatile apr_uint64_t *mem);
127362181Sdim
128362181Sdim/**
129362181Sdim * atomically set an apr_uint64_t in memory
130362181Sdim * @param mem pointer to the object
131362181Sdim * @param val value that the object will assume
132362181Sdim */
133362181SdimAPR_DECLARE(void) apr_atomic_set64(volatile apr_uint64_t *mem, apr_uint64_t val);
134362181Sdim
135362181Sdim/**
136362181Sdim * atomically add 'val' to an apr_uint64_t
137362181Sdim * @param mem pointer to the object
138362181Sdim * @param val amount to add
139362181Sdim * @return old value pointed to by mem
140362181Sdim */
141362181SdimAPR_DECLARE(apr_uint64_t) apr_atomic_add64(volatile apr_uint64_t *mem, apr_uint64_t val);
142362181Sdim
143362181Sdim/**
144362181Sdim * atomically subtract 'val' from an apr_uint64_t
145362181Sdim * @param mem pointer to the object
146362181Sdim * @param val amount to subtract
147362181Sdim */
148362181SdimAPR_DECLARE(void) apr_atomic_sub64(volatile apr_uint64_t *mem, apr_uint64_t val);
149362181Sdim
150362181Sdim/**
151362181Sdim * atomically increment an apr_uint64_t by 1
152362181Sdim * @param mem pointer to the object
153362181Sdim * @return old value pointed to by mem
154362181Sdim */
155362181SdimAPR_DECLARE(apr_uint64_t) apr_atomic_inc64(volatile apr_uint64_t *mem);
156362181Sdim
157362181Sdim/**
158362181Sdim * atomically decrement an apr_uint64_t by 1
159362181Sdim * @param mem pointer to the atomic value
160362181Sdim * @return zero if the value becomes zero on decrement, otherwise non-zero
161362181Sdim */
162362181SdimAPR_DECLARE(int) apr_atomic_dec64(volatile apr_uint64_t *mem);
163362181Sdim
164362181Sdim/**
165362181Sdim * compare an apr_uint64_t's value with 'cmp'.
166362181Sdim * If they are the same swap the value with 'with'
167362181Sdim * @param mem pointer to the value
168362181Sdim * @param with what to swap it with
169362181Sdim * @param cmp the value to compare it to
170362181Sdim * @return the old value of *mem
171362181Sdim */
172362181SdimAPR_DECLARE(apr_uint64_t) apr_atomic_cas64(volatile apr_uint64_t *mem, apr_uint64_t with,
173362181Sdim                              apr_uint64_t cmp);
174362181Sdim
175362181Sdim/**
176362181Sdim * exchange an apr_uint64_t's value with 'val'.
177362181Sdim * @param mem pointer to the value
178362181Sdim * @param val what to swap it with
179362181Sdim * @return the old value of *mem
180362181Sdim */
181362181SdimAPR_DECLARE(apr_uint64_t) apr_atomic_xchg64(volatile apr_uint64_t *mem, apr_uint64_t val);
182362181Sdim
183362181Sdim/**
184251875Speter * compare the pointer's value with cmp.
185251875Speter * If they are the same swap the value with 'with'
186251875Speter * @param mem pointer to the pointer
187251875Speter * @param with what to swap it with
188251875Speter * @param cmp the value to compare it to
189251875Speter * @return the old value of the pointer
190251875Speter */
191251875SpeterAPR_DECLARE(void*) apr_atomic_casptr(volatile void **mem, void *with, const void *cmp);
192251875Speter
193251875Speter/**
194251875Speter * exchange a pair of pointer values
195251875Speter * @param mem pointer to the pointer
196251875Speter * @param with what to swap it with
197251875Speter * @return the old value of the pointer
198251875Speter */
199251875SpeterAPR_DECLARE(void*) apr_atomic_xchgptr(volatile void **mem, void *with);
200251875Speter
201251875Speter/** @} */
202251875Speter
203251875Speter#ifdef __cplusplus
204251875Speter}
205251875Speter#endif
206251875Speter
207251875Speter#endif	/* !APR_ATOMIC_H */
208