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 29 extern "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 */ 44 APR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p); 45 46 /* 47 * Atomic operations on 32-bit values 48 * Note: Each of these functions internally implements a memory barrier 49 * on platforms that require it 50 */ 51 52 /** 53 * atomically read an apr_uint32_t from memory 54 * @param mem the pointer 55 */ 56 APR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem); 57 58 /** 59 * atomically set an apr_uint32_t in memory 60 * @param mem pointer to the object 61 * @param val value that the object will assume 62 */ 63 APR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val); 64 65 /** 66 * atomically add 'val' to an apr_uint32_t 67 * @param mem pointer to the object 68 * @param val amount to add 69 * @return old value pointed to by mem 70 */ 71 APR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val); 72 73 /** 74 * atomically subtract 'val' from an apr_uint32_t 75 * @param mem pointer to the object 76 * @param val amount to subtract 77 */ 78 APR_DECLARE(void) apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val); 79 80 /** 81 * atomically increment an apr_uint32_t by 1 82 * @param mem pointer to the object 83 * @return old value pointed to by mem 84 */ 85 APR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem); 86 87 /** 88 * atomically decrement an apr_uint32_t by 1 89 * @param mem pointer to the atomic value 90 * @return zero if the value becomes zero on decrement, otherwise non-zero 91 */ 92 APR_DECLARE(int) apr_atomic_dec32(volatile apr_uint32_t *mem); 93 94 /** 95 * compare an apr_uint32_t's value with 'cmp'. 96 * If they are the same swap the value with 'with' 97 * @param mem pointer to the value 98 * @param with what to swap it with 99 * @param cmp the value to compare it to 100 * @return the old value of *mem 101 */ 102 APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with, 103 apr_uint32_t cmp); 104 105 /** 106 * exchange an apr_uint32_t's value with 'val'. 107 * @param mem pointer to the value 108 * @param val what to swap it with 109 * @return the old value of *mem 110 */ 111 APR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val); 112 113 /** 114 * compare the pointer's value with cmp. 115 * If they are the same swap the value with 'with' 116 * @param mem pointer to the pointer 117 * @param with what to swap it with 118 * @param cmp the value to compare it to 119 * @return the old value of the pointer 120 */ 121 APR_DECLARE(void*) apr_atomic_casptr(volatile void **mem, void *with, const void *cmp); 122 123 /** @} */ 124 125 #ifdef __cplusplus 126 } 127 #endif 128 129 #endif /* !APR_ATOMIC_H */ 130