Apache HTTPD
global_mutex.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.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
26{
28 apr_status_t rv;
29
30 rv = apr_proc_mutex_destroy(m->proc_mutex);
31
32#if APR_HAS_THREADS
33 if (m->thread_mutex) {
34 if (rv != APR_SUCCESS) {
35 (void)apr_thread_mutex_destroy(m->thread_mutex);
36 }
37 else {
38 rv = apr_thread_mutex_destroy(m->thread_mutex);
39 }
40 }
41#endif /* APR_HAS_THREADS */
42
43 return rv;
44}
45
47 const char *fname,
50{
51 apr_status_t rv;
53
54 m = (apr_global_mutex_t *)apr_palloc(pool, sizeof(*m));
55 m->pool = pool;
56
57 rv = apr_proc_mutex_create(&m->proc_mutex, fname, mech, m->pool);
58 if (rv != APR_SUCCESS) {
59 return rv;
60 }
61
62#if APR_HAS_THREADS
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. */
65 }
66 else {
67 rv = apr_thread_mutex_create(&m->thread_mutex,
69 if (rv != APR_SUCCESS) {
70 rv = apr_proc_mutex_destroy(m->proc_mutex);
71 return rv;
72 }
73 }
74#endif /* APR_HAS_THREADS */
75
76 apr_pool_cleanup_register(m->pool, (void *)m,
78 *mutex = m;
79 return APR_SUCCESS;
80}
81
83 apr_global_mutex_t **mutex,
84 const char *fname,
86{
87 apr_status_t rv;
88
89 rv = apr_proc_mutex_child_init(&((*mutex)->proc_mutex), fname, pool);
90 return rv;
91}
92
94{
95 apr_status_t rv;
96
97#if APR_HAS_THREADS
98 if (mutex->thread_mutex) {
99 rv = apr_thread_mutex_lock(mutex->thread_mutex);
100 if (rv != APR_SUCCESS) {
101 return rv;
102 }
103 }
104#endif /* APR_HAS_THREADS */
105
106 rv = apr_proc_mutex_lock(mutex->proc_mutex);
107
108#if APR_HAS_THREADS
109 if (rv != APR_SUCCESS) {
110 if (mutex->thread_mutex) {
111 (void)apr_thread_mutex_unlock(mutex->thread_mutex);
112 }
113 }
114#endif /* APR_HAS_THREADS */
115
116 return rv;
117}
118
120{
121 apr_status_t rv;
122
123#if APR_HAS_THREADS
124 if (mutex->thread_mutex) {
125 rv = apr_thread_mutex_trylock(mutex->thread_mutex);
126 if (rv != APR_SUCCESS) {
127 return rv;
128 }
129 }
130#endif /* APR_HAS_THREADS */
131
133
134#if APR_HAS_THREADS
135 if (rv != APR_SUCCESS) {
136 if (mutex->thread_mutex) {
137 (void)apr_thread_mutex_unlock(mutex->thread_mutex);
138 }
139 }
140#endif /* APR_HAS_THREADS */
141
142 return rv;
143}
144
147{
148#if APR_HAS_TIMEDLOCKS
149 apr_status_t rv;
150
151#if APR_HAS_THREADS
152 if (mutex->thread_mutex) {
153 apr_time_t expiry = 0;
154 if (timeout > 0) {
155 expiry = apr_time_now() + timeout;
156 }
157 rv = apr_thread_mutex_timedlock(mutex->thread_mutex, timeout);
158 if (rv != APR_SUCCESS) {
159 return rv;
160 }
161 if (expiry) {
162 timeout = expiry - apr_time_now();
163 if (timeout < 0) {
164 timeout = 0;
165 }
166 }
167 }
168#endif /* APR_HAS_THREADS */
169
171
172#if APR_HAS_THREADS
173 if (rv != APR_SUCCESS) {
174 if (mutex->thread_mutex) {
175 (void)apr_thread_mutex_unlock(mutex->thread_mutex);
176 }
177 }
178#endif /* APR_HAS_THREADS */
179
180 return rv;
181#else /* APR_HAS_TIMEDLOCKS */
182 return APR_ENOTIMPL;
183#endif
184}
185
187{
188 apr_status_t rv;
189
191#if APR_HAS_THREADS
192 if (mutex->thread_mutex) {
193 if (rv != APR_SUCCESS) {
194 (void)apr_thread_mutex_unlock(mutex->thread_mutex);
195 }
196 else {
197 rv = apr_thread_mutex_unlock(mutex->thread_mutex);
198 }
199 }
200#endif /* APR_HAS_THREADS */
201 return rv;
202}
203
206{
207 ospmutex->pool = pmutex->pool;
208 ospmutex->proc_mutex = pmutex->proc_mutex;
209#if APR_HAS_THREADS
210 ospmutex->thread_mutex = pmutex->thread_mutex;
211#endif
212 return APR_SUCCESS;
213}
214
216{
217 return apr_pool_cleanup_run(mutex->pool, mutex, global_mutex_cleanup);
218}
219
224
229
231{
232 return apr_proc_mutex_name(mutex->proc_mutex);
233}
234
236{
237 apr_status_t rv;
239
241 return rv;
242}
243
245
APR Portability Routines.
APR Process Locking Routines.
APR Strings library.
APR Thread Mutex Routines.
static apr_status_t global_mutex_cleanup(void *data)
#define APR_ENOTIMPL
Definition apr_errno.h:476
apr_fileperms_t apr_uid_t uid
const char apr_lockmech_e mech
apr_fileperms_t apr_uid_t apr_gid_t gid
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
apr_size_t size
const char int apr_pool_t * pool
Definition apr_cstr.h:84
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
const char apr_fileperms_t perms
void * data
const char * fname
#define APR_PERMS_SET_FN(type)
#define APR_PERMS_SET_IMPLEMENT(type)
#define APR_POOL_IMPLEMENT_ACCESSOR(type)
Definition apr_pools.h:91
apr_os_proc_mutex_t * ospmutex
apr_global_mutex_t * pmutex
apr_lockmech_e
const void * m
apr_int64_t apr_interval_time_t
Definition apr_time.h:55
apr_int64_t apr_time_t
Definition apr_time.h:45
return NULL
Definition mod_so.c:359
apr_proc_mutex_t * proc_mutex
static void proc_mutex(abts_case *tc, void *data)
#define APR_PROCESS_LOCK_MECH_IS_GLOBAL
IN ULONG IN INT timeout