bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / atomic / unix / apr_atomic.c
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 #include "apr.h"
18 #include "apr_atomic.h"
19 #include "apr_thread_mutex.h"
20
21 #if !defined(apr_atomic_init) && !defined(APR_OVERRIDE_ATOMIC_INIT)
22
23 #if APR_HAS_THREADS
24 #define NUM_ATOMIC_HASH 7
25 /* shift by 2 to get rid of alignment issues */
26 #define ATOMIC_HASH(x) (unsigned int)(((unsigned long)(x)>>2)%(unsigned int)NUM_ATOMIC_HASH)
27 static apr_thread_mutex_t **hash_mutex;
28 #endif /* APR_HAS_THREADS */
29
30 apr_status_t apr_atomic_init(apr_pool_t *p)
31 {
32 #if APR_HAS_THREADS
33     int i;
34     apr_status_t rv;
35     hash_mutex = apr_palloc(p, sizeof(apr_thread_mutex_t*) * NUM_ATOMIC_HASH);
36
37     for (i = 0; i < NUM_ATOMIC_HASH; i++) {
38         rv = apr_thread_mutex_create(&(hash_mutex[i]),
39                                      APR_THREAD_MUTEX_DEFAULT, p);
40         if (rv != APR_SUCCESS) {
41            return rv;
42         }
43     }
44 #endif /* APR_HAS_THREADS */
45     return APR_SUCCESS;
46 }
47 #endif /*!defined(apr_atomic_init) && !defined(APR_OVERRIDE_ATOMIC_INIT) */
48
49 #if !defined(apr_atomic_add) && !defined(APR_OVERRIDE_ATOMIC_ADD)
50 void apr_atomic_add(volatile apr_atomic_t *mem, apr_uint32_t val) 
51 {
52 #if APR_HAS_THREADS
53     apr_thread_mutex_t *lock = hash_mutex[ATOMIC_HASH(mem)];
54     apr_uint32_t prev;
55        
56     if (apr_thread_mutex_lock(lock) == APR_SUCCESS) {
57         prev = *mem;
58         *mem += val;
59         apr_thread_mutex_unlock(lock);
60     }
61 #else
62     *mem += val;
63 #endif /* APR_HAS_THREADS */
64 }
65 #endif /*!defined(apr_atomic_add) && !defined(APR_OVERRIDE_ATOMIC_ADD) */
66
67 #if !defined(apr_atomic_set) && !defined(APR_OVERRIDE_ATOMIC_SET)
68 void apr_atomic_set(volatile apr_atomic_t *mem, apr_uint32_t val) 
69 {
70 #if APR_HAS_THREADS
71     apr_thread_mutex_t *lock = hash_mutex[ATOMIC_HASH(mem)];
72     apr_uint32_t prev;
73
74     if (apr_thread_mutex_lock(lock) == APR_SUCCESS) {
75         prev = *mem;
76         *mem = val;
77         apr_thread_mutex_unlock(lock);
78     }
79 #else
80     *mem = val;
81 #endif /* APR_HAS_THREADS */
82 }
83 #endif /*!defined(apr_atomic_set) && !defined(APR_OVERRIDE_ATOMIC_SET) */
84
85 #if !defined(apr_atomic_inc) && !defined(APR_OVERRIDE_ATOMIC_INC)
86 void apr_atomic_inc(volatile apr_uint32_t *mem) 
87 {
88 #if APR_HAS_THREADS
89     apr_thread_mutex_t *lock = hash_mutex[ATOMIC_HASH(mem)];
90     apr_uint32_t prev;
91
92     if (apr_thread_mutex_lock(lock) == APR_SUCCESS) {
93         prev = *mem;
94         (*mem)++;
95         apr_thread_mutex_unlock(lock);
96     }
97 #else
98     (*mem)++;
99 #endif /* APR_HAS_THREADS */
100 }
101 #endif /*!defined(apr_atomic_inc) && !defined(APR_OVERRIDE_ATOMIC_INC) */
102
103 #if !defined(apr_atomic_dec) && !defined(APR_OVERRIDE_ATOMIC_DEC)
104 int apr_atomic_dec(volatile apr_atomic_t *mem) 
105 {
106 #if APR_HAS_THREADS
107     apr_thread_mutex_t *lock = hash_mutex[ATOMIC_HASH(mem)];
108     apr_uint32_t new;
109
110     if (apr_thread_mutex_lock(lock) == APR_SUCCESS) {
111         (*mem)--;
112         new = *mem;
113         apr_thread_mutex_unlock(lock);
114         return new; 
115     }
116 #else
117     (*mem)--;
118 #endif /* APR_HAS_THREADS */
119     return *mem; 
120 }
121 #endif /*!defined(apr_atomic_dec) && !defined(APR_OVERRIDE_ATOMIC_DEC) */
122
123 #if !defined(apr_atomic_cas) && !defined(APR_OVERRIDE_ATOMIC_CAS)
124 apr_uint32_t apr_atomic_cas(volatile apr_uint32_t *mem, long with, long cmp)
125 {
126     apr_uint32_t prev;
127 #if APR_HAS_THREADS
128     apr_thread_mutex_t *lock = hash_mutex[ATOMIC_HASH(mem)];
129
130     if (apr_thread_mutex_lock(lock) == APR_SUCCESS) {
131         prev = *mem;
132         if (prev == (apr_uint32_t)cmp) {
133             *mem = (apr_uint32_t)with;
134         }
135         apr_thread_mutex_unlock(lock);
136         return prev;
137     }
138     return *mem;
139 #else
140     prev = *mem;
141     if (prev == (apr_uint32_t)cmp) {
142         *mem = (apr_uint32_t)with;
143     }
144     return prev;
145 #endif /* APR_HAS_THREADS */
146 }
147 #endif /*!defined(apr_atomic_cas) && !defined(APR_OVERRIDE_ATOMIC_CAS) */
148
149 #if !defined(apr_atomic_casptr) && !defined(APR_OVERRIDE_ATOMIC_CASPTR)
150 void *apr_atomic_casptr(volatile void **mem, void *with, const void *cmp)
151 {
152     void *prev;
153 #if APR_HAS_THREADS
154     apr_thread_mutex_t *lock = hash_mutex[ATOMIC_HASH(mem)];
155
156     if (apr_thread_mutex_lock(lock) == APR_SUCCESS) {
157         prev = *(void **)mem;
158         if (prev == cmp) {
159             *mem = with;
160         }
161         apr_thread_mutex_unlock(lock);
162         return prev;
163     }
164     return *(void **)mem;
165 #else
166     prev = *(void **)mem;
167     if (prev == cmp) {
168         *mem = with;
169     }
170     return prev;
171 #endif /* APR_HAS_THREADS */
172 }
173 #endif /*!defined(apr_atomic_cas) && !defined(APR_OVERRIDE_ATOMIC_CAS) */