00001 /* Licensed to the Apache Software Foundation (ASF) under one or more 00002 * contributor license agreements. See the NOTICE file distributed with 00003 * this work for additional information regarding copyright ownership. 00004 * The ASF licenses this file to You under the Apache License, Version 2.0 00005 * (the "License"); you may not use this file except in compliance with 00006 * the License. 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 * Note: destroying a condition variable (or likewise, destroying or 00048 * clearing the pool from which a condition variable was allocated) if 00049 * any threads are blocked waiting on it gives undefined results. 00050 */ 00051 00052 /** 00053 * Create and initialize a condition variable that can be used to signal 00054 * and schedule threads in a single process. 00055 * @param cond the memory address where the newly created condition variable 00056 * will be stored. 00057 * @param pool the pool from which to allocate the condition. 00058 */ 00059 APR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond, 00060 apr_pool_t *pool); 00061 00062 /** 00063 * Put the active calling thread to sleep until signaled to wake up. Each 00064 * condition variable must be associated with a mutex, and that mutex must 00065 * be locked before calling this function, or the behavior will be 00066 * undefined. As the calling thread is put to sleep, the given mutex 00067 * will be simultaneously released; and as this thread wakes up the lock 00068 * is again simultaneously acquired. 00069 * @param cond the condition variable on which to block. 00070 * @param mutex the mutex that must be locked upon entering this function, 00071 * is released while the thread is asleep, and is again acquired before 00072 * returning from this function. 00073 * @remark Spurious wakeups may occur. Before and after every call to wait on 00074 * a condition variable, the caller should test whether the condition is already 00075 * met. 00076 */ 00077 APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond, 00078 apr_thread_mutex_t *mutex); 00079 00080 /** 00081 * Put the active calling thread to sleep until signaled to wake up or 00082 * the timeout is reached. Each condition variable must be associated 00083 * with a mutex, and that mutex must be locked before calling this 00084 * function, or the behavior will be undefined. As the calling thread 00085 * is put to sleep, the given mutex will be simultaneously released; 00086 * and as this thread wakes up the lock is again simultaneously acquired. 00087 * @param cond the condition variable on which to block. 00088 * @param mutex the mutex that must be locked upon entering this function, 00089 * is released while the thread is asleep, and is again acquired before 00090 * returning from this function. 00091 * @param timeout The amount of time in microseconds to wait. This is 00092 * a maximum, not a minimum. If the condition is signaled, we 00093 * will wake up before this time, otherwise the error APR_TIMEUP 00094 * is returned. 00095 */ 00096 APR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond, 00097 apr_thread_mutex_t *mutex, 00098 apr_interval_time_t timeout); 00099 00100 /** 00101 * Signals a single thread, if one exists, that is blocking on the given 00102 * condition variable. That thread is then scheduled to wake up and acquire 00103 * the associated mutex. Although it is not required, if predictable scheduling 00104 * is desired, that mutex must be locked while calling this function. 00105 * @param cond the condition variable on which to produce the signal. 00106 * @remark If no threads are waiting on the condition variable, nothing happens. 00107 */ 00108 APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond); 00109 00110 /** 00111 * Signals all threads blocking on the given condition variable. 00112 * Each thread that was signaled is then scheduled to wake up and acquire 00113 * the associated mutex. This will happen in a serialized manner. 00114 * @param cond the condition variable on which to produce the broadcast. 00115 * @remark If no threads are waiting on the condition variable, nothing happens. 00116 */ 00117 APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond); 00118 00119 /** 00120 * Destroy the condition variable and free the associated memory. 00121 * @param cond the condition variable to destroy. 00122 */ 00123 APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond); 00124 00125 /** 00126 * Get the pool used by this thread_cond. 00127 * @return apr_pool_t the pool 00128 */ 00129 APR_POOL_DECLARE_ACCESSOR(thread_cond); 00130 00131 #endif /* APR_HAS_THREADS */ 00132 00133 /** @} */ 00134 00135 #ifdef __cplusplus 00136 } 00137 #endif 00138 00139 #endif /* ! APR_THREAD_COND_H */