apr_thread_cond.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. #ifndef APR_THREAD_COND_H
  17. #define APR_THREAD_COND_H
  18. /**
  19. * @file apr_thread_cond.h
  20. * @brief APR Condition Variable Routines
  21. */
  22. #include "apr.h"
  23. #include "apr_pools.h"
  24. #include "apr_errno.h"
  25. #include "apr_time.h"
  26. #include "apr_thread_mutex.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif /* __cplusplus */
  30. #if APR_HAS_THREADS || defined(DOXYGEN)
  31. /**
  32. * @defgroup apr_thread_cond Condition Variable Routines
  33. * @ingroup APR
  34. * @{
  35. */
  36. /** Opaque structure for thread condition variables */
  37. typedef struct apr_thread_cond_t apr_thread_cond_t;
  38. /**
  39. * Note: destroying a condition variable (or likewise, destroying or
  40. * clearing the pool from which a condition variable was allocated) if
  41. * any threads are blocked waiting on it gives undefined results.
  42. */
  43. /**
  44. * Create and initialize a condition variable that can be used to signal
  45. * and schedule threads in a single process.
  46. * @param cond the memory address where the newly created condition variable
  47. * will be stored.
  48. * @param pool the pool from which to allocate the condition.
  49. */
  50. APR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond,
  51. apr_pool_t *pool);
  52. /**
  53. * Put the active calling thread to sleep until signaled to wake up. Each
  54. * condition variable must be associated with a mutex, and that mutex must
  55. * be locked before calling this function, or the behavior will be
  56. * undefined. As the calling thread is put to sleep, the given mutex
  57. * will be simultaneously released; and as this thread wakes up the lock
  58. * is again simultaneously acquired.
  59. * @param cond the condition variable on which to block.
  60. * @param mutex the mutex that must be locked upon entering this function,
  61. * is released while the thread is asleep, and is again acquired before
  62. * returning from this function.
  63. * @remark Spurious wakeups may occur. Before and after every call to wait on
  64. * a condition variable, the caller should test whether the condition is already
  65. * met.
  66. */
  67. APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond,
  68. apr_thread_mutex_t *mutex);
  69. /**
  70. * Put the active calling thread to sleep until signaled to wake up or
  71. * the timeout is reached. Each condition variable must be associated
  72. * with a mutex, and that mutex must be locked before calling this
  73. * function, or the behavior will be undefined. As the calling thread
  74. * is put to sleep, the given mutex will be simultaneously released;
  75. * and as this thread wakes up the lock is again simultaneously acquired.
  76. * @param cond the condition variable on which to block.
  77. * @param mutex the mutex that must be locked upon entering this function,
  78. * is released while the thread is asleep, and is again acquired before
  79. * returning from this function.
  80. * @param timeout The amount of time in microseconds to wait. This is
  81. * a maximum, not a minimum. If the condition is signaled, we
  82. * will wake up before this time, otherwise the error APR_TIMEUP
  83. * is returned.
  84. */
  85. APR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond,
  86. apr_thread_mutex_t *mutex,
  87. apr_interval_time_t timeout);
  88. /**
  89. * Signals a single thread, if one exists, that is blocking on the given
  90. * condition variable. That thread is then scheduled to wake up and acquire
  91. * the associated mutex. Although it is not required, if predictable scheduling
  92. * is desired, that mutex must be locked while calling this function.
  93. * @param cond the condition variable on which to produce the signal.
  94. * @remark If no threads are waiting on the condition variable, nothing happens.
  95. */
  96. APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond);
  97. /**
  98. * Signals all threads blocking on the given condition variable.
  99. * Each thread that was signaled is then scheduled to wake up and acquire
  100. * the associated mutex. This will happen in a serialized manner.
  101. * @param cond the condition variable on which to produce the broadcast.
  102. * @remark If no threads are waiting on the condition variable, nothing happens.
  103. */
  104. APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond);
  105. /**
  106. * Destroy the condition variable and free the associated memory.
  107. * @param cond the condition variable to destroy.
  108. */
  109. APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond);
  110. /**
  111. * Get the pool used by this thread_cond.
  112. * @return apr_pool_t the pool
  113. */
  114. APR_POOL_DECLARE_ACCESSOR(thread_cond);
  115. #endif /* APR_HAS_THREADS */
  116. /** @} */
  117. #ifdef __cplusplus
  118. }
  119. #endif
  120. #endif /* ! APR_THREAD_COND_H */