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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "apr_arch_atomic.h"
19 #ifdef USE_ATOMICS_GENERIC
24 # define DECLARE_MUTEX_LOCKED(name, mem) \
25 apr_thread_mutex_t *name = mutex_hash(mem)
26 # define MUTEX_UNLOCK(name) \
28 if (apr_thread_mutex_unlock(name) != APR_SUCCESS) \
32 # define DECLARE_MUTEX_LOCKED(name, mem)
33 # define MUTEX_UNLOCK(name)
34 # warning Be warned: using stubs for all atomic operations
39 static apr_thread_mutex_t **hash_mutex;
41 #define NUM_ATOMIC_HASH 7
42 /* shift by 2 to get rid of alignment issues */
43 #define ATOMIC_HASH(x) (unsigned int)(((unsigned long)(x)>>2)%(unsigned int)NUM_ATOMIC_HASH)
45 static apr_status_t atomic_cleanup(void *data)
47 if (hash_mutex == data)
53 APR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p)
58 if (hash_mutex != NULL)
61 hash_mutex = apr_palloc(p, sizeof(apr_thread_mutex_t*) * NUM_ATOMIC_HASH);
62 apr_pool_cleanup_register(p, hash_mutex, atomic_cleanup,
63 apr_pool_cleanup_null);
65 for (i = 0; i < NUM_ATOMIC_HASH; i++) {
66 rv = apr_thread_mutex_create(&(hash_mutex[i]),
67 APR_THREAD_MUTEX_DEFAULT, p);
68 if (rv != APR_SUCCESS) {
76 static APR_INLINE apr_thread_mutex_t *mutex_hash(volatile apr_uint32_t *mem)
78 apr_thread_mutex_t *mutex = hash_mutex[ATOMIC_HASH(mem)];
80 if (apr_thread_mutex_lock(mutex) != APR_SUCCESS) {
89 APR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p)
94 #endif /* APR_HAS_THREADS */
96 APR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem)
101 APR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val)
103 DECLARE_MUTEX_LOCKED(mutex, mem);
110 APR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val)
112 apr_uint32_t old_value;
113 DECLARE_MUTEX_LOCKED(mutex, mem);
123 APR_DECLARE(void) apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val)
125 DECLARE_MUTEX_LOCKED(mutex, mem);
130 APR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem)
132 return apr_atomic_add32(mem, 1);
135 APR_DECLARE(int) apr_atomic_dec32(volatile apr_uint32_t *mem)
138 DECLARE_MUTEX_LOCKED(mutex, mem);
148 APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with,
152 DECLARE_MUTEX_LOCKED(mutex, mem);
164 APR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val)
167 DECLARE_MUTEX_LOCKED(mutex, mem);
177 APR_DECLARE(void*) apr_atomic_casptr(volatile void **mem, void *with, const void *cmp)
180 DECLARE_MUTEX_LOCKED(mutex, *mem);
182 prev = *(void **)mem;
192 APR_DECLARE(void*) apr_atomic_xchgptr(volatile void **mem, void *with)
195 DECLARE_MUTEX_LOCKED(mutex, *mem);
197 prev = *(void **)mem;
205 #endif /* USE_ATOMICS_GENERIC */