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

« back to all changes in this revision

Viewing changes to srclib/apr/locks/win32/thread_rwlock.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 "win32/apr_arch_thread_rwlock.h"
 
22
#include "apr_portable.h"
 
23
 
 
24
static apr_status_t thread_rwlock_cleanup(void *data)
 
25
{
 
26
    apr_thread_rwlock_t *rwlock = data;
 
27
    
 
28
    if (! CloseHandle(rwlock->read_event))
 
29
        return apr_get_os_error();
 
30
 
 
31
    if (! CloseHandle(rwlock->write_mutex))
 
32
        return apr_get_os_error();
 
33
    
 
34
    return APR_SUCCESS;
 
35
}
 
36
 
 
37
APR_DECLARE(apr_status_t)apr_thread_rwlock_create(apr_thread_rwlock_t **rwlock,
 
38
                                                  apr_pool_t *pool)
 
39
{
 
40
    *rwlock = apr_palloc(pool, sizeof(**rwlock));
 
41
 
 
42
    (*rwlock)->pool        = pool;
 
43
    (*rwlock)->readers     = 0;
 
44
 
 
45
    if (! ((*rwlock)->read_event = CreateEvent(NULL, TRUE, FALSE, NULL))) {
 
46
        *rwlock = NULL;
 
47
        return apr_get_os_error();
 
48
    }
 
49
 
 
50
    if (! ((*rwlock)->write_mutex = CreateMutex(NULL, FALSE, NULL))) {
 
51
        CloseHandle((*rwlock)->read_event);
 
52
        *rwlock = NULL;
 
53
        return apr_get_os_error();
 
54
    }
 
55
 
 
56
    apr_pool_cleanup_register(pool, *rwlock, thread_rwlock_cleanup,
 
57
                              apr_pool_cleanup_null);
 
58
 
 
59
    return APR_SUCCESS;
 
60
}
 
61
 
 
62
static apr_status_t apr_thread_rwlock_rdlock_core(apr_thread_rwlock_t *rwlock,
 
63
                                                  DWORD  milliseconds)
 
64
{
 
65
    DWORD   code = WaitForSingleObject(rwlock->write_mutex, milliseconds);
 
66
 
 
67
    if (code == WAIT_FAILED || code == WAIT_TIMEOUT)
 
68
        return APR_FROM_OS_ERROR(code);
 
69
 
 
70
    /* We've successfully acquired the writer mutex, we can't be locked
 
71
     * for write, so it's OK to add the reader lock.  The writer mutex
 
72
     * doubles as race condition protection for the readers counter.   
 
73
     */
 
74
    InterlockedIncrement(&rwlock->readers);
 
75
    
 
76
    if (! ResetEvent(rwlock->read_event))
 
77
        return apr_get_os_error();
 
78
    
 
79
    if (! ReleaseMutex(rwlock->write_mutex))
 
80
        return apr_get_os_error();
 
81
    
 
82
    return APR_SUCCESS;
 
83
}
 
84
 
 
85
APR_DECLARE(apr_status_t) apr_thread_rwlock_rdlock(apr_thread_rwlock_t *rwlock)
 
86
{
 
87
    return apr_thread_rwlock_rdlock_core(rwlock, INFINITE);
 
88
}
 
89
 
 
90
APR_DECLARE(apr_status_t) 
 
91
apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
 
92
{
 
93
    return apr_thread_rwlock_rdlock_core(rwlock, 0);
 
94
}
 
95
 
 
96
static apr_status_t 
 
97
apr_thread_rwlock_wrlock_core(apr_thread_rwlock_t *rwlock, DWORD milliseconds)
 
98
{
 
99
    DWORD   code = WaitForSingleObject(rwlock->write_mutex, milliseconds);
 
100
 
 
101
    if (code == WAIT_FAILED || code == WAIT_TIMEOUT)
 
102
        return APR_FROM_OS_ERROR(code);
 
103
 
 
104
    /* We've got the writer lock but we have to wait for all readers to
 
105
     * unlock before it's ok to use it.
 
106
     */
 
107
    if (rwlock->readers) {
 
108
        /* Must wait for readers to finish before returning, unless this
 
109
         * is an trywrlock (milliseconds == 0):
 
110
         */
 
111
        code = milliseconds
 
112
          ? WaitForSingleObject(rwlock->read_event, milliseconds)
 
113
          : WAIT_TIMEOUT;
 
114
        
 
115
        if (code == WAIT_FAILED || code == WAIT_TIMEOUT) {
 
116
            /* Unable to wait for readers to finish, release write lock: */
 
117
            if (! ReleaseMutex(rwlock->write_mutex))
 
118
                return apr_get_os_error();
 
119
            
 
120
            return APR_FROM_OS_ERROR(code);
 
121
        }
 
122
    }
 
123
 
 
124
    return APR_SUCCESS;
 
125
}
 
126
 
 
127
APR_DECLARE(apr_status_t) apr_thread_rwlock_wrlock(apr_thread_rwlock_t *rwlock)
 
128
{
 
129
    return apr_thread_rwlock_wrlock_core(rwlock, INFINITE);
 
130
}
 
131
 
 
132
APR_DECLARE(apr_status_t)apr_thread_rwlock_trywrlock(apr_thread_rwlock_t *rwlock)
 
133
{
 
134
    return apr_thread_rwlock_wrlock_core(rwlock, 0);
 
135
}
 
136
 
 
137
APR_DECLARE(apr_status_t) apr_thread_rwlock_unlock(apr_thread_rwlock_t *rwlock)
 
138
{
 
139
    apr_status_t rv = 0;
 
140
 
 
141
    /* First, guess that we're unlocking a writer */
 
142
    if (! ReleaseMutex(rwlock->write_mutex))
 
143
        rv = apr_get_os_error();
 
144
    
 
145
    if (rv == APR_FROM_OS_ERROR(ERROR_NOT_OWNER)) {
 
146
        /* Nope, we must have a read lock */
 
147
        if (rwlock->readers &&
 
148
            ! InterlockedDecrement(&rwlock->readers) &&
 
149
            ! SetEvent(rwlock->read_event)) {
 
150
            rv = apr_get_os_error();
 
151
        }
 
152
        else {
 
153
            rv = 0;
 
154
        }
 
155
    }
 
156
 
 
157
    return rv;
 
158
}
 
159
 
 
160
APR_DECLARE(apr_status_t) apr_thread_rwlock_destroy(apr_thread_rwlock_t *rwlock)
 
161
{
 
162
    return apr_pool_cleanup_run(rwlock->pool, rwlock, thread_rwlock_cleanup);
 
163
}
 
164
 
 
165
APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock)