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
24static 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
37APR_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
62static 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
85APR_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
90APR_DECLARE(apr_status_t)
91apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
92{
93    return apr_thread_rwlock_rdlock_core(rwlock, 0);
94}
95
96static apr_status_t
97apr_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
127APR_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
132APR_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
137APR_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
160APR_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
165APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock)
166