1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* 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_private.h"
19
#include "apr_general.h"
20
#include "apr_strings.h"
21
#include "apr_portable.h"
22
#include "apr_arch_file_io.h"
23
#include "apr_arch_proc_mutex.h"
24
#include "apr_arch_misc.h"
26
static apr_status_t proc_mutex_cleanup(void *mutex_)
28
apr_proc_mutex_t *mutex = mutex_;
31
if (CloseHandle(mutex->handle) == 0) {
32
return apr_get_os_error();
38
APR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex,
46
/* res_name_from_filename turns fname into a pseduo-name
47
* without slashes or backslashes, and prepends the \global
48
* prefix on Win2K and later
51
mutexkey = res_name_from_filename(fname, 1, pool);
57
#if APR_HAS_UNICODE_FS
60
hMutex = CreateMutexW(NULL, FALSE, mutexkey);
66
hMutex = CreateMutexA(NULL, FALSE, mutexkey);
71
return apr_get_os_error();
74
*mutex = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t));
75
(*mutex)->pool = pool;
76
(*mutex)->handle = hMutex;
77
(*mutex)->fname = fname;
78
apr_pool_cleanup_register((*mutex)->pool, *mutex,
79
proc_mutex_cleanup, apr_pool_cleanup_null);
83
APR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex,
91
/* Reinitializing unnamed mutexes is a noop in the Unix code. */
95
/* res_name_from_filename turns file into a pseudo-name
96
* without slashes or backslashes, and prepends the \global
97
* prefix on Win2K and later
99
mutexkey = res_name_from_filename(fname, 1, pool);
101
#if APR_HAS_UNICODE_FS
104
hMutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, mutexkey);
110
hMutex = OpenMutexA(MUTEX_ALL_ACCESS, FALSE, mutexkey);
115
return apr_get_os_error();
118
*mutex = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t));
119
(*mutex)->pool = pool;
120
(*mutex)->handle = hMutex;
121
(*mutex)->fname = fname;
122
apr_pool_cleanup_register((*mutex)->pool, *mutex,
123
proc_mutex_cleanup, apr_pool_cleanup_null);
127
APR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex)
131
rv = WaitForSingleObject(mutex->handle, INFINITE);
133
if (rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
136
else if (rv == WAIT_TIMEOUT) {
139
return apr_get_os_error();
142
APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex)
146
rv = WaitForSingleObject(mutex->handle, 0);
148
if (rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
151
else if (rv == WAIT_TIMEOUT) {
154
return apr_get_os_error();
157
APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex)
159
if (ReleaseMutex(mutex->handle) == 0) {
160
return apr_get_os_error();
165
APR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex)
169
stat = proc_mutex_cleanup(mutex);
170
if (stat == APR_SUCCESS) {
171
apr_pool_cleanup_kill(mutex->pool, mutex, proc_mutex_cleanup);
176
APR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *mutex)
178
return apr_proc_mutex_destroy((apr_proc_mutex_t *)mutex);
181
APR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex)
186
APR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex)
191
APR_DECLARE(const char *) apr_proc_mutex_defname(void)
196
APR_POOL_IMPLEMENT_ACCESSOR(proc_mutex)
198
/* Implement OS-specific accessors defined in apr_portable.h */
200
APR_DECLARE(apr_status_t) apr_os_proc_mutex_get(apr_os_proc_mutex_t *ospmutex,
201
apr_proc_mutex_t *mutex)
203
*ospmutex = mutex->handle;
207
APR_DECLARE(apr_status_t) apr_os_proc_mutex_put(apr_proc_mutex_t **pmutex,
208
apr_os_proc_mutex_t *ospmutex,
214
if ((*pmutex) == NULL) {
215
(*pmutex) = (apr_proc_mutex_t *)apr_palloc(pool,
216
sizeof(apr_proc_mutex_t));
217
(*pmutex)->pool = pool;
219
(*pmutex)->handle = *ospmutex;