summaryrefslogtreecommitdiffstats
path: root/locks/os2
diff options
context:
space:
mode:
Diffstat (limited to 'locks/os2')
-rw-r--r--locks/os2/proc_mutex.c289
-rw-r--r--locks/os2/thread_cond.c194
-rw-r--r--locks/os2/thread_mutex.c120
-rw-r--r--locks/os2/thread_rwlock.c200
4 files changed, 803 insertions, 0 deletions
diff --git a/locks/os2/proc_mutex.c b/locks/os2/proc_mutex.c
new file mode 100644
index 0000000..a86c208
--- /dev/null
+++ b/locks/os2/proc_mutex.c
@@ -0,0 +1,289 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "apr_general.h"
+#include "apr_lib.h"
+#include "apr_strings.h"
+#include "apr_portable.h"
+#include "apr_arch_proc_mutex.h"
+#include "apr_arch_file_io.h"
+#include <string.h>
+#include <stddef.h>
+
+#define CurrentTid (*_threadid)
+
+static char *fixed_name(const char *fname, apr_pool_t *pool)
+{
+ char *semname;
+
+ if (fname == NULL)
+ semname = NULL;
+ else {
+ /* Semaphores don't live in the file system, fix up the name */
+ while (*fname == '/' || *fname == '\\') {
+ fname++;
+ }
+
+ semname = apr_pstrcat(pool, "/SEM32/", fname, NULL);
+
+ if (semname[8] == ':') {
+ semname[8] = '$';
+ }
+ }
+
+ return semname;
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *vmutex)
+{
+ apr_proc_mutex_t *mutex = vmutex;
+ return apr_proc_mutex_destroy(mutex);
+}
+
+APR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex)
+{
+ return NULL;
+}
+
+APR_DECLARE(apr_lockmech_e) apr_proc_mutex_mech(apr_proc_mutex_t *mutex)
+{
+ return APR_LOCK_DEFAULT;
+}
+
+APR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex)
+{
+ return "os2sem";
+}
+
+APR_DECLARE(const char *) apr_proc_mutex_defname(void)
+{
+ return "os2sem";
+}
+
+
+APR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex,
+ const char *fname,
+ apr_lockmech_e mech,
+ apr_pool_t *pool)
+{
+ apr_proc_mutex_t *new;
+ ULONG rc;
+ char *semname;
+
+ if (mech != APR_LOCK_DEFAULT && mech != APR_LOCK_DEFAULT_TIMED) {
+ return APR_ENOTIMPL;
+ }
+
+ new = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t));
+ new->pool = pool;
+ new->owner = 0;
+ new->lock_count = 0;
+ *mutex = new;
+
+ semname = fixed_name(fname, pool);
+ rc = DosCreateMutexSem(semname, &(new->hMutex), DC_SEM_SHARED, FALSE);
+
+ if (!rc) {
+ apr_pool_cleanup_register(pool, new, apr_proc_mutex_cleanup, apr_pool_cleanup_null);
+ }
+
+ return APR_FROM_OS_ERROR(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex,
+ const char *fname,
+ apr_pool_t *pool)
+{
+ apr_proc_mutex_t *new;
+ ULONG rc;
+ char *semname;
+
+ new = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t));
+ new->pool = pool;
+ new->owner = 0;
+ new->lock_count = 0;
+
+ semname = fixed_name(fname, pool);
+ rc = DosOpenMutexSem(semname, &(new->hMutex));
+ *mutex = new;
+
+ if (!rc) {
+ apr_pool_cleanup_register(pool, new, apr_proc_mutex_cleanup, apr_pool_cleanup_null);
+ }
+
+ return APR_FROM_OS_ERROR(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex)
+{
+ ULONG rc = DosRequestMutexSem(mutex->hMutex, SEM_INDEFINITE_WAIT);
+
+ if (rc == 0) {
+ mutex->owner = CurrentTid;
+ mutex->lock_count++;
+ }
+
+ return APR_FROM_OS_ERROR(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex)
+{
+ ULONG rc = DosRequestMutexSem(mutex->hMutex, SEM_IMMEDIATE_RETURN);
+
+ if (rc == 0) {
+ mutex->owner = CurrentTid;
+ mutex->lock_count++;
+ }
+
+ return (rc == ERROR_TIMEOUT) ? APR_EBUSY : APR_FROM_OS_ERROR(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex,
+ apr_interval_time_t timeout)
+{
+ ULONG rc;
+
+ if (timeout <= 0) {
+ rc = DosRequestMutexSem(mutex->hMutex, SEM_IMMEDIATE_RETURN);
+ }
+ else {
+ rc = DosRequestMutexSem(mutex->hMutex, apr_time_as_msec(timeout));
+ }
+
+ if (rc == 0) {
+ mutex->owner = CurrentTid;
+ mutex->lock_count++;
+ }
+
+ return (rc == ERROR_TIMEOUT) ? APR_TIMEUP : APR_FROM_OS_ERROR(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex)
+{
+ ULONG rc;
+
+ if (mutex->owner == CurrentTid && mutex->lock_count > 0) {
+ mutex->lock_count--;
+ rc = DosReleaseMutexSem(mutex->hMutex);
+ return APR_FROM_OS_ERROR(rc);
+ }
+
+ return APR_SUCCESS;
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex)
+{
+ ULONG rc;
+ apr_status_t status = APR_SUCCESS;
+
+ if (mutex->owner == CurrentTid) {
+ while (mutex->lock_count > 0 && status == APR_SUCCESS) {
+ status = apr_proc_mutex_unlock(mutex);
+ }
+ }
+
+ if (status != APR_SUCCESS) {
+ return status;
+ }
+
+ if (mutex->hMutex == 0) {
+ return APR_SUCCESS;
+ }
+
+ rc = DosCloseMutexSem(mutex->hMutex);
+
+ if (!rc) {
+ mutex->hMutex = 0;
+ }
+
+ return APR_FROM_OS_ERROR(rc);
+}
+
+APR_PERMS_SET_ENOTIMPL(proc_mutex)
+
+APR_POOL_IMPLEMENT_ACCESSOR(proc_mutex)
+
+
+
+/* Implement OS-specific accessors defined in apr_portable.h */
+
+APR_DECLARE(apr_status_t) apr_os_proc_mutex_get_ex(apr_os_proc_mutex_t *ospmutex,
+ apr_proc_mutex_t *pmutex,
+ apr_lockmech_e *mech)
+{
+ *ospmutex = pmutex->hMutex;
+ if (mech) {
+ *mech = APR_LOCK_DEFAULT;
+ }
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_os_proc_mutex_get(apr_os_proc_mutex_t *ospmutex,
+ apr_proc_mutex_t *pmutex)
+{
+ return apr_os_proc_mutex_get_ex(ospmutex, pmutex, NULL);
+}
+
+APR_DECLARE(apr_status_t) apr_os_proc_mutex_put_ex(apr_proc_mutex_t **pmutex,
+ apr_os_proc_mutex_t *ospmutex,
+ apr_lockmech_e mech,
+ int register_cleanup,
+ apr_pool_t *pool)
+{
+ apr_proc_mutex_t *new;
+ if (pool == NULL) {
+ return APR_ENOPOOL;
+ }
+ if (mech != APR_LOCK_DEFAULT && mech != APR_LOCK_DEFAULT_TIMED) {
+ return APR_ENOTIMPL;
+ }
+
+ new = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t));
+ new->pool = pool;
+ new->owner = 0;
+ new->lock_count = 0;
+ new->hMutex = *ospmutex;
+ *pmutex = new;
+
+ if (register_cleanup) {
+ apr_pool_cleanup_register(pool, *pmutex, apr_proc_mutex_cleanup,
+ apr_pool_cleanup_null);
+ }
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_os_proc_mutex_put(apr_proc_mutex_t **pmutex,
+ apr_os_proc_mutex_t *ospmutex,
+ apr_pool_t *pool)
+{
+ return apr_os_proc_mutex_put_ex(pmutex, ospmutex, APR_LOCK_DEFAULT,
+ 0, pool);
+}
+
diff --git a/locks/os2/thread_cond.c b/locks/os2/thread_cond.c
new file mode 100644
index 0000000..1b8698e
--- /dev/null
+++ b/locks/os2/thread_cond.c
@@ -0,0 +1,194 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "apr_general.h"
+#include "apr_lib.h"
+#include "apr_strings.h"
+#include "apr_portable.h"
+#include "apr_arch_thread_mutex.h"
+#include "apr_arch_thread_cond.h"
+#include "apr_arch_file_io.h"
+#include <string.h>
+
+#ifndef DCE_POSTONE
+#define DCE_POSTONE 0x0800 // Post one flag
+#endif
+
+static apr_status_t thread_cond_cleanup(void *data)
+{
+ apr_thread_cond_t *cv = data;
+
+ if (cv->semaphore) {
+ DosCloseEventSem(cv->semaphore);
+ }
+
+ if (cv->mutex) {
+ DosCloseMutexSem(cv->mutex);
+ }
+
+ return APR_SUCCESS;
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond,
+ apr_pool_t *pool)
+{
+ int rc;
+ apr_thread_cond_t *cv;
+
+ cv = apr_pcalloc(pool, sizeof(**cond));
+ rc = DosCreateEventSem(NULL, &cv->semaphore, DCE_POSTONE, FALSE);
+
+ if (rc == 0) {
+ rc = DosCreateMutexSem(NULL, &cv->mutex, 0, FALSE);
+ }
+
+ *cond = cv;
+ cv->pool = pool;
+ apr_pool_cleanup_register(cv->pool, cv, thread_cond_cleanup,
+ apr_pool_cleanup_null);
+
+ return APR_FROM_OS_ERROR(rc);
+}
+
+
+
+static apr_status_t thread_cond_timedwait(apr_thread_cond_t *cond,
+ apr_thread_mutex_t *mutex,
+ ULONG timeout_ms )
+{
+ ULONG rc;
+ apr_status_t rv = APR_SUCCESS;
+ int wake = FALSE;
+ unsigned long generation;
+
+ DosRequestMutexSem(cond->mutex, SEM_INDEFINITE_WAIT);
+ cond->num_waiting++;
+ generation = cond->generation;
+ DosReleaseMutexSem(cond->mutex);
+
+ apr_thread_mutex_unlock(mutex);
+
+ do {
+ rc = DosWaitEventSem(cond->semaphore, timeout_ms);
+
+ DosRequestMutexSem(cond->mutex, SEM_INDEFINITE_WAIT);
+
+ if (cond->num_wake) {
+ if (cond->generation != generation) {
+ cond->num_wake--;
+ cond->num_waiting--;
+ rv = APR_SUCCESS;
+ break;
+ } else {
+ wake = TRUE;
+ }
+ }
+ else if (rc != 0) {
+ cond->num_waiting--;
+ rv = APR_TIMEUP;
+ break;
+ }
+
+ DosReleaseMutexSem(cond->mutex);
+
+ if (wake) {
+ wake = FALSE;
+ DosPostEventSem(cond->semaphore);
+ }
+ } while (1);
+
+ DosReleaseMutexSem(cond->mutex);
+ apr_thread_mutex_lock(mutex);
+ return rv;
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond,
+ apr_thread_mutex_t *mutex)
+{
+ return thread_cond_timedwait(cond, mutex, SEM_INDEFINITE_WAIT);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond,
+ apr_thread_mutex_t *mutex,
+ apr_interval_time_t timeout)
+{
+ ULONG timeout_ms = (timeout >= 0) ? apr_time_as_msec(timeout)
+ : SEM_INDEFINITE_WAIT;
+ return thread_cond_timedwait(cond, mutex, timeout_ms);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond)
+{
+ int wake = FALSE;
+
+ DosRequestMutexSem(cond->mutex, SEM_INDEFINITE_WAIT);
+
+ if (cond->num_waiting > cond->num_wake) {
+ wake = TRUE;
+ cond->num_wake++;
+ cond->generation++;
+ }
+
+ DosReleaseMutexSem(cond->mutex);
+
+ if (wake) {
+ DosPostEventSem(cond->semaphore);
+ }
+
+ return APR_SUCCESS;
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond)
+{
+ unsigned long num_wake = 0;
+
+ DosRequestMutexSem(cond->mutex, SEM_INDEFINITE_WAIT);
+
+ if (cond->num_waiting > cond->num_wake) {
+ num_wake = cond->num_waiting - cond->num_wake;
+ cond->num_wake = cond->num_waiting;
+ cond->generation++;
+ }
+
+ DosReleaseMutexSem(cond->mutex);
+
+ for (; num_wake; num_wake--) {
+ DosPostEventSem(cond->semaphore);
+ }
+
+ return APR_SUCCESS;
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond)
+{
+ return apr_pool_cleanup_run(cond->pool, cond, thread_cond_cleanup);
+}
+
+
+
+APR_POOL_IMPLEMENT_ACCESSOR(thread_cond)
diff --git a/locks/os2/thread_mutex.c b/locks/os2/thread_mutex.c
new file mode 100644
index 0000000..fea188d
--- /dev/null
+++ b/locks/os2/thread_mutex.c
@@ -0,0 +1,120 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "apr_general.h"
+#include "apr_lib.h"
+#include "apr_strings.h"
+#include "apr_portable.h"
+#include "apr_arch_thread_mutex.h"
+#include "apr_arch_file_io.h"
+#include <string.h>
+#include <stddef.h>
+
+static apr_status_t thread_mutex_cleanup(void *themutex)
+{
+ apr_thread_mutex_t *mutex = themutex;
+ return apr_thread_mutex_destroy(mutex);
+}
+
+
+
+/* XXX: Need to respect APR_THREAD_MUTEX_[UN]NESTED flags argument
+ * or return APR_ENOTIMPL!!!
+ */
+APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
+ unsigned int flags,
+ apr_pool_t *pool)
+{
+ apr_thread_mutex_t *new_mutex;
+ ULONG rc;
+
+ new_mutex = (apr_thread_mutex_t *)apr_palloc(pool, sizeof(apr_thread_mutex_t));
+ new_mutex->pool = pool;
+
+ rc = DosCreateMutexSem(NULL, &(new_mutex->hMutex), 0, FALSE);
+ *mutex = new_mutex;
+
+ if (!rc)
+ apr_pool_cleanup_register(pool, new_mutex, thread_mutex_cleanup, apr_pool_cleanup_null);
+
+ return APR_OS2_STATUS(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex)
+{
+ ULONG rc = DosRequestMutexSem(mutex->hMutex, SEM_INDEFINITE_WAIT);
+ return APR_OS2_STATUS(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex)
+{
+ ULONG rc = DosRequestMutexSem(mutex->hMutex, SEM_IMMEDIATE_RETURN);
+
+ return (rc == ERROR_TIMEOUT) ? APR_EBUSY : APR_FROM_OS_ERROR(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex,
+ apr_interval_time_t timeout)
+{
+ ULONG rc;
+
+ if (timeout <= 0) {
+ rc = DosRequestMutexSem(mutex->hMutex, SEM_IMMEDIATE_RETURN);
+ }
+ else {
+ rc = DosRequestMutexSem(mutex->hMutex, apr_time_as_msec(timeout));
+ }
+
+ return (rc == ERROR_TIMEOUT) ? APR_TIMEUP : APR_FROM_OS_ERROR(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex)
+{
+ ULONG rc = DosReleaseMutexSem(mutex->hMutex);
+ return APR_OS2_STATUS(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex)
+{
+ ULONG rc;
+
+ if (mutex->hMutex == 0)
+ return APR_SUCCESS;
+
+ while (DosReleaseMutexSem(mutex->hMutex) == 0);
+
+ rc = DosCloseMutexSem(mutex->hMutex);
+
+ if (!rc) {
+ mutex->hMutex = 0;
+ return APR_SUCCESS;
+ }
+
+ return APR_FROM_OS_ERROR(rc);
+}
+
+APR_POOL_IMPLEMENT_ACCESSOR(thread_mutex)
+
diff --git a/locks/os2/thread_rwlock.c b/locks/os2/thread_rwlock.c
new file mode 100644
index 0000000..195a56b
--- /dev/null
+++ b/locks/os2/thread_rwlock.c
@@ -0,0 +1,200 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "apr_general.h"
+#include "apr_lib.h"
+#include "apr_strings.h"
+#include "apr_portable.h"
+#include "apr_arch_thread_rwlock.h"
+#include "apr_arch_file_io.h"
+#include <string.h>
+
+static apr_status_t thread_rwlock_cleanup(void *therwlock)
+{
+ apr_thread_rwlock_t *rwlock = therwlock;
+ return apr_thread_rwlock_destroy(rwlock);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_rwlock_create(apr_thread_rwlock_t **rwlock,
+ apr_pool_t *pool)
+{
+ apr_thread_rwlock_t *new_rwlock;
+ ULONG rc;
+
+ new_rwlock = (apr_thread_rwlock_t *)apr_palloc(pool, sizeof(apr_thread_rwlock_t));
+ new_rwlock->pool = pool;
+ new_rwlock->readers = 0;
+
+ rc = DosCreateMutexSem(NULL, &(new_rwlock->write_lock), 0, FALSE);
+
+ if (rc)
+ return APR_FROM_OS_ERROR(rc);
+
+ rc = DosCreateEventSem(NULL, &(new_rwlock->read_done), 0, FALSE);
+
+ if (rc)
+ return APR_FROM_OS_ERROR(rc);
+
+ *rwlock = new_rwlock;
+
+ if (!rc)
+ apr_pool_cleanup_register(pool, new_rwlock, thread_rwlock_cleanup,
+ apr_pool_cleanup_null);
+
+ return APR_FROM_OS_ERROR(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_rwlock_rdlock(apr_thread_rwlock_t *rwlock)
+{
+ ULONG rc, posts;
+
+ rc = DosRequestMutexSem(rwlock->write_lock, SEM_INDEFINITE_WAIT);
+
+ if (rc)
+ return APR_FROM_OS_ERROR(rc);
+
+ /* We've successfully acquired the writer mutex so we can't be locked
+ * for write which means it's ok to add a reader lock. The writer mutex
+ * doubles as race condition protection for the readers counter.
+ */
+ rwlock->readers++;
+ DosResetEventSem(rwlock->read_done, &posts);
+ rc = DosReleaseMutexSem(rwlock->write_lock);
+ return APR_FROM_OS_ERROR(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
+{
+ /* As above but with different wait time */
+ ULONG rc, posts;
+
+ rc = DosRequestMutexSem(rwlock->write_lock, SEM_IMMEDIATE_RETURN);
+
+ if (rc)
+ return APR_FROM_OS_ERROR(rc);
+
+ rwlock->readers++;
+ DosResetEventSem(rwlock->read_done, &posts);
+ rc = DosReleaseMutexSem(rwlock->write_lock);
+ return APR_FROM_OS_ERROR(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_rwlock_wrlock(apr_thread_rwlock_t *rwlock)
+{
+ ULONG rc;
+
+ rc = DosRequestMutexSem(rwlock->write_lock, SEM_INDEFINITE_WAIT);
+
+ if (rc)
+ return APR_FROM_OS_ERROR(rc);
+
+ /* We've got the writer lock but we have to wait for all readers to
+ * unlock before it's ok to use it
+ */
+
+ if (rwlock->readers) {
+ rc = DosWaitEventSem(rwlock->read_done, SEM_INDEFINITE_WAIT);
+
+ if (rc)
+ DosReleaseMutexSem(rwlock->write_lock);
+ }
+
+ return APR_FROM_OS_ERROR(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_rwlock_trywrlock(apr_thread_rwlock_t *rwlock)
+{
+ ULONG rc;
+
+ rc = DosRequestMutexSem(rwlock->write_lock, SEM_IMMEDIATE_RETURN);
+
+ if (rc)
+ return APR_FROM_OS_ERROR(rc);
+
+ /* We've got the writer lock but we have to wait for all readers to
+ * unlock before it's ok to use it
+ */
+
+ if (rwlock->readers) {
+ /* There are readers active, give up */
+ DosReleaseMutexSem(rwlock->write_lock);
+ rc = ERROR_TIMEOUT;
+ }
+
+ return APR_FROM_OS_ERROR(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_rwlock_unlock(apr_thread_rwlock_t *rwlock)
+{
+ ULONG rc;
+
+ /* First, guess that we're unlocking a writer */
+ rc = DosReleaseMutexSem(rwlock->write_lock);
+
+ if (rc == ERROR_NOT_OWNER) {
+ /* Nope, we must have a read lock */
+ if (rwlock->readers) {
+ DosEnterCritSec();
+ rwlock->readers--;
+
+ if (rwlock->readers == 0) {
+ DosPostEventSem(rwlock->read_done);
+ }
+
+ DosExitCritSec();
+ rc = 0;
+ }
+ }
+
+ return APR_FROM_OS_ERROR(rc);
+}
+
+
+
+APR_DECLARE(apr_status_t) apr_thread_rwlock_destroy(apr_thread_rwlock_t *rwlock)
+{
+ ULONG rc;
+
+ if (rwlock->write_lock == 0)
+ return APR_SUCCESS;
+
+ while (DosReleaseMutexSem(rwlock->write_lock) == 0);
+
+ rc = DosCloseMutexSem(rwlock->write_lock);
+
+ if (!rc) {
+ rwlock->write_lock = 0;
+ DosCloseEventSem(rwlock->read_done);
+ return APR_SUCCESS;
+ }
+
+ return APR_FROM_OS_ERROR(rc);
+}
+
+APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock)
+