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.
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"
25 static apr_status_t global_mutex_cleanup(void *data)
27 apr_global_mutex_t *m = (apr_global_mutex_t *)data;
30 rv = apr_proc_mutex_destroy(m->proc_mutex);
33 if (m->thread_mutex) {
34 if (rv != APR_SUCCESS) {
35 (void)apr_thread_mutex_destroy(m->thread_mutex);
38 rv = apr_thread_mutex_destroy(m->thread_mutex);
41 #endif /* APR_HAS_THREADS */
46 APR_DECLARE(apr_status_t) apr_global_mutex_create(apr_global_mutex_t **mutex,
52 apr_global_mutex_t *m;
54 m = (apr_global_mutex_t *)apr_palloc(pool, sizeof(*m));
57 rv = apr_proc_mutex_create(&m->proc_mutex, fname, mech, m->pool);
58 if (rv != APR_SUCCESS) {
63 if (m->proc_mutex->meth->flags & APR_PROCESS_LOCK_MECH_IS_GLOBAL) {
64 m->thread_mutex = NULL; /* We don't need a thread lock. */
67 rv = apr_thread_mutex_create(&m->thread_mutex,
68 APR_THREAD_MUTEX_DEFAULT, m->pool);
69 if (rv != APR_SUCCESS) {
70 rv = apr_proc_mutex_destroy(m->proc_mutex);
74 #endif /* APR_HAS_THREADS */
76 apr_pool_cleanup_register(m->pool, (void *)m,
77 global_mutex_cleanup, apr_pool_cleanup_null);
82 APR_DECLARE(apr_status_t) apr_global_mutex_child_init(
83 apr_global_mutex_t **mutex,
89 rv = apr_proc_mutex_child_init(&((*mutex)->proc_mutex), fname, pool);
93 APR_DECLARE(apr_status_t) apr_global_mutex_lock(apr_global_mutex_t *mutex)
98 if (mutex->thread_mutex) {
99 rv = apr_thread_mutex_lock(mutex->thread_mutex);
100 if (rv != APR_SUCCESS) {
104 #endif /* APR_HAS_THREADS */
106 rv = apr_proc_mutex_lock(mutex->proc_mutex);
109 if (rv != APR_SUCCESS) {
110 if (mutex->thread_mutex) {
111 (void)apr_thread_mutex_unlock(mutex->thread_mutex);
114 #endif /* APR_HAS_THREADS */
119 APR_DECLARE(apr_status_t) apr_global_mutex_trylock(apr_global_mutex_t *mutex)
124 if (mutex->thread_mutex) {
125 rv = apr_thread_mutex_trylock(mutex->thread_mutex);
126 if (rv != APR_SUCCESS) {
130 #endif /* APR_HAS_THREADS */
132 rv = apr_proc_mutex_trylock(mutex->proc_mutex);
135 if (rv != APR_SUCCESS) {
136 if (mutex->thread_mutex) {
137 (void)apr_thread_mutex_unlock(mutex->thread_mutex);
140 #endif /* APR_HAS_THREADS */
145 APR_DECLARE(apr_status_t) apr_global_mutex_timedlock(apr_global_mutex_t *mutex,
146 apr_interval_time_t timeout)
148 #if APR_HAS_TIMEDLOCKS
152 if (mutex->thread_mutex) {
153 apr_time_t expiry = 0;
155 expiry = apr_time_now() + timeout;
157 rv = apr_thread_mutex_timedlock(mutex->thread_mutex, timeout);
158 if (rv != APR_SUCCESS) {
162 timeout = expiry - apr_time_now();
168 #endif /* APR_HAS_THREADS */
170 rv = apr_proc_mutex_timedlock(mutex->proc_mutex, timeout);
173 if (rv != APR_SUCCESS) {
174 if (mutex->thread_mutex) {
175 (void)apr_thread_mutex_unlock(mutex->thread_mutex);
178 #endif /* APR_HAS_THREADS */
181 #else /* APR_HAS_TIMEDLOCKS */
186 APR_DECLARE(apr_status_t) apr_global_mutex_unlock(apr_global_mutex_t *mutex)
190 rv = apr_proc_mutex_unlock(mutex->proc_mutex);
192 if (mutex->thread_mutex) {
193 if (rv != APR_SUCCESS) {
194 (void)apr_thread_mutex_unlock(mutex->thread_mutex);
197 rv = apr_thread_mutex_unlock(mutex->thread_mutex);
200 #endif /* APR_HAS_THREADS */
204 APR_DECLARE(apr_status_t) apr_os_global_mutex_get(apr_os_global_mutex_t *ospmutex,
205 apr_global_mutex_t *pmutex)
207 ospmutex->pool = pmutex->pool;
208 ospmutex->proc_mutex = pmutex->proc_mutex;
210 ospmutex->thread_mutex = pmutex->thread_mutex;
215 APR_DECLARE(apr_status_t) apr_global_mutex_destroy(apr_global_mutex_t *mutex)
217 return apr_pool_cleanup_run(mutex->pool, mutex, global_mutex_cleanup);
220 APR_DECLARE(const char *) apr_global_mutex_lockfile(apr_global_mutex_t *mutex)
222 return apr_proc_mutex_lockfile(mutex->proc_mutex);
225 APR_DECLARE(apr_lockmech_e) apr_global_mutex_mech(apr_global_mutex_t *mutex)
227 return apr_proc_mutex_mech(mutex->proc_mutex);
230 APR_DECLARE(const char *) apr_global_mutex_name(apr_global_mutex_t *mutex)
232 return apr_proc_mutex_name(mutex->proc_mutex);
235 APR_PERMS_SET_IMPLEMENT(global_mutex)
238 apr_global_mutex_t *mutex = (apr_global_mutex_t *)theglobal_mutex;
240 rv = APR_PERMS_SET_FN(proc_mutex)(mutex->proc_mutex, perms, uid, gid);
244 APR_POOL_IMPLEMENT_ACCESSOR(global_mutex)