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.
17 #include "apr_general.h"
19 #include "apr_strings.h"
20 #include "apr_portable.h"
21 #include "apr_arch_thread_rwlock.h"
22 #include "apr_arch_file_io.h"
25 static apr_status_t thread_rwlock_cleanup(void *therwlock)
27 apr_thread_rwlock_t *rwlock = therwlock;
28 return apr_thread_rwlock_destroy(rwlock);
33 APR_DECLARE(apr_status_t) apr_thread_rwlock_create(apr_thread_rwlock_t **rwlock,
36 apr_thread_rwlock_t *new_rwlock;
39 new_rwlock = (apr_thread_rwlock_t *)apr_palloc(pool, sizeof(apr_thread_rwlock_t));
40 new_rwlock->pool = pool;
41 new_rwlock->readers = 0;
43 rc = DosCreateMutexSem(NULL, &(new_rwlock->write_lock), 0, FALSE);
46 return APR_FROM_OS_ERROR(rc);
48 rc = DosCreateEventSem(NULL, &(new_rwlock->read_done), 0, FALSE);
51 return APR_FROM_OS_ERROR(rc);
56 apr_pool_cleanup_register(pool, new_rwlock, thread_rwlock_cleanup,
57 apr_pool_cleanup_null);
59 return APR_FROM_OS_ERROR(rc);
64 APR_DECLARE(apr_status_t) apr_thread_rwlock_rdlock(apr_thread_rwlock_t *rwlock)
68 rc = DosRequestMutexSem(rwlock->write_lock, SEM_INDEFINITE_WAIT);
71 return APR_FROM_OS_ERROR(rc);
73 /* We've successfully acquired the writer mutex so we can't be locked
74 * for write which means it's ok to add a reader lock. The writer mutex
75 * doubles as race condition protection for the readers counter.
78 DosResetEventSem(rwlock->read_done, &posts);
79 rc = DosReleaseMutexSem(rwlock->write_lock);
80 return APR_FROM_OS_ERROR(rc);
85 APR_DECLARE(apr_status_t) apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
87 /* As above but with different wait time */
90 rc = DosRequestMutexSem(rwlock->write_lock, SEM_IMMEDIATE_RETURN);
93 return APR_FROM_OS_ERROR(rc);
96 DosResetEventSem(rwlock->read_done, &posts);
97 rc = DosReleaseMutexSem(rwlock->write_lock);
98 return APR_FROM_OS_ERROR(rc);
103 APR_DECLARE(apr_status_t) apr_thread_rwlock_wrlock(apr_thread_rwlock_t *rwlock)
107 rc = DosRequestMutexSem(rwlock->write_lock, SEM_INDEFINITE_WAIT);
110 return APR_FROM_OS_ERROR(rc);
112 /* We've got the writer lock but we have to wait for all readers to
113 * unlock before it's ok to use it
116 if (rwlock->readers) {
117 rc = DosWaitEventSem(rwlock->read_done, SEM_INDEFINITE_WAIT);
120 DosReleaseMutexSem(rwlock->write_lock);
123 return APR_FROM_OS_ERROR(rc);
128 APR_DECLARE(apr_status_t) apr_thread_rwlock_trywrlock(apr_thread_rwlock_t *rwlock)
132 rc = DosRequestMutexSem(rwlock->write_lock, SEM_IMMEDIATE_RETURN);
135 return APR_FROM_OS_ERROR(rc);
137 /* We've got the writer lock but we have to wait for all readers to
138 * unlock before it's ok to use it
141 if (rwlock->readers) {
142 /* There are readers active, give up */
143 DosReleaseMutexSem(rwlock->write_lock);
147 return APR_FROM_OS_ERROR(rc);
152 APR_DECLARE(apr_status_t) apr_thread_rwlock_unlock(apr_thread_rwlock_t *rwlock)
156 /* First, guess that we're unlocking a writer */
157 rc = DosReleaseMutexSem(rwlock->write_lock);
159 if (rc == ERROR_NOT_OWNER) {
160 /* Nope, we must have a read lock */
161 if (rwlock->readers) {
165 if (rwlock->readers == 0) {
166 DosPostEventSem(rwlock->read_done);
174 return APR_FROM_OS_ERROR(rc);
179 APR_DECLARE(apr_status_t) apr_thread_rwlock_destroy(apr_thread_rwlock_t *rwlock)
183 if (rwlock->write_lock == 0)
186 while (DosReleaseMutexSem(rwlock->write_lock) == 0);
188 rc = DosCloseMutexSem(rwlock->write_lock);
191 rwlock->write_lock = 0;
192 DosCloseEventSem(rwlock->read_done);
196 return APR_FROM_OS_ERROR(rc);
199 APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock)