00001 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as 00002 * applicable. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef APR_THREAD_COND_H 00018 #define APR_THREAD_COND_H 00019 00020 /** 00021 * @file apr_thread_cond.h 00022 * @brief APR Condition Variable Routines 00023 */ 00024 00025 #include "apr.h" 00026 #include "apr_pools.h" 00027 #include "apr_errno.h" 00028 #include "apr_time.h" 00029 #include "apr_thread_mutex.h" 00030 00031 #ifdef __cplusplus 00032 extern "C" { 00033 #endif /* __cplusplus */ 00034 00035 #if APR_HAS_THREADS || defined(DOXYGEN) 00036 00037 /** 00038 * @defgroup apr_thread_cond Condition Variable Routines 00039 * @ingroup APR 00040 * @{ 00041 */ 00042 00043 /** Opaque structure for thread condition variables */ 00044 typedef struct apr_thread_cond_t apr_thread_cond_t; 00045 00046 /** 00047 * Create and initialize a condition variable that can be used to signal 00048 * and schedule threads in a single process. 00049 * @param cond the memory address where the newly created condition variable 00050 * will be stored. 00051 * @param pool the pool from which to allocate the mutex. 00052 */ 00053 APR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond, 00054 apr_pool_t *pool); 00055 00056 /** 00057 * Put the active calling thread to sleep until signaled to wake up. Each 00058 * condition variable must be associated with a mutex, and that mutex must 00059 * be locked before calling this function, or the behavior will be 00060 * undefined. As the calling thread is put to sleep, the given mutex 00061 * will be simultaneously released; and as this thread wakes up the lock 00062 * is again simultaneously acquired. 00063 * @param cond the condition variable on which to block. 00064 * @param mutex the mutex that must be locked upon entering this function, 00065 * is released while the thread is asleep, and is again acquired before 00066 * returning from this function. 00067 */ 00068 APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond, 00069 apr_thread_mutex_t *mutex); 00070 00071 /** 00072 * Put the active calling thread to sleep until signaled to wake up or 00073 * the timeout is reached. Each condition variable must be associated 00074 * with a mutex, and that mutex must be locked before calling this 00075 * function, or the behavior will be undefined. As the calling thread 00076 * is put to sleep, the given mutex will be simultaneously released; 00077 * and as this thread wakes up the lock is again simultaneously acquired. 00078 * @param cond the condition variable on which to block. 00079 * @param mutex the mutex that must be locked upon entering this function, 00080 * is released while the thread is asleep, and is again acquired before 00081 * returning from this function. 00082 * @param timeout The amount of time in microseconds to wait. This is 00083 * a maximum, not a minimum. If the condition is signaled, we 00084 * will wake up before this time, otherwise the error APR_TIMEUP 00085 * is returned. 00086 */ 00087 APR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond, 00088 apr_thread_mutex_t *mutex, 00089 apr_interval_time_t timeout); 00090 00091 /** 00092 * Signals a singla thread, if one exists, that is blocking on the given 00093 * condition variable. That thread is then scheduled to wake up and acquire 00094 * the associated mutex. Although it is not required, if predictible schedule 00095 * is desired, that mutex must be locked while calling this function. 00096 * @param cond the condition variable on which to produce the signal. 00097 */ 00098 APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond); 00099 00100 /** 00101 * Signals all threads blocking on the given condition variable. 00102 * Each thread that was signaled is then schedule to wake up and acquire 00103 * the associated mutex. This will happen in a serialized manner. 00104 * @param cond the condition variable on which to produce the broadcast. 00105 */ 00106 APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond); 00107 00108 /** 00109 * Destroy the condition variable and free the associated memory. 00110 * @param cond the condition variable to destroy. 00111 */ 00112 APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond); 00113 00114 /** 00115 * Get the pool used by this thread_cond. 00116 * @return apr_pool_t the pool 00117 */ 00118 APR_POOL_DECLARE_ACCESSOR(thread_cond); 00119 00120 #endif /* APR_HAS_THREADS */ 00121 00122 /** @} */ 00123 00124 #ifdef __cplusplus 00125 } 00126 #endif 00127 00128 #endif /* ! APR_THREAD_COND_H */