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_arch_thread_rwlock.h"
18 #include "apr_private.h"
22 #ifdef HAVE_PTHREAD_RWLOCKS
24 static apr_status_t thread_rwlock_cleanup(void *data)
26 apr_thread_rwlock_t *rwlock = (apr_thread_rwlock_t *)data;
29 pthread_rwlock_unlock(rwlock->rwlock);
30 stat = pthread_rwlock_destroy(rwlock->rwlock);
31 #ifdef PTHREAD_SETS_ERRNO
39 APR_DECLARE(apr_status_t) apr_thread_rwlock_create(apr_thread_rwlock_t **rwlock,
42 apr_thread_rwlock_t *new_rwlock;
45 new_rwlock = (apr_thread_rwlock_t *)apr_pcalloc(pool,
46 sizeof(apr_thread_rwlock_t));
48 if (new_rwlock == NULL) {
52 new_rwlock->pool = pool;
53 new_rwlock->rwlock = (pthread_rwlock_t *)apr_palloc(pool,
54 sizeof(pthread_rwlock_t));
56 if (new_rwlock->rwlock == NULL) {
60 if ((stat = pthread_rwlock_init(new_rwlock->rwlock, NULL))) {
61 #ifdef PTHREAD_SETS_ERRNO
64 thread_rwlock_cleanup(new_rwlock);
68 apr_pool_cleanup_register(new_rwlock->pool,
69 (void *)new_rwlock, thread_rwlock_cleanup,
70 apr_pool_cleanup_null);
76 APR_DECLARE(apr_status_t) apr_thread_rwlock_rdlock(apr_thread_rwlock_t *rwlock)
80 stat = pthread_rwlock_rdlock(rwlock->rwlock);
81 #ifdef PTHREAD_SETS_ERRNO
89 APR_DECLARE(apr_status_t) apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
93 stat = pthread_rwlock_tryrdlock(rwlock->rwlock);
94 #ifdef PTHREAD_SETS_ERRNO
99 /* Normalize the return code. */
105 APR_DECLARE(apr_status_t) apr_thread_rwlock_wrlock(apr_thread_rwlock_t *rwlock)
109 stat = pthread_rwlock_wrlock(rwlock->rwlock);
110 #ifdef PTHREAD_SETS_ERRNO
118 APR_DECLARE(apr_status_t) apr_thread_rwlock_trywrlock(apr_thread_rwlock_t *rwlock)
122 stat = pthread_rwlock_trywrlock(rwlock->rwlock);
123 #ifdef PTHREAD_SETS_ERRNO
128 /* Normalize the return code. */
134 APR_DECLARE(apr_status_t) apr_thread_rwlock_unlock(apr_thread_rwlock_t *rwlock)
138 stat = pthread_rwlock_unlock(rwlock->rwlock);
139 #ifdef PTHREAD_SETS_ERRNO
147 APR_DECLARE(apr_status_t) apr_thread_rwlock_destroy(apr_thread_rwlock_t *rwlock)
150 if ((stat = thread_rwlock_cleanup(rwlock)) == APR_SUCCESS) {
151 apr_pool_cleanup_kill(rwlock->pool, rwlock, thread_rwlock_cleanup);
157 #else /* HAVE_PTHREAD_RWLOCKS */
159 APR_DECLARE(apr_status_t) apr_thread_rwlock_create(apr_thread_rwlock_t **rwlock,
165 APR_DECLARE(apr_status_t) apr_thread_rwlock_rdlock(apr_thread_rwlock_t *rwlock)
170 APR_DECLARE(apr_status_t) apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
175 APR_DECLARE(apr_status_t) apr_thread_rwlock_wrlock(apr_thread_rwlock_t *rwlock)
180 APR_DECLARE(apr_status_t) apr_thread_rwlock_trywrlock(apr_thread_rwlock_t *rwlock)
185 APR_DECLARE(apr_status_t) apr_thread_rwlock_unlock(apr_thread_rwlock_t *rwlock)
190 APR_DECLARE(apr_status_t) apr_thread_rwlock_destroy(apr_thread_rwlock_t *rwlock)
195 #endif /* HAVE_PTHREAD_RWLOCKS */
196 APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock)
198 #endif /* APR_HAS_THREADS */