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 */ 00044 APR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p); 00045 00046 /* 00047 * Atomic operations on 32-bit values 00048 * Note: Each of these functions internally implements a memory barrier 00049 * on platforms that require it 00050 */ 00051 00052 /** 00053 * atomically read an apr_uint32_t from memory 00054 * @param mem the pointer 00055 */ 00056 APR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem); 00057 00058 /** 00059 * atomically set an apr_uint32_t in memory 00060 * @param mem pointer to the object 00061 * @param val value that the object will assume 00062 */ 00063 APR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val); 00064 00065 /** 00066 * atomically add 'val' to an apr_uint32_t 00067 * @param mem pointer to the object 00068 * @param val amount to add 00069 * @return old value pointed to by mem 00070 */ 00071 APR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val); 00072 00073 /** 00074 * atomically subtract 'val' from an apr_uint32_t 00075 * @param mem pointer to the object 00076 * @param val amount to subtract 00077 */ 00078 APR_DECLARE(void) apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val); 00079 00080 /** 00081 * atomically increment an apr_uint32_t by 1 00082 * @param mem pointer to the object 00083 * @return old value pointed to by mem 00084 */ 00085 APR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem); 00086 00087 /** 00088 * atomically decrement an apr_uint32_t by 1 00089 * @param mem pointer to the atomic value 00090 * @return zero if the value becomes zero on decrement, otherwise non-zero 00091 */ 00092 APR_DECLARE(int) apr_atomic_dec32(volatile apr_uint32_t *mem); 00093 00094 /** 00095 * compare an apr_uint32_t's value with 'cmp'. 00096 * If they are the same swap the value with 'with' 00097 * @param mem pointer to the value 00098 * @param with what to swap it with 00099 * @param cmp the value to compare it to 00100 * @return the old value of *mem 00101 */ 00102 APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with, 00103 apr_uint32_t cmp); 00104 00105 /** 00106 * exchange an apr_uint32_t's value with 'val'. 00107 * @param mem pointer to the value 00108 * @param val what to swap it with 00109 * @return the old value of *mem 00110 */ 00111 APR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val); 00112 00113 /** 00114 * compare the pointer's value with cmp. 00115 * If they are the same swap the value with 'with' 00116 * @param mem pointer to the pointer 00117 * @param with what to swap it with 00118 * @param cmp the value to compare it to 00119 * @return the old value of the pointer 00120 */ 00121 APR_DECLARE(void*) apr_atomic_casptr(volatile void **mem, void *with, const void *cmp); 00122 00123 /** @} */ 00124 00125 #ifdef __cplusplus 00126 } 00127 #endif 00128 00129 #endif /* !APR_ATOMIC_H */