/src/httpd/srclib/apr/locks/unix/global_mutex.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
19 | | #include "apr_strings.h" |
20 | | #include "apr_arch_global_mutex.h" |
21 | | #include "apr_proc_mutex.h" |
22 | | #include "apr_thread_mutex.h" |
23 | | #include "apr_portable.h" |
24 | | |
25 | | static apr_status_t global_mutex_cleanup(void *data) |
26 | 0 | { |
27 | 0 | apr_global_mutex_t *m = (apr_global_mutex_t *)data; |
28 | 0 | apr_status_t rv; |
29 | |
|
30 | 0 | rv = apr_proc_mutex_destroy(m->proc_mutex); |
31 | |
|
32 | 0 | #if APR_HAS_THREADS |
33 | 0 | if (m->thread_mutex) { |
34 | 0 | if (rv != APR_SUCCESS) { |
35 | 0 | (void)apr_thread_mutex_destroy(m->thread_mutex); |
36 | 0 | } |
37 | 0 | else { |
38 | 0 | rv = apr_thread_mutex_destroy(m->thread_mutex); |
39 | 0 | } |
40 | 0 | } |
41 | 0 | #endif /* APR_HAS_THREADS */ |
42 | |
|
43 | 0 | return rv; |
44 | 0 | } |
45 | | |
46 | | APR_DECLARE(apr_status_t) apr_global_mutex_create(apr_global_mutex_t **mutex, |
47 | | const char *fname, |
48 | | apr_lockmech_e mech, |
49 | | apr_pool_t *pool) |
50 | 0 | { |
51 | 0 | apr_status_t rv; |
52 | 0 | apr_global_mutex_t *m; |
53 | |
|
54 | 0 | m = (apr_global_mutex_t *)apr_palloc(pool, sizeof(*m)); |
55 | 0 | m->pool = pool; |
56 | |
|
57 | 0 | rv = apr_proc_mutex_create(&m->proc_mutex, fname, mech, m->pool); |
58 | 0 | if (rv != APR_SUCCESS) { |
59 | 0 | return rv; |
60 | 0 | } |
61 | | |
62 | 0 | #if APR_HAS_THREADS |
63 | 0 | if (m->proc_mutex->meth->flags & APR_PROCESS_LOCK_MECH_IS_GLOBAL) { |
64 | 0 | m->thread_mutex = NULL; /* We don't need a thread lock. */ |
65 | 0 | } |
66 | 0 | else { |
67 | 0 | rv = apr_thread_mutex_create(&m->thread_mutex, |
68 | 0 | APR_THREAD_MUTEX_DEFAULT, m->pool); |
69 | 0 | if (rv != APR_SUCCESS) { |
70 | 0 | rv = apr_proc_mutex_destroy(m->proc_mutex); |
71 | 0 | return rv; |
72 | 0 | } |
73 | 0 | } |
74 | 0 | #endif /* APR_HAS_THREADS */ |
75 | | |
76 | 0 | apr_pool_cleanup_register(m->pool, (void *)m, |
77 | 0 | global_mutex_cleanup, apr_pool_cleanup_null); |
78 | 0 | *mutex = m; |
79 | 0 | return APR_SUCCESS; |
80 | 0 | } |
81 | | |
82 | | APR_DECLARE(apr_status_t) apr_global_mutex_child_init( |
83 | | apr_global_mutex_t **mutex, |
84 | | const char *fname, |
85 | | apr_pool_t *pool) |
86 | 0 | { |
87 | 0 | apr_status_t rv; |
88 | |
|
89 | 0 | rv = apr_proc_mutex_child_init(&((*mutex)->proc_mutex), fname, pool); |
90 | 0 | return rv; |
91 | 0 | } |
92 | | |
93 | | APR_DECLARE(apr_status_t) apr_global_mutex_lock(apr_global_mutex_t *mutex) |
94 | 0 | { |
95 | 0 | apr_status_t rv; |
96 | |
|
97 | 0 | #if APR_HAS_THREADS |
98 | 0 | if (mutex->thread_mutex) { |
99 | 0 | rv = apr_thread_mutex_lock(mutex->thread_mutex); |
100 | 0 | if (rv != APR_SUCCESS) { |
101 | 0 | return rv; |
102 | 0 | } |
103 | 0 | } |
104 | 0 | #endif /* APR_HAS_THREADS */ |
105 | | |
106 | 0 | rv = apr_proc_mutex_lock(mutex->proc_mutex); |
107 | |
|
108 | 0 | #if APR_HAS_THREADS |
109 | 0 | if (rv != APR_SUCCESS) { |
110 | 0 | if (mutex->thread_mutex) { |
111 | 0 | (void)apr_thread_mutex_unlock(mutex->thread_mutex); |
112 | 0 | } |
113 | 0 | } |
114 | 0 | #endif /* APR_HAS_THREADS */ |
115 | |
|
116 | 0 | return rv; |
117 | 0 | } |
118 | | |
119 | | APR_DECLARE(apr_status_t) apr_global_mutex_trylock(apr_global_mutex_t *mutex) |
120 | 0 | { |
121 | 0 | apr_status_t rv; |
122 | |
|
123 | 0 | #if APR_HAS_THREADS |
124 | 0 | if (mutex->thread_mutex) { |
125 | 0 | rv = apr_thread_mutex_trylock(mutex->thread_mutex); |
126 | 0 | if (rv != APR_SUCCESS) { |
127 | 0 | return rv; |
128 | 0 | } |
129 | 0 | } |
130 | 0 | #endif /* APR_HAS_THREADS */ |
131 | | |
132 | 0 | rv = apr_proc_mutex_trylock(mutex->proc_mutex); |
133 | |
|
134 | 0 | #if APR_HAS_THREADS |
135 | 0 | if (rv != APR_SUCCESS) { |
136 | 0 | if (mutex->thread_mutex) { |
137 | 0 | (void)apr_thread_mutex_unlock(mutex->thread_mutex); |
138 | 0 | } |
139 | 0 | } |
140 | 0 | #endif /* APR_HAS_THREADS */ |
141 | |
|
142 | 0 | return rv; |
143 | 0 | } |
144 | | |
145 | | APR_DECLARE(apr_status_t) apr_global_mutex_timedlock(apr_global_mutex_t *mutex, |
146 | | apr_interval_time_t timeout) |
147 | 0 | { |
148 | 0 | apr_status_t rv; |
149 | |
|
150 | 0 | #if APR_HAS_THREADS |
151 | 0 | if (mutex->thread_mutex) { |
152 | 0 | apr_time_t expiry = 0; |
153 | 0 | if (timeout > 0) { |
154 | 0 | expiry = apr_time_now() + timeout; |
155 | 0 | } |
156 | 0 | rv = apr_thread_mutex_timedlock(mutex->thread_mutex, timeout); |
157 | 0 | if (rv != APR_SUCCESS) { |
158 | 0 | return rv; |
159 | 0 | } |
160 | 0 | if (expiry) { |
161 | 0 | timeout = expiry - apr_time_now(); |
162 | 0 | if (timeout < 0) { |
163 | 0 | timeout = 0; |
164 | 0 | } |
165 | 0 | } |
166 | 0 | } |
167 | 0 | #endif /* APR_HAS_THREADS */ |
168 | | |
169 | 0 | rv = apr_proc_mutex_timedlock(mutex->proc_mutex, timeout); |
170 | |
|
171 | 0 | #if APR_HAS_THREADS |
172 | 0 | if (rv != APR_SUCCESS) { |
173 | 0 | if (mutex->thread_mutex) { |
174 | 0 | (void)apr_thread_mutex_unlock(mutex->thread_mutex); |
175 | 0 | } |
176 | 0 | } |
177 | 0 | #endif /* APR_HAS_THREADS */ |
178 | |
|
179 | 0 | return rv; |
180 | 0 | } |
181 | | |
182 | | APR_DECLARE(apr_status_t) apr_global_mutex_unlock(apr_global_mutex_t *mutex) |
183 | 0 | { |
184 | 0 | apr_status_t rv; |
185 | |
|
186 | 0 | rv = apr_proc_mutex_unlock(mutex->proc_mutex); |
187 | 0 | #if APR_HAS_THREADS |
188 | 0 | if (mutex->thread_mutex) { |
189 | 0 | if (rv != APR_SUCCESS) { |
190 | 0 | (void)apr_thread_mutex_unlock(mutex->thread_mutex); |
191 | 0 | } |
192 | 0 | else { |
193 | 0 | rv = apr_thread_mutex_unlock(mutex->thread_mutex); |
194 | 0 | } |
195 | 0 | } |
196 | 0 | #endif /* APR_HAS_THREADS */ |
197 | 0 | return rv; |
198 | 0 | } |
199 | | |
200 | | APR_DECLARE(apr_status_t) apr_os_global_mutex_get(apr_os_global_mutex_t *ospmutex, |
201 | | apr_global_mutex_t *pmutex) |
202 | 0 | { |
203 | 0 | ospmutex->pool = pmutex->pool; |
204 | 0 | ospmutex->proc_mutex = pmutex->proc_mutex; |
205 | 0 | #if APR_HAS_THREADS |
206 | 0 | ospmutex->thread_mutex = pmutex->thread_mutex; |
207 | 0 | #endif |
208 | 0 | return APR_SUCCESS; |
209 | 0 | } |
210 | | |
211 | | APR_DECLARE(apr_status_t) apr_global_mutex_destroy(apr_global_mutex_t *mutex) |
212 | 0 | { |
213 | 0 | return apr_pool_cleanup_run(mutex->pool, mutex, global_mutex_cleanup); |
214 | 0 | } |
215 | | |
216 | | APR_DECLARE(const char *) apr_global_mutex_lockfile(apr_global_mutex_t *mutex) |
217 | 0 | { |
218 | 0 | return apr_proc_mutex_lockfile(mutex->proc_mutex); |
219 | 0 | } |
220 | | |
221 | | APR_DECLARE(apr_lockmech_e) apr_global_mutex_mech(apr_global_mutex_t *mutex) |
222 | 0 | { |
223 | 0 | return apr_proc_mutex_mech(mutex->proc_mutex); |
224 | 0 | } |
225 | | |
226 | | APR_DECLARE(const char *) apr_global_mutex_name(apr_global_mutex_t *mutex) |
227 | 0 | { |
228 | 0 | return apr_proc_mutex_name(mutex->proc_mutex); |
229 | 0 | } |
230 | | |
231 | | APR_PERMS_SET_IMPLEMENT(global_mutex) |
232 | 0 | { |
233 | 0 | apr_status_t rv; |
234 | 0 | apr_global_mutex_t *mutex = (apr_global_mutex_t *)theglobal_mutex; |
235 | |
|
236 | 0 | rv = APR_PERMS_SET_FN(proc_mutex)(mutex->proc_mutex, perms, uid, gid); |
237 | 0 | return rv; |
238 | 0 | } |
239 | | |
240 | | APR_POOL_IMPLEMENT_ACCESSOR(global_mutex) |
241 | | |