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 apr_pool_cleanup_register(new_mutex->pool,
81 new_mutex, thread_mutex_cleanup,
82 apr_pool_cleanup_null);
88 APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex)
92 rv = pthread_mutex_lock(&mutex->mutex);
93 #ifdef HAVE_ZOS_PTHREADS
102 APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex)
106 rv = pthread_mutex_trylock(&mutex->mutex);
108 #ifdef HAVE_ZOS_PTHREADS
111 return (rv == EBUSY) ? APR_EBUSY : rv;
117 APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex)
121 status = pthread_mutex_unlock(&mutex->mutex);
122 #ifdef HAVE_ZOS_PTHREADS
131 APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex)
133 return apr_pool_cleanup_run(mutex->pool, mutex, thread_mutex_cleanup);
136 APR_POOL_IMPLEMENT_ACCESSOR(thread_mutex)
138 #endif /* APR_HAS_THREADS */