diff options
author | Brian Havard <bjh@apache.org> | 2001-09-15 03:27:32 +0000 |
---|---|---|
committer | Brian Havard <bjh@apache.org> | 2001-09-15 03:27:32 +0000 |
commit | fd7989efc7fc59e0f80d6a6a36001ff134e2b8d9 (patch) | |
tree | 14cb4acf4469aec53649dc443ba11d1f621c5e11 | |
parent | 702ade51f422ff22f8754926c6d0c9aa9965dd37 (diff) | |
download | apr-fd7989efc7fc59e0f80d6a6a36001ff134e2b8d9.tar.gz |
OS/2: Implement read/write locks. As there's no native equivalent in OS/2
I've hacked up an implementation using a mutex/event semaphore pair. Not
heavily verified but passes a few tests I put it through.
git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@62329 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | include/arch/os2/thread_rwlock.h | 3 | ||||
-rw-r--r-- | locks/os2/thread_rwlock.c | 153 |
2 files changed, 148 insertions, 8 deletions
diff --git a/include/arch/os2/thread_rwlock.h b/include/arch/os2/thread_rwlock.h index d8d153136..c959ffc09 100644 --- a/include/arch/os2/thread_rwlock.h +++ b/include/arch/os2/thread_rwlock.h @@ -60,6 +60,9 @@ struct apr_thread_rwlock_t { apr_pool_t *pool; + int readers; + HMTX write_lock; + HEV read_done; }; #endif /* THREAD_RWLOCK_H */ diff --git a/locks/os2/thread_rwlock.c b/locks/os2/thread_rwlock.c index e769f3f7f..59cc5639a 100644 --- a/locks/os2/thread_rwlock.c +++ b/locks/os2/thread_rwlock.c @@ -60,39 +60,176 @@ #include "fileio.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) { - return APR_ENOTIMPL; + 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) { - return APR_ENOTIMPL; + 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) { - return APR_ENOTIMPL; + /* 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) { - return APR_ENOTIMPL; + 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) { - return APR_ENOTIMPL; + 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) { - return APR_ENOTIMPL; + 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) { - return APR_ENOTIMPL; -} + 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); +} |