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.
18 #include "apr_strings.h"
19 #include "apr_arch_global_mutex.h"
20 #include "apr_proc_mutex.h"
21 #include "apr_thread_mutex.h"
22 #include "apr_portable.h"
24 static apr_status_t global_mutex_cleanup(void *data)
26 apr_global_mutex_t *m = (apr_global_mutex_t *)data;
29 rv = apr_proc_mutex_destroy(m->proc_mutex);
32 if (m->thread_mutex) {
33 if (rv != APR_SUCCESS) {
34 (void)apr_thread_mutex_destroy(m->thread_mutex);
37 rv = apr_thread_mutex_destroy(m->thread_mutex);
40 #endif /* APR_HAS_THREADS */
45 APR_DECLARE(apr_status_t) apr_global_mutex_create(apr_global_mutex_t **mutex,
51 apr_global_mutex_t *m;
53 m = (apr_global_mutex_t *)apr_palloc(pool, sizeof(*m));
56 rv = apr_proc_mutex_create(&m->proc_mutex, fname, mech, m->pool);
57 if (rv != APR_SUCCESS) {
62 if (m->proc_mutex->inter_meth->flags & APR_PROCESS_LOCK_MECH_IS_GLOBAL) {
63 m->thread_mutex = NULL; /* We don't need a thread lock. */
66 rv = apr_thread_mutex_create(&m->thread_mutex,
67 APR_THREAD_MUTEX_DEFAULT, m->pool);
68 if (rv != APR_SUCCESS) {
69 rv = apr_proc_mutex_destroy(m->proc_mutex);
73 #endif /* APR_HAS_THREADS */
75 apr_pool_cleanup_register(m->pool, (void *)m,
76 global_mutex_cleanup, apr_pool_cleanup_null);
81 APR_DECLARE(apr_status_t) apr_global_mutex_child_init(
82 apr_global_mutex_t **mutex,
88 rv = apr_proc_mutex_child_init(&((*mutex)->proc_mutex), fname, pool);
92 APR_DECLARE(apr_status_t) apr_global_mutex_lock(apr_global_mutex_t *mutex)
97 if (mutex->thread_mutex) {
98 rv = apr_thread_mutex_lock(mutex->thread_mutex);
99 if (rv != APR_SUCCESS) {
103 #endif /* APR_HAS_THREADS */
105 rv = apr_proc_mutex_lock(mutex->proc_mutex);
108 if (rv != APR_SUCCESS) {
109 if (mutex->thread_mutex) {
110 (void)apr_thread_mutex_unlock(mutex->thread_mutex);
113 #endif /* APR_HAS_THREADS */
118 APR_DECLARE(apr_status_t) apr_global_mutex_trylock(apr_global_mutex_t *mutex)
123 if (mutex->thread_mutex) {
124 rv = apr_thread_mutex_trylock(mutex->thread_mutex);
125 if (rv != APR_SUCCESS) {
129 #endif /* APR_HAS_THREADS */
131 rv = apr_proc_mutex_trylock(mutex->proc_mutex);
134 if (rv != APR_SUCCESS) {
135 if (mutex->thread_mutex) {
136 (void)apr_thread_mutex_unlock(mutex->thread_mutex);
139 #endif /* APR_HAS_THREADS */
144 APR_DECLARE(apr_status_t) apr_global_mutex_unlock(apr_global_mutex_t *mutex)
148 rv = apr_proc_mutex_unlock(mutex->proc_mutex);
150 if (mutex->thread_mutex) {
151 if (rv != APR_SUCCESS) {
152 (void)apr_thread_mutex_unlock(mutex->thread_mutex);
155 rv = apr_thread_mutex_unlock(mutex->thread_mutex);
158 #endif /* APR_HAS_THREADS */
162 APR_DECLARE(apr_status_t) apr_os_global_mutex_get(apr_os_global_mutex_t *ospmutex,
163 apr_global_mutex_t *pmutex)
165 ospmutex->pool = pmutex->pool;
166 ospmutex->proc_mutex = pmutex->proc_mutex;
168 ospmutex->thread_mutex = pmutex->thread_mutex;
173 APR_DECLARE(apr_status_t) apr_global_mutex_destroy(apr_global_mutex_t *mutex)
175 return apr_pool_cleanup_run(mutex->pool, mutex, global_mutex_cleanup);
178 APR_DECLARE(const char *) apr_global_mutex_lockfile(apr_global_mutex_t *mutex)
180 return apr_proc_mutex_lockfile(mutex->proc_mutex);
183 APR_DECLARE(const char *) apr_global_mutex_name(apr_global_mutex_t *mutex)
185 return apr_proc_mutex_name(mutex->proc_mutex);
188 APR_POOL_IMPLEMENT_ACCESSOR(global_mutex)