1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* 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
/* The rwlock must be initialized but not locked by any thread when
25
* cleanup is called. */
26
static apr_status_t thread_rwlock_cleanup(void *data)
28
apr_thread_rwlock_t *rwlock = (apr_thread_rwlock_t *)data;
31
stat = pthread_rwlock_destroy(&rwlock->rwlock);
32
#ifdef PTHREAD_SETS_ERRNO
40
APR_DECLARE(apr_status_t) apr_thread_rwlock_create(apr_thread_rwlock_t **rwlock,
43
apr_thread_rwlock_t *new_rwlock;
46
new_rwlock = apr_palloc(pool, sizeof(apr_thread_rwlock_t));
47
new_rwlock->pool = pool;
49
if ((stat = pthread_rwlock_init(&new_rwlock->rwlock, NULL))) {
50
#ifdef PTHREAD_SETS_ERRNO
56
apr_pool_cleanup_register(new_rwlock->pool,
57
(void *)new_rwlock, thread_rwlock_cleanup,
58
apr_pool_cleanup_null);
64
APR_DECLARE(apr_status_t) apr_thread_rwlock_rdlock(apr_thread_rwlock_t *rwlock)
68
stat = pthread_rwlock_rdlock(&rwlock->rwlock);
69
#ifdef PTHREAD_SETS_ERRNO
77
APR_DECLARE(apr_status_t) apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
81
stat = pthread_rwlock_tryrdlock(&rwlock->rwlock);
82
#ifdef PTHREAD_SETS_ERRNO
87
/* Normalize the return code. */
93
APR_DECLARE(apr_status_t) apr_thread_rwlock_wrlock(apr_thread_rwlock_t *rwlock)
97
stat = pthread_rwlock_wrlock(&rwlock->rwlock);
98
#ifdef PTHREAD_SETS_ERRNO
106
APR_DECLARE(apr_status_t) apr_thread_rwlock_trywrlock(apr_thread_rwlock_t *rwlock)
110
stat = pthread_rwlock_trywrlock(&rwlock->rwlock);
111
#ifdef PTHREAD_SETS_ERRNO
116
/* Normalize the return code. */
122
APR_DECLARE(apr_status_t) apr_thread_rwlock_unlock(apr_thread_rwlock_t *rwlock)
126
stat = pthread_rwlock_unlock(&rwlock->rwlock);
127
#ifdef PTHREAD_SETS_ERRNO
135
APR_DECLARE(apr_status_t) apr_thread_rwlock_destroy(apr_thread_rwlock_t *rwlock)
137
return apr_pool_cleanup_run(rwlock->pool, rwlock, thread_rwlock_cleanup);
140
#else /* HAVE_PTHREAD_RWLOCKS */
142
APR_DECLARE(apr_status_t) apr_thread_rwlock_create(apr_thread_rwlock_t **rwlock,
148
APR_DECLARE(apr_status_t) apr_thread_rwlock_rdlock(apr_thread_rwlock_t *rwlock)
153
APR_DECLARE(apr_status_t) apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
158
APR_DECLARE(apr_status_t) apr_thread_rwlock_wrlock(apr_thread_rwlock_t *rwlock)
163
APR_DECLARE(apr_status_t) apr_thread_rwlock_trywrlock(apr_thread_rwlock_t *rwlock)
168
APR_DECLARE(apr_status_t) apr_thread_rwlock_unlock(apr_thread_rwlock_t *rwlock)
173
APR_DECLARE(apr_status_t) apr_thread_rwlock_destroy(apr_thread_rwlock_t *rwlock)
178
#endif /* HAVE_PTHREAD_RWLOCKS */
179
APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock)
181
#endif /* APR_HAS_THREADS */