00001 /* Licensed to the Apache Software Foundation (ASF) under one or more 00002 * contributor license agreements. See the NOTICE file distributed with 00003 * this work for additional information regarding copyright ownership. 00004 * The ASF licenses this file to You under the Apache License, Version 2.0 00005 * (the "License"); you may not use this file except in compliance with 00006 * the License. You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef APR_ATOMIC_H 00018 #define APR_ATOMIC_H 00019 00020 /** 00021 * @file apr_atomic.h 00022 * @brief APR Atomic Operations 00023 */ 00024 00025 #include "apr.h" 00026 #include "apr_pools.h" 00027 00028 #ifdef __cplusplus 00029 extern "C" { 00030 #endif 00031 00032 /** 00033 * @defgroup apr_atomic Atomic Operations 00034 * @ingroup APR 00035 * @{ 00036 */ 00037 00038 /** 00039 * this function is required on some platforms to initialize the 00040 * atomic operation's internal structures 00041 * @param p pool 00042 * @return APR_SUCCESS on successful completion 00043 * @remark Programs do NOT need to call this directly. APR will call this 00044 * automatically from apr_initialize. 00045 * @internal 00046 */ 00047 APR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p); 00048 00049 /* 00050 * Atomic operations on 32-bit values 00051 * Note: Each of these functions internally implements a memory barrier 00052 * on platforms that require it 00053 */ 00054 00055 /** 00056 * atomically read an apr_uint32_t from memory 00057 * @param mem the pointer 00058 */ 00059 APR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem); 00060 00061 /** 00062 * atomically set an apr_uint32_t in memory 00063 * @param mem pointer to the object 00064 * @param val value that the object will assume 00065 */ 00066 APR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val); 00067 00068 /** 00069 * atomically add 'val' to an apr_uint32_t 00070 * @param mem pointer to the object 00071 * @param val amount to add 00072 * @return old value pointed to by mem 00073 */ 00074 APR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val); 00075 00076 /** 00077 * atomically subtract 'val' from an apr_uint32_t 00078 * @param mem pointer to the object 00079 * @param val amount to subtract 00080 */ 00081 APR_DECLARE(void) apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val); 00082 00083 /** 00084 * atomically increment an apr_uint32_t by 1 00085 * @param mem pointer to the object 00086 * @return old value pointed to by mem 00087 */ 00088 APR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem); 00089 00090 /** 00091 * atomically decrement an apr_uint32_t by 1 00092 * @param mem pointer to the atomic value 00093 * @return zero if the value becomes zero on decrement, otherwise non-zero 00094 */ 00095 APR_DECLARE(int) apr_atomic_dec32(volatile apr_uint32_t *mem); 00096 00097 /** 00098 * compare an apr_uint32_t's value with 'cmp'. 00099 * If they are the same swap the value with 'with' 00100 * @param mem pointer to the value 00101 * @param with what to swap it with 00102 * @param cmp the value to compare it to 00103 * @return the old value of *mem 00104 */ 00105 APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with, 00106 apr_uint32_t cmp); 00107 00108 /** 00109 * exchange an apr_uint32_t's value with 'val'. 00110 * @param mem pointer to the value 00111 * @param val what to swap it with 00112 * @return the old value of *mem 00113 */ 00114 APR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val); 00115 00116 /** 00117 * compare the pointer's value with cmp. 00118 * If they are the same swap the value with 'with' 00119 * @param mem pointer to the pointer 00120 * @param with what to swap it with 00121 * @param cmp the value to compare it to 00122 * @return the old value of the pointer 00123 */ 00124 APR_DECLARE(void*) apr_atomic_casptr(volatile void **mem, void *with, const void *cmp); 00125 00126 /** 00127 * exchange a pair of pointer values 00128 * @param mem pointer to the pointer 00129 * @param with what to swap it with 00130 * @return the old value of the pointer 00131 */ 00132 APR_DECLARE(void*) apr_atomic_xchgptr(volatile void **mem, void *with); 00133 00134 /** @} */ 00135 00136 #ifdef __cplusplus 00137 } 00138 #endif 00139 00140 #endif /* !APR_ATOMIC_H */