feat:replace boost library with C++11 std library
This commit is contained in:
+206
@@ -0,0 +1,206 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/*Read/Write locking implementation based on the MultiLock code from
|
||||
* Stephen Beaulieu <hippo@be.com>
|
||||
*/
|
||||
|
||||
#include "apr_arch_proc_mutex.h"
|
||||
#include "apr_strings.h"
|
||||
#include "apr_portable.h"
|
||||
|
||||
static apr_status_t _proc_mutex_cleanup(void * data)
|
||||
{
|
||||
apr_proc_mutex_t *lock = (apr_proc_mutex_t*)data;
|
||||
if (lock->LockCount != 0) {
|
||||
/* we're still locked... */
|
||||
while (atomic_add(&lock->LockCount , -1) > 1){
|
||||
/* OK we had more than one person waiting on the lock so
|
||||
* the sem is also locked. Release it until we have no more
|
||||
* locks left.
|
||||
*/
|
||||
release_sem (lock->Lock);
|
||||
}
|
||||
}
|
||||
delete_sem(lock->Lock);
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
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;
|
||||
apr_status_t stat = APR_SUCCESS;
|
||||
|
||||
if (mech != APR_LOCK_DEFAULT) {
|
||||
return APR_ENOTIMPL;
|
||||
}
|
||||
|
||||
new = (apr_proc_mutex_t *)apr_pcalloc(pool, sizeof(apr_proc_mutex_t));
|
||||
if (new == NULL){
|
||||
return APR_ENOMEM;
|
||||
}
|
||||
|
||||
if ((stat = create_sem(0, "APR_Lock")) < B_NO_ERROR) {
|
||||
_proc_mutex_cleanup(new);
|
||||
return stat;
|
||||
}
|
||||
new->LockCount = 0;
|
||||
new->Lock = stat;
|
||||
new->pool = pool;
|
||||
|
||||
apr_pool_cleanup_register(new->pool, (void *)new, _proc_mutex_cleanup,
|
||||
apr_pool_cleanup_null);
|
||||
|
||||
(*mutex) = new;
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex,
|
||||
const char *fname,
|
||||
apr_pool_t *pool)
|
||||
{
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex)
|
||||
{
|
||||
int32 stat;
|
||||
|
||||
if (atomic_add(&mutex->LockCount, 1) > 0) {
|
||||
if ((stat = acquire_sem(mutex->Lock)) < B_NO_ERROR) {
|
||||
atomic_add(&mutex->LockCount, -1);
|
||||
return stat;
|
||||
}
|
||||
}
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex)
|
||||
{
|
||||
return APR_ENOTIMPL;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex)
|
||||
{
|
||||
int32 stat;
|
||||
|
||||
if (atomic_add(&mutex->LockCount, -1) > 1) {
|
||||
if ((stat = release_sem(mutex->Lock)) < B_NO_ERROR) {
|
||||
atomic_add(&mutex->LockCount, 1);
|
||||
return stat;
|
||||
}
|
||||
}
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex)
|
||||
{
|
||||
apr_status_t stat;
|
||||
if ((stat = _proc_mutex_cleanup(mutex)) == APR_SUCCESS) {
|
||||
apr_pool_cleanup_kill(mutex->pool, mutex, _proc_mutex_cleanup);
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
return stat;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *mutex)
|
||||
{
|
||||
return _proc_mutex_cleanup(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 "beossem";
|
||||
}
|
||||
|
||||
APR_DECLARE(const char *) apr_proc_mutex_defname(void)
|
||||
{
|
||||
return "beossem";
|
||||
}
|
||||
|
||||
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->sem = pmutex->Lock;
|
||||
ospmutex->ben = pmutex->LockCount;
|
||||
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)
|
||||
{
|
||||
if (pool == NULL) {
|
||||
return APR_ENOPOOL;
|
||||
}
|
||||
if (mech != APR_LOCK_DEFAULT) {
|
||||
return APR_ENOTIMPL;
|
||||
}
|
||||
|
||||
if ((*pmutex) == NULL) {
|
||||
(*pmutex) = (apr_proc_mutex_t *)apr_pcalloc(pool, sizeof(apr_proc_mutex_t));
|
||||
(*pmutex)->pool = pool;
|
||||
}
|
||||
(*pmutex)->Lock = ospmutex->sem;
|
||||
(*pmutex)->LockCount = ospmutex->ben;
|
||||
|
||||
if (register_cleanup) {
|
||||
apr_pool_cleanup_register(pool, *pmutex, _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);
|
||||
}
|
||||
|
||||
+186
@@ -0,0 +1,186 @@
|
||||
/* 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_arch_thread_mutex.h"
|
||||
#include "apr_arch_thread_cond.h"
|
||||
#include "apr_strings.h"
|
||||
#include "apr_portable.h"
|
||||
|
||||
static apr_status_t thread_cond_cleanup(void *data)
|
||||
{
|
||||
struct waiter *w;
|
||||
apr_thread_cond_t *cond = (apr_thread_cond_t *)data;
|
||||
|
||||
acquire_sem(cond->lock);
|
||||
delete_sem(cond->lock);
|
||||
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
static struct waiter_t *make_waiter(apr_pool_t *pool)
|
||||
{
|
||||
struct waiter_t *w = (struct waiter_t*)
|
||||
apr_palloc(pool, sizeof(struct waiter_t));
|
||||
if (w == NULL)
|
||||
return NULL;
|
||||
|
||||
w->sem = create_sem(0, "apr conditional waiter");
|
||||
if (w->sem < 0)
|
||||
return NULL;
|
||||
|
||||
APR_RING_ELEM_INIT(w, link);
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond,
|
||||
apr_pool_t *pool)
|
||||
{
|
||||
apr_thread_cond_t *new_cond;
|
||||
sem_id rv;
|
||||
int i;
|
||||
|
||||
new_cond = (apr_thread_cond_t *)apr_palloc(pool, sizeof(apr_thread_cond_t));
|
||||
|
||||
if (new_cond == NULL)
|
||||
return APR_ENOMEM;
|
||||
|
||||
if ((rv = create_sem(1, "apr conditional lock")) < B_OK)
|
||||
return rv;
|
||||
|
||||
new_cond->lock = rv;
|
||||
new_cond->pool = pool;
|
||||
APR_RING_INIT(&new_cond->alist, waiter_t, link);
|
||||
APR_RING_INIT(&new_cond->flist, waiter_t, link);
|
||||
|
||||
for (i=0;i < 10 ;i++) {
|
||||
struct waiter_t *nw = make_waiter(pool);
|
||||
APR_RING_INSERT_TAIL(&new_cond->flist, nw, waiter_t, link);
|
||||
}
|
||||
|
||||
apr_pool_cleanup_register(new_cond->pool,
|
||||
(void *)new_cond, thread_cond_cleanup,
|
||||
apr_pool_cleanup_null);
|
||||
|
||||
*cond = new_cond;
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static apr_status_t do_wait(apr_thread_cond_t *cond, apr_thread_mutex_t *mutex,
|
||||
int timeout)
|
||||
{
|
||||
struct waiter_t *wait;
|
||||
thread_id cth = find_thread(NULL);
|
||||
apr_status_t rv;
|
||||
int flags = B_RELATIVE_TIMEOUT;
|
||||
|
||||
/* We must be the owner of the mutex or we can't do this... */
|
||||
if (mutex->owner != cth) {
|
||||
/* What should we return??? */
|
||||
return APR_EINVAL;
|
||||
}
|
||||
|
||||
acquire_sem(cond->lock);
|
||||
wait = APR_RING_FIRST(&cond->flist);
|
||||
if (wait)
|
||||
APR_RING_REMOVE(wait, link);
|
||||
else
|
||||
wait = make_waiter(cond->pool);
|
||||
APR_RING_INSERT_TAIL(&cond->alist, wait, waiter_t, link);
|
||||
cond->condlock = mutex;
|
||||
release_sem(cond->lock);
|
||||
|
||||
apr_thread_mutex_unlock(cond->condlock);
|
||||
|
||||
if (timeout == 0)
|
||||
flags = 0;
|
||||
|
||||
rv = acquire_sem_etc(wait->sem, 1, flags, timeout);
|
||||
|
||||
apr_thread_mutex_lock(cond->condlock);
|
||||
|
||||
if (rv != B_OK) {
|
||||
if (rv == B_TIMED_OUT)
|
||||
return APR_TIMEUP;
|
||||
return rv;
|
||||
}
|
||||
|
||||
acquire_sem(cond->lock);
|
||||
APR_RING_REMOVE(wait, link);
|
||||
APR_RING_INSERT_TAIL(&cond->flist, wait, waiter_t, link);
|
||||
release_sem(cond->lock);
|
||||
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond,
|
||||
apr_thread_mutex_t *mutex)
|
||||
{
|
||||
return do_wait(cond, mutex, 0);
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond,
|
||||
apr_thread_mutex_t *mutex,
|
||||
apr_interval_time_t timeout)
|
||||
{
|
||||
return do_wait(cond, mutex, timeout);
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond)
|
||||
{
|
||||
struct waiter_t *wake;
|
||||
|
||||
acquire_sem(cond->lock);
|
||||
if (!APR_RING_EMPTY(&cond->alist, waiter_t, link)) {
|
||||
wake = APR_RING_FIRST(&cond->alist);
|
||||
APR_RING_REMOVE(wake, link);
|
||||
release_sem(wake->sem);
|
||||
APR_RING_INSERT_TAIL(&cond->flist, wake, waiter_t, link);
|
||||
}
|
||||
release_sem(cond->lock);
|
||||
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond)
|
||||
{
|
||||
struct waiter_t *wake;
|
||||
|
||||
acquire_sem(cond->lock);
|
||||
while (! APR_RING_EMPTY(&cond->alist, waiter_t, link)) {
|
||||
wake = APR_RING_FIRST(&cond->alist);
|
||||
APR_RING_REMOVE(wake, link);
|
||||
release_sem(wake->sem);
|
||||
APR_RING_INSERT_TAIL(&cond->flist, wake, waiter_t, link);
|
||||
}
|
||||
release_sem(cond->lock);
|
||||
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond)
|
||||
{
|
||||
apr_status_t stat;
|
||||
if ((stat = thread_cond_cleanup(cond)) == APR_SUCCESS) {
|
||||
apr_pool_cleanup_kill(cond->pool, cond, thread_cond_cleanup);
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
return stat;
|
||||
}
|
||||
|
||||
APR_POOL_IMPLEMENT_ACCESSOR(thread_cond)
|
||||
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/*Read/Write locking implementation based on the MultiLock code from
|
||||
* Stephen Beaulieu <hippo@be.com>
|
||||
*/
|
||||
|
||||
#include "apr_arch_thread_mutex.h"
|
||||
#include "apr_strings.h"
|
||||
#include "apr_portable.h"
|
||||
|
||||
static apr_status_t _thread_mutex_cleanup(void * data)
|
||||
{
|
||||
apr_thread_mutex_t *lock = (apr_thread_mutex_t*)data;
|
||||
if (lock->LockCount != 0) {
|
||||
/* we're still locked... */
|
||||
while (atomic_add(&lock->LockCount , -1) > 1){
|
||||
/* OK we had more than one person waiting on the lock so
|
||||
* the sem is also locked. Release it until we have no more
|
||||
* locks left.
|
||||
*/
|
||||
release_sem (lock->Lock);
|
||||
}
|
||||
}
|
||||
delete_sem(lock->Lock);
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
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_m;
|
||||
apr_status_t stat = APR_SUCCESS;
|
||||
|
||||
new_m = (apr_thread_mutex_t *)apr_pcalloc(pool, sizeof(apr_thread_mutex_t));
|
||||
if (new_m == NULL){
|
||||
return APR_ENOMEM;
|
||||
}
|
||||
|
||||
if ((stat = create_sem(0, "APR_Lock")) < B_NO_ERROR) {
|
||||
_thread_mutex_cleanup(new_m);
|
||||
return stat;
|
||||
}
|
||||
new_m->LockCount = 0;
|
||||
new_m->Lock = stat;
|
||||
new_m->pool = pool;
|
||||
|
||||
/* Optimal default is APR_THREAD_MUTEX_UNNESTED,
|
||||
* no additional checks required for either flag.
|
||||
*/
|
||||
new_m->nested = flags & APR_THREAD_MUTEX_NESTED;
|
||||
|
||||
apr_pool_cleanup_register(new_m->pool, (void *)new_m, _thread_mutex_cleanup,
|
||||
apr_pool_cleanup_null);
|
||||
|
||||
(*mutex) = new_m;
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
#if APR_HAS_CREATE_LOCKS_NP
|
||||
APR_DECLARE(apr_status_t) apr_thread_mutex_create_np(apr_thread_mutex_t **mutex,
|
||||
const char *fname,
|
||||
apr_lockmech_e_np mech,
|
||||
apr_pool_t *pool)
|
||||
{
|
||||
return APR_ENOTIMPL;
|
||||
}
|
||||
#endif
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex)
|
||||
{
|
||||
int32 stat;
|
||||
thread_id me = find_thread(NULL);
|
||||
|
||||
if (mutex->nested && mutex->owner == me) {
|
||||
mutex->owner_ref++;
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
if (atomic_add(&mutex->LockCount, 1) > 0) {
|
||||
if ((stat = acquire_sem(mutex->Lock)) < B_NO_ERROR) {
|
||||
/* Oh dear, acquire_sem failed!! */
|
||||
atomic_add(&mutex->LockCount, -1);
|
||||
return stat;
|
||||
}
|
||||
}
|
||||
|
||||
mutex->owner = me;
|
||||
mutex->owner_ref = 1;
|
||||
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex)
|
||||
{
|
||||
return APR_ENOTIMPL;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex)
|
||||
{
|
||||
int32 stat;
|
||||
|
||||
if (mutex->nested && mutex->owner == find_thread(NULL)) {
|
||||
mutex->owner_ref--;
|
||||
if (mutex->owner_ref > 0)
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
if (atomic_add(&mutex->LockCount, -1) > 1) {
|
||||
if ((stat = release_sem(mutex->Lock)) < B_NO_ERROR) {
|
||||
atomic_add(&mutex->LockCount, 1);
|
||||
return stat;
|
||||
}
|
||||
}
|
||||
|
||||
mutex->owner = -1;
|
||||
mutex->owner_ref = 0;
|
||||
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex)
|
||||
{
|
||||
apr_status_t stat;
|
||||
if ((stat = _thread_mutex_cleanup(mutex)) == APR_SUCCESS) {
|
||||
apr_pool_cleanup_kill(mutex->pool, mutex, _thread_mutex_cleanup);
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
return stat;
|
||||
}
|
||||
|
||||
APR_POOL_IMPLEMENT_ACCESSOR(thread_mutex)
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/*Read/Write locking implementation based on the MultiLock code from
|
||||
* Stephen Beaulieu <hippo@be.com>
|
||||
*/
|
||||
|
||||
#include "apr_arch_thread_rwlock.h"
|
||||
#include "apr_strings.h"
|
||||
#include "apr_portable.h"
|
||||
|
||||
#define BIG_NUM 100000
|
||||
|
||||
static apr_status_t _thread_rw_cleanup(void * data)
|
||||
{
|
||||
apr_thread_rwlock_t *mutex = (apr_thread_rwlock_t*)data;
|
||||
|
||||
if (mutex->ReadCount != 0) {
|
||||
while (atomic_add(&mutex->ReadCount , -1) > 1){
|
||||
release_sem (mutex->Read);
|
||||
}
|
||||
}
|
||||
if (mutex->WriteCount != 0) {
|
||||
while (atomic_add(&mutex->WriteCount , -1) > 1){
|
||||
release_sem (mutex->Write);
|
||||
}
|
||||
}
|
||||
if (mutex->LockCount != 0) {
|
||||
while (atomic_add(&mutex->LockCount , -1) > 1){
|
||||
release_sem (mutex->Lock);
|
||||
}
|
||||
}
|
||||
|
||||
delete_sem(mutex->Read);
|
||||
delete_sem(mutex->Write);
|
||||
delete_sem(mutex->Lock);
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_rwlock_create(apr_thread_rwlock_t **rwlock,
|
||||
apr_pool_t *pool)
|
||||
{
|
||||
apr_thread_rwlock_t *new;
|
||||
|
||||
new = (apr_thread_rwlock_t *)apr_pcalloc(pool, sizeof(apr_thread_rwlock_t));
|
||||
if (new == NULL){
|
||||
return APR_ENOMEM;
|
||||
}
|
||||
|
||||
new->pool = pool;
|
||||
/* we need to make 3 locks... */
|
||||
new->ReadCount = 0;
|
||||
new->WriteCount = 0;
|
||||
new->LockCount = 0;
|
||||
new->Read = create_sem(0, "APR_ReadLock");
|
||||
new->Write = create_sem(0, "APR_WriteLock");
|
||||
new->Lock = create_sem(0, "APR_Lock");
|
||||
|
||||
if (new->Lock < 0 || new->Read < 0 || new->Write < 0) {
|
||||
_thread_rw_cleanup(new);
|
||||
return -1;
|
||||
}
|
||||
|
||||
apr_pool_cleanup_register(new->pool, (void *)new, _thread_rw_cleanup,
|
||||
apr_pool_cleanup_null);
|
||||
(*rwlock) = new;
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_rwlock_rdlock(apr_thread_rwlock_t *rwlock)
|
||||
{
|
||||
int32 rv = APR_SUCCESS;
|
||||
|
||||
if (find_thread(NULL) == rwlock->writer) {
|
||||
/* we're the writer - no problem */
|
||||
rwlock->Nested++;
|
||||
} else {
|
||||
/* we're not the writer */
|
||||
int32 r = atomic_add(&rwlock->ReadCount, 1);
|
||||
if (r < 0) {
|
||||
/* Oh dear, writer holds lock, wait for sem */
|
||||
rv = acquire_sem_etc(rwlock->Read, 1, B_DO_NOT_RESCHEDULE,
|
||||
B_INFINITE_TIMEOUT);
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
|
||||
{
|
||||
return APR_ENOTIMPL;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_rwlock_wrlock(apr_thread_rwlock_t *rwlock)
|
||||
{
|
||||
int rv = APR_SUCCESS;
|
||||
|
||||
if (find_thread(NULL) == rwlock->writer) {
|
||||
rwlock->Nested++;
|
||||
} else {
|
||||
/* we're not the writer... */
|
||||
if (atomic_add(&rwlock->LockCount, 1) >= 1) {
|
||||
/* we're locked - acquire the sem */
|
||||
rv = acquire_sem_etc(rwlock->Lock, 1, B_DO_NOT_RESCHEDULE,
|
||||
B_INFINITE_TIMEOUT);
|
||||
}
|
||||
if (rv == APR_SUCCESS) {
|
||||
/* decrement the ReadCount to a large -ve number so that
|
||||
* we block on new readers...
|
||||
*/
|
||||
int32 readers = atomic_add(&rwlock->ReadCount, -BIG_NUM);
|
||||
if (readers > 0) {
|
||||
/* readers are holding the lock */
|
||||
rv = acquire_sem_etc(rwlock->Write, readers, B_DO_NOT_RESCHEDULE,
|
||||
B_INFINITE_TIMEOUT);
|
||||
}
|
||||
if (rv == APR_SUCCESS)
|
||||
rwlock->writer = find_thread(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_rwlock_trywrlock(apr_thread_rwlock_t *rwlock)
|
||||
{
|
||||
return APR_ENOTIMPL;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_rwlock_unlock(apr_thread_rwlock_t *rwlock)
|
||||
{
|
||||
apr_status_t rv = APR_SUCCESS;
|
||||
int32 readers;
|
||||
|
||||
/* we know we hold the lock, so don't check it :) */
|
||||
if (find_thread(NULL) == rwlock->writer) {
|
||||
/* we know we hold the lock, so don't check it :) */
|
||||
if (rwlock->Nested > 1) {
|
||||
/* we're recursively locked */
|
||||
rwlock->Nested--;
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
/* OK so we need to release the sem if we have it :) */
|
||||
readers = atomic_add(&rwlock->ReadCount, BIG_NUM) + BIG_NUM;
|
||||
if (readers > 0) {
|
||||
rv = release_sem_etc(rwlock->Read, readers, B_DO_NOT_RESCHEDULE);
|
||||
}
|
||||
if (rv == APR_SUCCESS) {
|
||||
rwlock->writer = -1;
|
||||
if (atomic_add(&rwlock->LockCount, -1) > 1) {
|
||||
rv = release_sem_etc(rwlock->Lock, 1, B_DO_NOT_RESCHEDULE);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* We weren't the Writer, so just release the ReadCount... */
|
||||
if (atomic_add(&rwlock->ReadCount, -1) < 0) {
|
||||
/* we have a writer waiting for the lock, so release it */
|
||||
rv = release_sem_etc(rwlock->Write, 1, B_DO_NOT_RESCHEDULE);
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
APR_DECLARE(apr_status_t) apr_thread_rwlock_destroy(apr_thread_rwlock_t *rwlock)
|
||||
{
|
||||
apr_status_t stat;
|
||||
if ((stat = _thread_rw_cleanup(rwlock)) == APR_SUCCESS) {
|
||||
apr_pool_cleanup_kill(rwlock->pool, rwlock, _thread_rw_cleanup);
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
return stat;
|
||||
}
|
||||
|
||||
APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock)
|
||||
|
||||
Reference in New Issue
Block a user