1/**
2 * @copyright
3 * ====================================================================
4 *    Licensed to the Apache Software Foundation (ASF) under one
5 *    or more contributor license agreements.  See the NOTICE file
6 *    distributed with this work for additional information
7 *    regarding copyright ownership.  The ASF licenses this file
8 *    to you under the Apache License, Version 2.0 (the
9 *    "License"); you may not use this file except in compliance
10 *    with the License.  You may obtain a copy of the License at
11 *
12 *      http://www.apache.org/licenses/LICENSE-2.0
13 *
14 *    Unless required by applicable law or agreed to in writing,
15 *    software distributed under the License is distributed on an
16 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 *    KIND, either express or implied.  See the License for the
18 *    specific language governing permissions and limitations
19 *    under the License.
20 * ====================================================================
21 * @endcopyright
22 *
23 * @file svn_atomic.h
24 * @brief Macros and functions for atomic operations
25 */
26
27#ifndef SVN_ATOMIC_H
28#define SVN_ATOMIC_H
29
30#include <apr_version.h>
31#include <apr_atomic.h>
32
33#include "svn_error.h"
34#include "private/svn_dep_compat.h"
35
36#ifdef __cplusplus
37extern "C" {
38#endif /* __cplusplus */
39
40/**
41 * @name Macro definitions for atomic types and operations
42 *
43 * @note These are necessary because the apr_atomic API changed somewhat
44 *       between apr-0.x and apr-1.x.
45 * @{
46 */
47
48/** The type used by all the other atomic operations. */
49#define svn_atomic_t apr_uint32_t
50
51/** Atomically read an #svn_atomic_t from memory. */
52#define svn_atomic_read(mem) apr_atomic_read32((mem))
53
54/** Atomically set an #svn_atomic_t in memory. */
55#define svn_atomic_set(mem, val) apr_atomic_set32((mem), (val))
56
57/** Atomically increment an #svn_atomic_t. */
58#define svn_atomic_inc(mem) apr_atomic_inc32(mem)
59
60/** Atomically decrement an #svn_atomic_t. */
61#define svn_atomic_dec(mem) apr_atomic_dec32(mem)
62
63/**
64 * Atomic compare-and-swap.
65 *
66 * Compare the value that @a mem points to with @a cmp. If they are
67 * the same swap the value with @a with.
68 *
69 * @note svn_atomic_cas should not be combined with the other
70 *       svn_atomic operations.  A comment in apr_atomic.h explains
71 *       that on some platforms, the CAS function is implemented in a
72 *       way that is incompatible with the other atomic operations.
73 */
74#define svn_atomic_cas(mem, with, cmp) \
75    apr_atomic_cas32((mem), (with), (cmp))
76/** @} */
77
78/**
79 * @name Single-threaded atomic initialization
80 * @{
81 */
82
83/**
84 * Callback for svn_atomic__init_once().
85 * @return an #svn_error_t if the initialization fails.
86 * @since New in 1.10
87 */
88typedef svn_error_t *(*svn_atomic__err_init_func_t)(void *baton,
89                                                    apr_pool_t *pool);
90
91/**
92 * Callback for svn_atomic__init_no_error().
93 * @return a string containing an error message if the initialization fails.
94 * @since New in 1.10
95 */
96typedef const char *(*svn_atomic__str_init_func_t)(void *baton);
97
98/**
99 * Call an initialization function in a thread-safe manner.
100 *
101 * @a global_status must be a pointer to a global, zero-initialized
102 * #svn_atomic_t. @a err_init_func is a pointer to the function that
103 * performs the actual initialization. @a baton and and @a pool are
104 * passed on to @a err_init_func for its use.
105 *
106 * @return the error returned by @a err_init_func.
107 *
108 * @since New in 1.5.
109 */
110svn_error_t *
111svn_atomic__init_once(volatile svn_atomic_t *global_status,
112                      svn_atomic__err_init_func_t err_init_func,
113                      void *baton,
114                      apr_pool_t* pool);
115
116/**
117 * Call an initialization function in a thread-safe manner.
118 *
119 * Unlike svn_atomic__init_once(), this function does not need a pool
120 * and does not create an #svn_error_t, and neither should the
121 * @a str_init_func implementation.
122 *
123 * @a global_status must be a pointer to a global, zero-initialized
124 * #svn_atomic_t. @a str_init_func is a pointer to the function that
125 * performs the actual initialization. @a baton is passed on to
126 * @a str_init_func for its use.
127 *
128 * @return the error string returned by @a str_init_func.
129 *
130 * @since New in 1.10.
131 */
132const char *
133svn_atomic__init_once_no_error(volatile svn_atomic_t *global_status,
134                               svn_atomic__str_init_func_t str_init_func,
135                               void *baton);
136
137
138/**
139 * Query and increment the global counter and set @a value to the new
140 * counter value.
141 *
142 * This function is thread-safe and you should call it whenever you need
143 * a number that is unique within the current process. The values are > 0.
144 *
145 * @return the error object in case of a synchronization failure.
146 *
147 * @since New in 1.10.
148 */
149svn_error_t *
150svn_atomic__unique_counter(apr_uint64_t* value);
151
152/** @} */
153
154#ifdef __cplusplus
155}
156#endif /* __cplusplus */
157
158#endif /* SVN_ATOMIC_H */
159