Apache HTTPD
framework
httpd-2.4.62
srclib
apr
atomic
unix
mutex64.c
Go to the documentation of this file.
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_arch_atomic.h
"
18
#include "
apr_thread_mutex.h
"
19
20
#if defined(USE_ATOMICS_GENERIC64)
21
22
#include <stdlib.h>
23
24
#if APR_HAS_THREADS
25
# define DECLARE_MUTEX_LOCKED(name, mem) \
26
apr_thread_mutex_t *name = mutex_hash(mem)
27
# define MUTEX_UNLOCK(name) \
28
do { \
29
if (apr_thread_mutex_unlock(name) != APR_SUCCESS) \
30
abort(); \
31
} while (0)
32
#else
33
# define DECLARE_MUTEX_LOCKED(name, mem)
34
# define MUTEX_UNLOCK(name)
35
# warning Be warned: using stubs for all atomic operations
36
#endif
37
38
#if APR_HAS_THREADS
39
40
static
apr_thread_mutex_t
**
hash_mutex
;
41
42
#define NUM_ATOMIC_HASH 7
43
/* shift by 2 to get rid of alignment issues */
44
#define ATOMIC_HASH(x) (unsigned int)(((unsigned long)(x)>>2)%(unsigned int)NUM_ATOMIC_HASH)
45
46
static
apr_status_t
atomic_cleanup
(
void
*
data
)
47
{
48
if
(
hash_mutex
==
data
)
49
hash_mutex
=
NULL
;
50
51
return
APR_SUCCESS
;
52
}
53
54
apr_status_t
apr__atomic_generic64_init
(
apr_pool_t
*
p
)
55
{
56
int
i
;
57
apr_status_t
rv;
58
59
if
(
hash_mutex
!=
NULL
)
60
return
APR_SUCCESS
;
61
62
hash_mutex
=
apr_palloc
(
p
,
sizeof
(
apr_thread_mutex_t
*) *
NUM_ATOMIC_HASH
);
63
apr_pool_cleanup_register
(
p
,
hash_mutex
,
atomic_cleanup
,
64
apr_pool_cleanup_null
);
65
66
for
(
i
= 0;
i
<
NUM_ATOMIC_HASH
;
i
++) {
67
rv =
apr_thread_mutex_create
(&(
hash_mutex
[
i
]),
68
APR_THREAD_MUTEX_DEFAULT
,
p
);
69
if
(rv !=
APR_SUCCESS
) {
70
return
rv;
71
}
72
}
73
74
return
APR_SUCCESS
;
75
}
76
77
static
APR_INLINE
apr_thread_mutex_t
*
mutex_hash
(
volatile
apr_uint64_t
*
mem
)
78
{
79
apr_thread_mutex_t
*mutex =
hash_mutex
[
ATOMIC_HASH
(
mem
)];
80
81
if
(
apr_thread_mutex_lock
(mutex) !=
APR_SUCCESS
) {
82
abort
();
83
}
84
85
return
mutex;
86
}
87
88
#else
89
90
apr_status_t
apr__atomic_generic64_init
(
apr_pool_t
*
p
)
91
{
92
return
APR_SUCCESS
;
93
}
94
95
#endif
/* APR_HAS_THREADS */
96
97
APR_DECLARE
(
apr_uint64_t
)
apr_atomic_read64
(
volatile
apr_uint64_t
*
mem
)
98
{
99
apr_uint64_t
cur_value
;
100
DECLARE_MUTEX_LOCKED
(mutex,
mem
);
101
102
cur_value
= *
mem
;
103
104
MUTEX_UNLOCK
(mutex);
105
106
return
cur_value
;
107
}
108
109
APR_DECLARE
(
void
)
apr_atomic_set64
(
volatile
apr_uint64_t
*
mem
,
apr_uint64_t
val
)
110
{
111
DECLARE_MUTEX_LOCKED
(mutex,
mem
);
112
113
*
mem
=
val
;
114
115
MUTEX_UNLOCK
(mutex);
116
}
117
118
APR_DECLARE
(
apr_uint64_t
)
apr_atomic_add64
(
volatile
apr_uint64_t
*
mem
,
apr_uint64_t
val
)
119
{
120
apr_uint64_t
old_value
;
121
DECLARE_MUTEX_LOCKED
(mutex,
mem
);
122
123
old_value
= *
mem
;
124
*
mem
+=
val
;
125
126
MUTEX_UNLOCK
(mutex);
127
128
return
old_value
;
129
}
130
131
APR_DECLARE
(
void
)
apr_atomic_sub64
(
volatile
apr_uint64_t
*
mem
,
apr_uint64_t
val
)
132
{
133
DECLARE_MUTEX_LOCKED
(mutex,
mem
);
134
*
mem
-=
val
;
135
MUTEX_UNLOCK
(mutex);
136
}
137
138
APR_DECLARE
(
apr_uint64_t
)
apr_atomic_inc64
(
volatile
apr_uint64_t
*
mem
)
139
{
140
return
apr_atomic_add64
(
mem
, 1);
141
}
142
143
APR_DECLARE
(
int
)
apr_atomic_dec64
(
volatile
apr_uint64_t
*
mem
)
144
{
145
apr_uint64_t
new
;
146
DECLARE_MUTEX_LOCKED
(mutex,
mem
);
147
148
(*mem)--;
149
new
= *
mem
;
150
151
MUTEX_UNLOCK
(mutex);
152
153
return
new
;
154
}
155
156
APR_DECLARE
(
apr_uint64_t
)
apr_atomic_cas64
(
volatile
apr_uint64_t
*
mem
,
apr_uint64_t
with
,
157
apr_uint64_t
cmp
)
158
{
159
apr_uint64_t
prev;
160
DECLARE_MUTEX_LOCKED
(mutex,
mem
);
161
162
prev = *
mem
;
163
if
(prev ==
cmp
) {
164
*
mem
=
with
;
165
}
166
167
MUTEX_UNLOCK
(mutex);
168
169
return
prev;
170
}
171
172
APR_DECLARE
(
apr_uint64_t
)
apr_atomic_xchg64
(
volatile
apr_uint64_t
*
mem
,
apr_uint64_t
val
)
173
{
174
apr_uint64_t
prev;
175
DECLARE_MUTEX_LOCKED
(mutex,
mem
);
176
177
prev = *
mem
;
178
*
mem
=
val
;
179
180
MUTEX_UNLOCK
(mutex);
181
182
return
prev;
183
}
184
185
#endif
/* USE_ATOMICS_GENERIC64 */
apr_arch_atomic.h
apr__atomic_generic64_init
apr_status_t apr__atomic_generic64_init(apr_pool_t *p)
apr_thread_mutex.h
APR Thread Mutex Routines.
APR_DECLARE
const void apr_status_t(*) apr_status_t(* APR_DECLARE)(void) apr_pool_pre_cleanup_register(apr_pool_t *p
Definition
apr_pools.h:646
size
apr_size_t size
Definition
apr_allocator.h:115
cmp
apr_uint32_t apr_uint32_t cmp
Definition
apr_atomic.h:106
with
apr_uint32_t with
Definition
apr_atomic.h:105
val
apr_uint32_t val
Definition
apr_atomic.h:66
APR_SUCCESS
#define APR_SUCCESS
Definition
apr_errno.h:225
apr_status_t
int apr_status_t
Definition
apr_errno.h:44
data
void * data
Definition
apr_file_io.h:832
mem
void * mem
Definition
apr_skiplist.h:88
p
apr_pool_t * p
Definition
md_event.c:32
NULL
return NULL
Definition
mod_so.c:359
i
int i
Definition
mod_so.c:347
apr_pool_t
Definition
apr_pools.c:562
apr_thread_mutex_t
Definition
apr_arch_thread_mutex.h:28
Generated by
1.9.8