~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/locks/win32/proc_mutex.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
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
 
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
 
 
26
static apr_status_t proc_mutex_cleanup(void *mutex_)
 
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
 
 
38
APR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex,
 
39
                                                const char *fname,
 
40
                                                apr_lockmech_e mech,
 
41
                                                apr_pool_t *pool)
 
42
{
 
43
    HANDLE hMutex;
 
44
    void *mutexkey;
 
45
 
 
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
 
49
     */
 
50
    if (fname) {
 
51
        mutexkey = res_name_from_filename(fname, 1, pool);
 
52
    }
 
53
    else {
 
54
        mutexkey = NULL;
 
55
    }
 
56
 
 
57
#if APR_HAS_UNICODE_FS
 
58
    IF_WIN_OS_IS_UNICODE
 
59
    {
 
60
        hMutex = CreateMutexW(NULL, FALSE, mutexkey);
 
61
    }
 
62
#endif
 
63
#if APR_HAS_ANSI_FS
 
64
    ELSE_WIN_OS_IS_ANSI
 
65
    {
 
66
        hMutex = CreateMutexA(NULL, FALSE, mutexkey);
 
67
    }
 
68
#endif
 
69
 
 
70
    if (!hMutex) {
 
71
        return apr_get_os_error();
 
72
    }
 
73
 
 
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);
 
80
    return APR_SUCCESS;
 
81
}
 
82
 
 
83
APR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex,
 
84
                                                    const char *fname,
 
85
                                                    apr_pool_t *pool)
 
86
{
 
87
    HANDLE hMutex;
 
88
    void *mutexkey;
 
89
 
 
90
    if (!fname) {
 
91
        /* Reinitializing unnamed mutexes is a noop in the Unix code. */
 
92
        return APR_SUCCESS;
 
93
    }
 
94
 
 
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
 
98
     */
 
99
    mutexkey = res_name_from_filename(fname, 1, pool);
 
100
 
 
101
#if APR_HAS_UNICODE_FS
 
102
    IF_WIN_OS_IS_UNICODE
 
103
    {
 
104
        hMutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, mutexkey);
 
105
    }
 
106
#endif
 
107
#if APR_HAS_ANSI_FS
 
108
    ELSE_WIN_OS_IS_ANSI
 
109
    {
 
110
        hMutex = OpenMutexA(MUTEX_ALL_ACCESS, FALSE, mutexkey);
 
111
    }
 
112
#endif
 
113
 
 
114
    if (!hMutex) {
 
115
        return apr_get_os_error();
 
116
    }
 
117
 
 
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);
 
124
    return APR_SUCCESS;
 
125
}
 
126
    
 
127
APR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex)
 
128
{
 
129
    DWORD rv;
 
130
 
 
131
    rv = WaitForSingleObject(mutex->handle, INFINITE);
 
132
 
 
133
    if (rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
 
134
        return APR_SUCCESS;
 
135
    } 
 
136
    else if (rv == WAIT_TIMEOUT) {
 
137
        return APR_EBUSY;
 
138
    }
 
139
    return apr_get_os_error();
 
140
}
 
141
 
 
142
APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex)
 
143
{
 
144
    DWORD rv;
 
145
 
 
146
    rv = WaitForSingleObject(mutex->handle, 0);
 
147
 
 
148
    if (rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
 
149
        return APR_SUCCESS;
 
150
    } 
 
151
    else if (rv == WAIT_TIMEOUT) {
 
152
        return APR_EBUSY;
 
153
    }
 
154
    return apr_get_os_error();
 
155
}
 
156
 
 
157
APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex)
 
158
{
 
159
    if (ReleaseMutex(mutex->handle) == 0) {
 
160
        return apr_get_os_error();
 
161
    }
 
162
    return APR_SUCCESS;
 
163
}
 
164
 
 
165
APR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex)
 
166
{
 
167
    apr_status_t stat;
 
168
 
 
169
    stat = proc_mutex_cleanup(mutex);
 
170
    if (stat == APR_SUCCESS) {
 
171
        apr_pool_cleanup_kill(mutex->pool, mutex, proc_mutex_cleanup);
 
172
    }
 
173
    return stat;
 
174
}
 
175
 
 
176
APR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *mutex)
 
177
{
 
178
    return apr_proc_mutex_destroy((apr_proc_mutex_t *)mutex);
 
179
}
 
180
 
 
181
APR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex)
 
182
{
 
183
    return NULL;
 
184
}
 
185
 
 
186
APR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex)
 
187
{
 
188
    return mutex->fname;
 
189
}
 
190
 
 
191
APR_DECLARE(const char *) apr_proc_mutex_defname(void)
 
192
{
 
193
    return "win32mutex";
 
194
}
 
195
 
 
196
APR_POOL_IMPLEMENT_ACCESSOR(proc_mutex)
 
197
 
 
198
/* Implement OS-specific accessors defined in apr_portable.h */
 
199
 
 
200
APR_DECLARE(apr_status_t) apr_os_proc_mutex_get(apr_os_proc_mutex_t *ospmutex,
 
201
                                                apr_proc_mutex_t *mutex)
 
202
{
 
203
    *ospmutex = mutex->handle;
 
204
    return APR_SUCCESS;
 
205
}
 
206
 
 
207
APR_DECLARE(apr_status_t) apr_os_proc_mutex_put(apr_proc_mutex_t **pmutex,
 
208
                                                apr_os_proc_mutex_t *ospmutex,
 
209
                                                apr_pool_t *pool)
 
210
{
 
211
    if (pool == NULL) {
 
212
        return APR_ENOPOOL;
 
213
    }
 
214
    if ((*pmutex) == NULL) {
 
215
        (*pmutex) = (apr_proc_mutex_t *)apr_palloc(pool,
 
216
                                                   sizeof(apr_proc_mutex_t));
 
217
        (*pmutex)->pool = pool;
 
218
    }
 
219
    (*pmutex)->handle = *ospmutex;
 
220
    return APR_SUCCESS;
 
221
}
 
222