thread_cond.c revision 251875
151973Smsmith/* Licensed to the Apache Software Foundation (ASF) under one or more
251973Smsmith * contributor license agreements.  See the NOTICE file distributed with
351973Smsmith * this work for additional information regarding copyright ownership.
451973Smsmith * The ASF licenses this file to You under the Apache License, Version 2.0
551973Smsmith * (the "License"); you may not use this file except in compliance with
651973Smsmith * the License.  You may obtain a copy of the License at
751973Smsmith *
851973Smsmith *     http://www.apache.org/licenses/LICENSE-2.0
951973Smsmith *
1051973Smsmith * Unless required by applicable law or agreed to in writing, software
1151973Smsmith * distributed under the License is distributed on an "AS IS" BASIS,
1251973Smsmith * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1351973Smsmith * See the License for the specific language governing permissions and
1451973Smsmith * limitations under the License.
1551973Smsmith */
1651973Smsmith
1751973Smsmith#include "apr.h"
1851973Smsmith
1951973Smsmith#if APR_HAS_THREADS
2051973Smsmith
2151973Smsmith#include "apr_arch_thread_mutex.h"
2251973Smsmith#include "apr_arch_thread_cond.h"
2351973Smsmith
2451973Smsmithstatic apr_status_t thread_cond_cleanup(void *data)
2551973Smsmith{
2651973Smsmith    apr_thread_cond_t *cond = (apr_thread_cond_t *)data;
2751973Smsmith    apr_status_t rv;
2851973Smsmith
2951973Smsmith    rv = pthread_cond_destroy(&cond->cond);
3051973Smsmith#ifdef HAVE_ZOS_PTHREADS
3151973Smsmith    if (rv) {
3251973Smsmith        rv = errno;
3351973Smsmith    }
3451973Smsmith#endif
3559136Smsmith    return rv;
3651973Smsmith}
3751973Smsmith
3851973SmsmithAPR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond,
3951973Smsmith                                                 apr_pool_t *pool)
4059136Smsmith{
4159136Smsmith    apr_thread_cond_t *new_cond;
4259136Smsmith    apr_status_t rv;
4351973Smsmith
4451973Smsmith    new_cond = apr_palloc(pool, sizeof(apr_thread_cond_t));
4551973Smsmith
4651973Smsmith    new_cond->pool = pool;
4751973Smsmith
4851973Smsmith    if ((rv = pthread_cond_init(&new_cond->cond, NULL))) {
4951973Smsmith#ifdef HAVE_ZOS_PTHREADS
5051973Smsmith        rv = errno;
5151973Smsmith#endif
5251973Smsmith        return rv;
5351973Smsmith    }
5451973Smsmith
5551973Smsmith    apr_pool_cleanup_register(new_cond->pool,
5651973Smsmith                              (void *)new_cond, thread_cond_cleanup,
5751973Smsmith                              apr_pool_cleanup_null);
5851973Smsmith
5951973Smsmith    *cond = new_cond;
6051973Smsmith    return APR_SUCCESS;
6151973Smsmith}
6251973Smsmith
6351973SmsmithAPR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond,
6451973Smsmith                                               apr_thread_mutex_t *mutex)
6558188Smsmith{
6658188Smsmith    apr_status_t rv;
6758188Smsmith
6858188Smsmith    rv = pthread_cond_wait(&cond->cond, &mutex->mutex);
6951973Smsmith#ifdef HAVE_ZOS_PTHREADS
7051973Smsmith    if (rv) {
7159136Smsmith        rv = errno;
7259136Smsmith    }
7359136Smsmith#endif
7459136Smsmith    return rv;
7559136Smsmith}
7659136Smsmith
7759136SmsmithAPR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond,
7859136Smsmith                                                    apr_thread_mutex_t *mutex,
7959136Smsmith                                                    apr_interval_time_t timeout)
8059136Smsmith{
8159136Smsmith    apr_status_t rv;
8259136Smsmith    apr_time_t then;
8359136Smsmith    struct timespec abstime;
8459136Smsmith
8559136Smsmith    then = apr_time_now() + timeout;
8659136Smsmith    abstime.tv_sec = apr_time_sec(then);
8759136Smsmith    abstime.tv_nsec = apr_time_usec(then) * 1000; /* nanoseconds */
8859136Smsmith
8959136Smsmith    rv = pthread_cond_timedwait(&cond->cond, &mutex->mutex, &abstime);
9051973Smsmith#ifdef HAVE_ZOS_PTHREADS
9151973Smsmith    if (rv) {
9251973Smsmith        rv = errno;
9351973Smsmith    }
9451973Smsmith#endif
9559136Smsmith    if (ETIMEDOUT == rv) {
9659136Smsmith        return APR_TIMEUP;
9759136Smsmith    }
98    return rv;
99}
100
101
102APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond)
103{
104    apr_status_t rv;
105
106    rv = pthread_cond_signal(&cond->cond);
107#ifdef HAVE_ZOS_PTHREADS
108    if (rv) {
109        rv = errno;
110    }
111#endif
112    return rv;
113}
114
115APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond)
116{
117    apr_status_t rv;
118
119    rv = pthread_cond_broadcast(&cond->cond);
120#ifdef HAVE_ZOS_PTHREADS
121    if (rv) {
122        rv = errno;
123    }
124#endif
125    return rv;
126}
127
128APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond)
129{
130    return apr_pool_cleanup_run(cond->pool, cond, thread_cond_cleanup);
131}
132
133APR_POOL_IMPLEMENT_ACCESSOR(thread_cond)
134
135#endif /* APR_HAS_THREADS */
136