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