Apache HTTPD
thread_rwlock.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_arch_thread_rwlock.h"
22#include "apr_portable.h"
23
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
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
58
59 return APR_SUCCESS;
60}
61
64{
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 */
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
86{
88}
89
95
96static apr_status_t
98{
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 */
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
128{
130}
131
133{
134 return apr_thread_rwlock_wrlock_core(rwlock, 0);
135}
136
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
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
161{
162 return apr_pool_cleanup_run(rwlock->pool, rwlock, thread_rwlock_cleanup);
163}
164
#define TRUE
Definition abts.h:38
#define FALSE
Definition abts.h:35
#define SetEvent(h)
#define CloseHandle(h)
#define WaitForSingleObject(h, d)
APR Miscellaneous library routines.
APR Portability Routines.
APR Strings library.
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_FROM_OS_ERROR(e)
Definition apr_errno.h:1214
#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
void * data
#define APR_POOL_IMPLEMENT_ACCESSOR(type)
Definition apr_pools.h:91
return NULL
Definition mod_so.c:359
static apr_status_t thread_rwlock_cleanup(void *data)
typedef DWORD(WINAPI *apr_winapi_fpt_GetCompressedFileSizeA)(IN LPCSTR lpFileName
apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
static apr_status_t apr_thread_rwlock_wrlock_core(apr_thread_rwlock_t *rwlock, DWORD milliseconds)
static apr_status_t apr_thread_rwlock_rdlock_core(apr_thread_rwlock_t *rwlock, DWORD milliseconds)