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 #ifndef APR_THREAD_COND_H
18 #define APR_THREAD_COND_H
21 * @file apr_thread_cond.h
22 * @brief APR Condition Variable Routines
26 #include "apr_pools.h"
27 #include "apr_errno.h"
29 #include "apr_thread_mutex.h"
33 #endif /* __cplusplus */
35 #if APR_HAS_THREADS || defined(DOXYGEN)
38 * @defgroup apr_thread_cond Condition Variable Routines
43 /** Opaque structure for thread condition variables */
44 typedef struct apr_thread_cond_t apr_thread_cond_t;
47 * Create and initialize a condition variable that can be used to signal
48 * and schedule threads in a single process.
49 * @param cond the memory address where the newly created condition variable
51 * @param pool the pool from which to allocate the mutex.
53 APR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond,
57 * Put the active calling thread to sleep until signaled to wake up. Each
58 * condition variable must be associated with a mutex, and that mutex must
59 * be locked before calling this function, or the behavior will be
60 * undefined. As the calling thread is put to sleep, the given mutex
61 * will be simultaneously released; and as this thread wakes up the lock
62 * is again simultaneously acquired.
63 * @param cond the condition variable on which to block.
64 * @param mutex the mutex that must be locked upon entering this function,
65 * is released while the thread is asleep, and is again acquired before
66 * returning from this function.
68 APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond,
69 apr_thread_mutex_t *mutex);
72 * Put the active calling thread to sleep until signaled to wake up or
73 * the timeout is reached. Each condition variable must be associated
74 * with a mutex, and that mutex must be locked before calling this
75 * function, or the behavior will be undefined. As the calling thread
76 * is put to sleep, the given mutex will be simultaneously released;
77 * and as this thread wakes up the lock is again simultaneously acquired.
78 * @param cond the condition variable on which to block.
79 * @param mutex the mutex that must be locked upon entering this function,
80 * is released while the thread is asleep, and is again acquired before
81 * returning from this function.
82 * @param timeout The amount of time in microseconds to wait. This is
83 * a maximum, not a minimum. If the condition is signaled, we
84 * will wake up before this time, otherwise the error APR_TIMEUP
87 APR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond,
88 apr_thread_mutex_t *mutex,
89 apr_interval_time_t timeout);
92 * Signals a singla thread, if one exists, that is blocking on the given
93 * condition variable. That thread is then scheduled to wake up and acquire
94 * the associated mutex. Although it is not required, if predictible schedule
95 * is desired, that mutex must be locked while calling this function.
96 * @param cond the condition variable on which to produce the signal.
98 APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond);
101 * Signals all threads blocking on the given condition variable.
102 * Each thread that was signaled is then schedule to wake up and acquire
103 * the associated mutex. This will happen in a serialized manner.
104 * @param cond the condition variable on which to produce the broadcast.
106 APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond);
109 * Destroy the condition variable and free the associated memory.
110 * @param cond the condition variable to destroy.
112 APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond);
115 * Get the pool used by this thread_cond.
116 * @return apr_pool_t the pool
118 APR_POOL_DECLARE_ACCESSOR(thread_cond);
120 #endif /* APR_HAS_THREADS */
128 #endif /* ! APR_THREAD_COND_H */