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 "win32/apr_arch_thread_rwlock.h"
22
#include "apr_portable.h"
24
static apr_status_t thread_rwlock_cleanup(void *data)
26
return apr_thread_rwlock_destroy((apr_thread_rwlock_t *) data);
29
APR_DECLARE(apr_status_t)apr_thread_rwlock_create(apr_thread_rwlock_t **rwlock,
32
*rwlock = apr_palloc(pool, sizeof(**rwlock));
34
(*rwlock)->pool = pool;
35
(*rwlock)->readers = 0;
37
if (! ((*rwlock)->read_event = CreateEvent(NULL, TRUE, FALSE, NULL))) {
39
return apr_get_os_error();
42
if (! ((*rwlock)->write_mutex = CreateMutex(NULL, FALSE, NULL))) {
43
CloseHandle((*rwlock)->read_event);
45
return apr_get_os_error();
48
apr_pool_cleanup_register(pool, *rwlock, thread_rwlock_cleanup,
49
apr_pool_cleanup_null);
54
static apr_status_t apr_thread_rwlock_rdlock_core(apr_thread_rwlock_t *rwlock,
57
DWORD code = WaitForSingleObject(rwlock->write_mutex, milliseconds);
59
if (code == WAIT_FAILED || code == WAIT_TIMEOUT)
60
return APR_FROM_OS_ERROR(code);
62
/* We've successfully acquired the writer mutex, we can't be locked
63
* for write, so it's OK to add the reader lock. The writer mutex
64
* doubles as race condition protection for the readers counter.
66
InterlockedIncrement(&rwlock->readers);
68
if (! ResetEvent(rwlock->read_event))
69
return apr_get_os_error();
71
if (! ReleaseMutex(rwlock->write_mutex))
72
return apr_get_os_error();
77
APR_DECLARE(apr_status_t) apr_thread_rwlock_rdlock(apr_thread_rwlock_t *rwlock)
79
return apr_thread_rwlock_rdlock_core(rwlock, INFINITE);
82
APR_DECLARE(apr_status_t)
83
apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
85
return apr_thread_rwlock_rdlock_core(rwlock, 0);
89
apr_thread_rwlock_wrlock_core(apr_thread_rwlock_t *rwlock, DWORD milliseconds)
91
DWORD code = WaitForSingleObject(rwlock->write_mutex, milliseconds);
93
if (code == WAIT_FAILED || code == WAIT_TIMEOUT)
94
return APR_FROM_OS_ERROR(code);
96
/* We've got the writer lock but we have to wait for all readers to
97
* unlock before it's ok to use it.
99
if (rwlock->readers) {
100
/* Must wait for readers to finish before returning, unless this
101
* is an trywrlock (milliseconds == 0):
104
? WaitForSingleObject(rwlock->read_event, milliseconds)
107
if (code == WAIT_FAILED || code == WAIT_TIMEOUT) {
108
/* Unable to wait for readers to finish, release write lock: */
109
if (! ReleaseMutex(rwlock->write_mutex))
110
return apr_get_os_error();
112
return APR_FROM_OS_ERROR(code);
119
APR_DECLARE(apr_status_t) apr_thread_rwlock_wrlock(apr_thread_rwlock_t *rwlock)
121
return apr_thread_rwlock_wrlock_core(rwlock, INFINITE);
124
APR_DECLARE(apr_status_t)apr_thread_rwlock_trywrlock(apr_thread_rwlock_t *rwlock)
126
return apr_thread_rwlock_wrlock_core(rwlock, 0);
129
APR_DECLARE(apr_status_t) apr_thread_rwlock_unlock(apr_thread_rwlock_t *rwlock)
133
/* First, guess that we're unlocking a writer */
134
if (! ReleaseMutex(rwlock->write_mutex))
135
rv = apr_get_os_error();
137
if (rv == APR_FROM_OS_ERROR(ERROR_NOT_OWNER)) {
138
/* Nope, we must have a read lock */
139
if (rwlock->readers &&
140
! InterlockedDecrement(&rwlock->readers) &&
141
! SetEvent(rwlock->read_event)) {
142
rv = apr_get_os_error();
152
APR_DECLARE(apr_status_t) apr_thread_rwlock_destroy(apr_thread_rwlock_t *rwlock)
154
if (! CloseHandle(rwlock->read_event))
155
return apr_get_os_error();
157
if (! CloseHandle(rwlock->write_mutex))
158
return apr_get_os_error();
163
APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock)