Apache HTTPD
proc_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#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"
25
27{
28 apr_proc_mutex_t *mutex = mutex_;
29
30 if (mutex->handle) {
31 if (CloseHandle(mutex->handle) == 0) {
32 return apr_get_os_error();
33 }
34 }
35 return APR_SUCCESS;
36}
37
39 const char *fname,
42{
43 HANDLE hMutex;
44 void *mutexkey;
45
47 return APR_ENOTIMPL;
48 }
49
50 /* res_name_from_filename turns fname into a pseduo-name
51 * without slashes or backslashes, and prepends the \global
52 * prefix on Win2K and later
53 */
54 if (fname) {
56 }
57 else {
58 mutexkey = NULL;
59 }
60
61#if APR_HAS_UNICODE_FS
63 {
64 hMutex = CreateMutexW(NULL, FALSE, mutexkey);
65 }
66#endif
67#if APR_HAS_ANSI_FS
69 {
70 hMutex = CreateMutexA(NULL, FALSE, mutexkey);
71 }
72#endif
73
74 if (!hMutex) {
75 return apr_get_os_error();
76 }
77
79 (*mutex)->pool = pool;
80 (*mutex)->handle = hMutex;
81 (*mutex)->fname = fname;
82 apr_pool_cleanup_register((*mutex)->pool, *mutex,
84 return APR_SUCCESS;
85}
86
88 const char *fname,
90{
91 HANDLE hMutex;
92 void *mutexkey;
93
94 if (!fname) {
95 /* Reinitializing unnamed mutexes is a noop in the Unix code. */
96 return APR_SUCCESS;
97 }
98
99 /* res_name_from_filename turns file into a pseudo-name
100 * without slashes or backslashes, and prepends the \global
101 * prefix on Win2K and later
102 */
104
105#if defined(_WIN32_WCE)
106 hMutex = CreateMutex(NULL, FALSE, mutexkey);
107 if (hMutex && ERROR_ALREADY_EXISTS != GetLastError()) {
108 CloseHandle(hMutex);
109 hMutex = NULL;
111 }
112#else
113#if APR_HAS_UNICODE_FS
115 {
117 }
118#endif
119#if APR_HAS_ANSI_FS
121 {
123 }
124#endif
125#endif
126
127 if (!hMutex) {
128 return apr_get_os_error();
129 }
130
131 *mutex = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t));
132 (*mutex)->pool = pool;
133 (*mutex)->handle = hMutex;
134 (*mutex)->fname = fname;
135 apr_pool_cleanup_register((*mutex)->pool, *mutex,
137 return APR_SUCCESS;
138}
139
141{
142 DWORD rv;
143
144 rv = WaitForSingleObject(mutex->handle, INFINITE);
145
146 if (rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
147 return APR_SUCCESS;
148 }
149 return apr_get_os_error();
150}
151
153{
154 DWORD rv;
155
156 rv = WaitForSingleObject(mutex->handle, 0);
157
158 if (rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
159 return APR_SUCCESS;
160 }
161 else if (rv == WAIT_TIMEOUT) {
162 return APR_EBUSY;
163 }
164 return apr_get_os_error();
165}
166
169{
170 DWORD rv, timeout_ms = 0;
172
173 do {
174 if (t > 0) {
175 /* Given timeout is 64bit usecs whereas Windows timeouts are
176 * 32bit msecs and below INFINITE (2^32 - 1), so we may need
177 * multiple timed out waits...
178 */
179 if (t > apr_time_from_msec(INFINITE - 1)) {
180 timeout_ms = INFINITE - 1;
182 }
183 else {
184 timeout_ms = (DWORD)apr_time_as_msec(t);
185 t = 0;
186 }
187 }
188 rv = WaitForSingleObject(mutex->handle, timeout_ms);
189 } while (rv == WAIT_TIMEOUT && t > 0);
190
191 if (rv == WAIT_TIMEOUT) {
192 return APR_TIMEUP;
193 }
194 if (rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
195 return APR_SUCCESS;
196 }
197 return apr_get_os_error();
198}
199
201{
202 if (ReleaseMutex(mutex->handle) == 0) {
203 return apr_get_os_error();
204 }
205 return APR_SUCCESS;
206}
207
209{
211
212 stat = proc_mutex_cleanup(mutex);
213 if (stat == APR_SUCCESS) {
215 }
216 return stat;
217}
218
220{
222}
223
225{
226 return mutex->fname;
227}
228
233
235{
236 return apr_proc_mutex_defname();
237}
238
239APR_DECLARE(const char *) apr_proc_mutex_defname(void)
240{
241 return "win32mutex";
242}
243
245
247
248/* Implement OS-specific accessors defined in apr_portable.h */
249
253{
254 *ospmutex = pmutex->handle;
255 if (mech) {
257 }
258 return APR_SUCCESS;
259}
260
263{
265}
266
272{
273 if (pool == NULL) {
274 return APR_ENOPOOL;
275 }
277 return APR_ENOTIMPL;
278 }
279
280 if ((*pmutex) == NULL) {
281 (*pmutex) = (apr_proc_mutex_t *)apr_palloc(pool,
282 sizeof(apr_proc_mutex_t));
283 (*pmutex)->pool = pool;
284 }
285 (*pmutex)->handle = *ospmutex;
286
287 if (register_cleanup) {
290 }
291 return APR_SUCCESS;
292}
293
297{
299 0, pool);
300}
301
#define FALSE
Definition abts.h:35
#define CreateMutexA(sd, b, nm)
#define CreateMutexW(sd, b, nm)
#define CloseHandle(h)
#define WaitForSingleObject(h, d)
APR Miscellaneous library routines.
APR Portability Routines.
APR Strings library.
#define APR_ENOTIMPL
Definition apr_errno.h:476
#define APR_TIMEUP
Definition apr_errno.h:450
#define APR_ENOPOOL
Definition apr_errno.h:290
#define APR_EBUSY
Definition apr_errno.h:480
const char apr_lockmech_e mech
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
#define apr_get_os_error()
Definition apr_errno.h:1217
int apr_status_t
Definition apr_errno.h:44
const char * fname
apr_interval_time_t t
#define APR_PERMS_SET_ENOTIMPL(type)
#define APR_POOL_IMPLEMENT_ACCESSOR(type)
Definition apr_pools.h:91
apr_os_proc_mutex_t * ospmutex
apr_os_file_t int register_cleanup
apr_global_mutex_t * pmutex
apr_lockmech_e
@ APR_LOCK_DEFAULT_TIMED
@ APR_LOCK_DEFAULT
#define apr_time_as_msec(time)
Definition apr_time.h:72
#define apr_time_from_msec(msec)
Definition apr_time.h:75
apr_int64_t apr_interval_time_t
Definition apr_time.h:55
return NULL
Definition mod_so.c:359
static void proc_mutex(abts_case *tc, void *data)
IN ULONG IN INT timeout
#define ELSE_WIN_OS_IS_ANSI
typedef HANDLE(WINAPI *apr_winapi_fpt_CreateToolhelp32Snapshot)(DWORD dwFlags
#define IF_WIN_OS_IS_UNICODE
typedef DWORD(WINAPI *apr_winapi_fpt_GetCompressedFileSizeA)(IN LPCSTR lpFileName
void * res_name_from_filename(const char *file, int global, apr_pool_t *pool)
Definition open.c:140
static apr_status_t proc_mutex_cleanup(void *mutex_)
Definition proc_mutex.c:26