fspr_thread_cond.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 fspr_thread_cond.h
  20. * @brief APR Condition Variable Routines
  21. */
  22. #include "fspr.h"
  23. #include "fspr_pools.h"
  24. #include "fspr_errno.h"
  25. #include "fspr_time.h"
  26. #include "fspr_thread_mutex.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif /* __cplusplus */
  30. #if APR_HAS_THREADS || defined(DOXYGEN)
  31. /**
  32. * @defgroup fspr_thread_cond Condition Variable Routines
  33. * @ingroup APR
  34. * @{
  35. */
  36. /** Opaque structure for thread condition variables */
  37. typedef struct fspr_thread_cond_t fspr_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 mutex.
  49. */
  50. APR_DECLARE(fspr_status_t) fspr_thread_cond_create(fspr_thread_cond_t **cond,
  51. fspr_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. */
  64. APR_DECLARE(fspr_status_t) fspr_thread_cond_wait(fspr_thread_cond_t *cond,
  65. fspr_thread_mutex_t *mutex);
  66. /**
  67. * Put the active calling thread to sleep until signaled to wake up or
  68. * the timeout is reached. Each condition variable must be associated
  69. * with a mutex, and that mutex must be locked before calling this
  70. * function, or the behavior will be undefined. As the calling thread
  71. * is put to sleep, the given mutex will be simultaneously released;
  72. * and as this thread wakes up the lock is again simultaneously acquired.
  73. * @param cond the condition variable on which to block.
  74. * @param mutex the mutex that must be locked upon entering this function,
  75. * is released while the thread is asleep, and is again acquired before
  76. * returning from this function.
  77. * @param timeout The amount of time in microseconds to wait. This is
  78. * a maximum, not a minimum. If the condition is signaled, we
  79. * will wake up before this time, otherwise the error APR_TIMEUP
  80. * is returned.
  81. */
  82. APR_DECLARE(fspr_status_t) fspr_thread_cond_timedwait(fspr_thread_cond_t *cond,
  83. fspr_thread_mutex_t *mutex,
  84. fspr_interval_time_t timeout);
  85. /**
  86. * Signals a single thread, if one exists, that is blocking on the given
  87. * condition variable. That thread is then scheduled to wake up and acquire
  88. * the associated mutex. Although it is not required, if predictable scheduling
  89. * is desired, that mutex must be locked while calling this function.
  90. * @param cond the condition variable on which to produce the signal.
  91. */
  92. APR_DECLARE(fspr_status_t) fspr_thread_cond_signal(fspr_thread_cond_t *cond);
  93. /**
  94. * Signals all threads blocking on the given condition variable.
  95. * Each thread that was signaled is then scheduled to wake up and acquire
  96. * the associated mutex. This will happen in a serialized manner.
  97. * @param cond the condition variable on which to produce the broadcast.
  98. */
  99. APR_DECLARE(fspr_status_t) fspr_thread_cond_broadcast(fspr_thread_cond_t *cond);
  100. /**
  101. * Destroy the condition variable and free the associated memory.
  102. * @param cond the condition variable to destroy.
  103. */
  104. APR_DECLARE(fspr_status_t) fspr_thread_cond_destroy(fspr_thread_cond_t *cond);
  105. /**
  106. * Get the pool used by this thread_cond.
  107. * @return fspr_pool_t the pool
  108. */
  109. APR_POOL_DECLARE_ACCESSOR(thread_cond);
  110. #endif /* APR_HAS_THREADS */
  111. /** @} */
  112. #ifdef __cplusplus
  113. }
  114. #endif
  115. #endif /* ! APR_THREAD_COND_H */