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 /*Read/Write locking implementation based on the MultiLock code from
18 * Stephen Beaulieu <hippo@be.com>
21 #include "beos/apr_arch_thread_mutex.h"
22 #include "apr_strings.h"
23 #include "apr_portable.h"
25 static apr_status_t _thread_mutex_cleanup(void * data)
27 apr_thread_mutex_t *lock = (apr_thread_mutex_t*)data;
28 if (lock->LockCount != 0) {
29 /* we're still locked... */
30 while (atomic_add(&lock->LockCount , -1) > 1){
31 /* OK we had more than one person waiting on the lock so
32 * the sem is also locked. Release it until we have no more
35 release_sem (lock->Lock);
38 delete_sem(lock->Lock);
42 APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
46 apr_thread_mutex_t *new_m;
47 apr_status_t stat = APR_SUCCESS;
49 new_m = (apr_thread_mutex_t *)apr_pcalloc(pool, sizeof(apr_thread_mutex_t));
54 if ((stat = create_sem(0, "APR_Lock")) < B_NO_ERROR) {
55 _thread_mutex_cleanup(new_m);
62 /* Optimal default is APR_THREAD_MUTEX_UNNESTED,
63 * no additional checks required for either flag.
65 new_m->nested = flags & APR_THREAD_MUTEX_NESTED;
67 apr_pool_cleanup_register(new_m->pool, (void *)new_m, _thread_mutex_cleanup,
68 apr_pool_cleanup_null);
74 #if APR_HAS_CREATE_LOCKS_NP
75 APR_DECLARE(apr_status_t) apr_thread_mutex_create_np(apr_thread_mutex_t **mutex,
77 apr_lockmech_e_np mech,
84 APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex)
87 thread_id me = find_thread(NULL);
89 if (mutex->nested && mutex->owner == me) {
94 if (atomic_add(&mutex->LockCount, 1) > 0) {
95 if ((stat = acquire_sem(mutex->Lock)) < B_NO_ERROR) {
96 /* Oh dear, acquire_sem failed!! */
97 atomic_add(&mutex->LockCount, -1);
103 mutex->owner_ref = 1;
108 APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex)
113 APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex)
117 if (mutex->nested && mutex->owner == find_thread(NULL)) {
119 if (mutex->owner_ref > 0)
123 if (atomic_add(&mutex->LockCount, -1) > 1) {
124 if ((stat = release_sem(mutex->Lock)) < B_NO_ERROR) {
125 atomic_add(&mutex->LockCount, 1);
131 mutex->owner_ref = 0;
136 APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex)
139 if ((stat = _thread_mutex_cleanup(mutex)) == APR_SUCCESS) {
140 apr_pool_cleanup_kill(mutex->pool, mutex, _thread_mutex_cleanup);
146 APR_POOL_IMPLEMENT_ACCESSOR(thread_mutex)