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_mutex.h"
18 #define APR_WANT_MEMFUNC
23 static apr_status_t thread_mutex_cleanup(void *data)
25 apr_thread_mutex_t *mutex = data;
28 rv = pthread_mutex_destroy(&mutex->mutex);
29 #ifdef HAVE_ZOS_PTHREADS
37 APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
41 apr_thread_mutex_t *new_mutex;
44 #ifndef HAVE_PTHREAD_MUTEX_RECURSIVE
45 if (flags & APR_THREAD_MUTEX_NESTED) {
50 new_mutex = apr_pcalloc(pool, sizeof(apr_thread_mutex_t));
51 new_mutex->pool = pool;
53 #ifdef HAVE_PTHREAD_MUTEX_RECURSIVE
54 if (flags & APR_THREAD_MUTEX_NESTED) {
55 pthread_mutexattr_t mattr;
57 rv = pthread_mutexattr_init(&mattr);
60 rv = pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
62 pthread_mutexattr_destroy(&mattr);
66 rv = pthread_mutex_init(&new_mutex->mutex, &mattr);
68 pthread_mutexattr_destroy(&mattr);
71 rv = pthread_mutex_init(&new_mutex->mutex, NULL);
74 #ifdef HAVE_ZOS_PTHREADS
80 #ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
81 if (flags & APR_THREAD_MUTEX_TIMED) {
82 rv = apr_thread_cond_create(&new_mutex->cond, pool);
84 #ifdef HAVE_ZOS_PTHREADS
87 pthread_mutex_destroy(&new_mutex->mutex);
93 apr_pool_cleanup_register(new_mutex->pool,
94 new_mutex, thread_mutex_cleanup,
95 apr_pool_cleanup_null);
101 APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex)
108 rv = pthread_mutex_lock(&mutex->mutex);
110 #ifdef HAVE_ZOS_PTHREADS
117 mutex->num_waiters++;
118 rv = apr_thread_cond_wait(mutex->cond, mutex);
119 mutex->num_waiters--;
125 rv2 = pthread_mutex_unlock(&mutex->mutex);
127 #ifdef HAVE_ZOS_PTHREADS
137 rv = pthread_mutex_lock(&mutex->mutex);
138 #ifdef HAVE_ZOS_PTHREADS
147 APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex)
154 rv = pthread_mutex_lock(&mutex->mutex);
156 #ifdef HAVE_ZOS_PTHREADS
169 rv2 = pthread_mutex_unlock(&mutex->mutex);
171 #ifdef HAVE_ZOS_PTHREADS
181 rv = pthread_mutex_trylock(&mutex->mutex);
183 #ifdef HAVE_ZOS_PTHREADS
186 return (rv == EBUSY) ? APR_EBUSY : rv;
192 APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex,
193 apr_interval_time_t timeout)
195 apr_status_t rv = APR_ENOTIMPL;
196 #if APR_HAS_TIMEDLOCKS
198 #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
200 rv = pthread_mutex_trylock(&mutex->mutex);
202 #ifdef HAVE_ZOS_PTHREADS
211 struct timespec abstime;
213 timeout += apr_time_now();
214 abstime.tv_sec = apr_time_sec(timeout);
215 abstime.tv_nsec = apr_time_usec(timeout) * 1000; /* nanoseconds */
217 rv = pthread_mutex_timedlock(&mutex->mutex, &abstime);
219 #ifdef HAVE_ZOS_PTHREADS
222 if (rv == ETIMEDOUT) {
228 #else /* HAVE_PTHREAD_MUTEX_TIMEDLOCK */
231 rv = pthread_mutex_lock(&mutex->mutex);
233 #ifdef HAVE_ZOS_PTHREADS
244 mutex->num_waiters++;
246 rv = apr_thread_cond_timedwait(mutex->cond, mutex,
249 #ifdef HAVE_ZOS_PTHREADS
254 } while (mutex->locked);
255 mutex->num_waiters--;
258 pthread_mutex_unlock(&mutex->mutex);
265 rv = pthread_mutex_unlock(&mutex->mutex);
267 #ifdef HAVE_ZOS_PTHREADS
274 #endif /* HAVE_PTHREAD_MUTEX_TIMEDLOCK */
276 #endif /* APR_HAS_TIMEDLOCKS */
280 APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex)
285 status = pthread_mutex_lock(&mutex->mutex);
287 #ifdef HAVE_ZOS_PTHREADS
293 if (!mutex->locked) {
296 else if (mutex->num_waiters) {
297 status = apr_thread_cond_signal(mutex->cond);
300 pthread_mutex_unlock(&mutex->mutex);
307 status = pthread_mutex_unlock(&mutex->mutex);
308 #ifdef HAVE_ZOS_PTHREADS
317 APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex)
319 apr_status_t rv, rv2 = APR_SUCCESS;
322 rv2 = apr_thread_cond_destroy(mutex->cond);
324 rv = apr_pool_cleanup_run(mutex->pool, mutex, thread_mutex_cleanup);
325 if (rv == APR_SUCCESS) {
332 APR_POOL_IMPLEMENT_ACCESSOR(thread_mutex)
334 #endif /* APR_HAS_THREADS */