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
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 apr_thread_rwlock_t *rwlock = data;
28 if (! CloseHandle(rwlock->read_event))
29 return apr_get_os_error();
31 if (! CloseHandle(rwlock->write_mutex))
32 return apr_get_os_error();
37 APR_DECLARE(apr_status_t)apr_thread_rwlock_create(apr_thread_rwlock_t **rwlock,
40 *rwlock = apr_palloc(pool, sizeof(**rwlock));
42 (*rwlock)->pool = pool;
43 (*rwlock)->readers = 0;
45 if (! ((*rwlock)->read_event = CreateEvent(NULL, TRUE, FALSE, NULL))) {
47 return apr_get_os_error();
50 if (! ((*rwlock)->write_mutex = CreateMutex(NULL, FALSE, NULL))) {
51 CloseHandle((*rwlock)->read_event);
53 return apr_get_os_error();
56 apr_pool_cleanup_register(pool, *rwlock, thread_rwlock_cleanup,
57 apr_pool_cleanup_null);
62 static apr_status_t apr_thread_rwlock_rdlock_core(apr_thread_rwlock_t *rwlock,
65 DWORD code = WaitForSingleObject(rwlock->write_mutex, milliseconds);
67 if (code == WAIT_FAILED || code == WAIT_TIMEOUT)
68 return APR_FROM_OS_ERROR(code);
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.
74 InterlockedIncrement(&rwlock->readers);
76 if (! ResetEvent(rwlock->read_event))
77 return apr_get_os_error();
79 if (! ReleaseMutex(rwlock->write_mutex))
80 return apr_get_os_error();
85 APR_DECLARE(apr_status_t) apr_thread_rwlock_rdlock(apr_thread_rwlock_t *rwlock)
87 return apr_thread_rwlock_rdlock_core(rwlock, INFINITE);
90 APR_DECLARE(apr_status_t)
91 apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
93 return apr_thread_rwlock_rdlock_core(rwlock, 0);
97 apr_thread_rwlock_wrlock_core(apr_thread_rwlock_t *rwlock, DWORD milliseconds)
99 DWORD code = WaitForSingleObject(rwlock->write_mutex, milliseconds);
101 if (code == WAIT_FAILED || code == WAIT_TIMEOUT)
102 return APR_FROM_OS_ERROR(code);
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.
107 if (rwlock->readers) {
108 /* Must wait for readers to finish before returning, unless this
109 * is an trywrlock (milliseconds == 0):
112 ? WaitForSingleObject(rwlock->read_event, milliseconds)
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();
120 return APR_FROM_OS_ERROR(code);
127 APR_DECLARE(apr_status_t) apr_thread_rwlock_wrlock(apr_thread_rwlock_t *rwlock)
129 return apr_thread_rwlock_wrlock_core(rwlock, INFINITE);
132 APR_DECLARE(apr_status_t)apr_thread_rwlock_trywrlock(apr_thread_rwlock_t *rwlock)
134 return apr_thread_rwlock_wrlock_core(rwlock, 0);
137 APR_DECLARE(apr_status_t) apr_thread_rwlock_unlock(apr_thread_rwlock_t *rwlock)
141 /* First, guess that we're unlocking a writer */
142 if (! ReleaseMutex(rwlock->write_mutex))
143 rv = apr_get_os_error();
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();
160 APR_DECLARE(apr_status_t) apr_thread_rwlock_destroy(apr_thread_rwlock_t *rwlock)
162 return apr_pool_cleanup_run(rwlock->pool, rwlock, thread_rwlock_cleanup);
165 APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock)