thread_cond.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #include "fspr.h"
  17. #include "fspr_private.h"
  18. #include "fspr_general.h"
  19. #include "fspr_strings.h"
  20. #include "fspr_arch_thread_mutex.h"
  21. #include "fspr_arch_thread_cond.h"
  22. #include "fspr_portable.h"
  23. #include <limits.h>
  24. static fspr_status_t thread_cond_cleanup(void *data)
  25. {
  26. fspr_thread_cond_t *cond = data;
  27. CloseHandle(cond->semaphore);
  28. DeleteCriticalSection(&cond->csection);
  29. return APR_SUCCESS;
  30. }
  31. APR_DECLARE(fspr_status_t) fspr_thread_cond_create(fspr_thread_cond_t **cond,
  32. fspr_pool_t *pool)
  33. {
  34. fspr_thread_cond_t *cv;
  35. cv = fspr_pcalloc(pool, sizeof(**cond));
  36. if (cv == NULL) {
  37. return APR_ENOMEM;
  38. }
  39. cv->semaphore = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
  40. if (cv->semaphore == NULL) {
  41. return fspr_get_os_error();
  42. }
  43. *cond = cv;
  44. cv->pool = pool;
  45. InitializeCriticalSection(&cv->csection);
  46. fspr_pool_cleanup_register(cv->pool, cv, thread_cond_cleanup,
  47. fspr_pool_cleanup_null);
  48. return APR_SUCCESS;
  49. }
  50. APR_DECLARE(fspr_status_t) fspr_thread_cond_destroy(fspr_thread_cond_t *cond)
  51. {
  52. return fspr_pool_cleanup_run(cond->pool, cond, thread_cond_cleanup);
  53. }
  54. static APR_INLINE fspr_status_t _thread_cond_timedwait(fspr_thread_cond_t *cond,
  55. fspr_thread_mutex_t *mutex,
  56. DWORD timeout_ms )
  57. {
  58. DWORD res;
  59. fspr_status_t rv;
  60. unsigned int wake = 0;
  61. unsigned long generation;
  62. EnterCriticalSection(&cond->csection);
  63. cond->num_waiting++;
  64. generation = cond->generation;
  65. LeaveCriticalSection(&cond->csection);
  66. fspr_thread_mutex_unlock(mutex);
  67. do {
  68. res = WaitForSingleObject(cond->semaphore, timeout_ms);
  69. EnterCriticalSection(&cond->csection);
  70. if (cond->num_wake) {
  71. if (cond->generation != generation) {
  72. cond->num_wake--;
  73. cond->num_waiting--;
  74. rv = APR_SUCCESS;
  75. break;
  76. } else {
  77. wake = 1;
  78. }
  79. }
  80. else if (res != WAIT_OBJECT_0) {
  81. cond->num_waiting--;
  82. rv = APR_TIMEUP;
  83. break;
  84. }
  85. LeaveCriticalSection(&cond->csection);
  86. if (wake) {
  87. wake = 0;
  88. ReleaseSemaphore(cond->semaphore, 1, NULL);
  89. }
  90. } while (1);
  91. LeaveCriticalSection(&cond->csection);
  92. fspr_thread_mutex_lock(mutex);
  93. return rv;
  94. }
  95. APR_DECLARE(fspr_status_t) fspr_thread_cond_wait(fspr_thread_cond_t *cond,
  96. fspr_thread_mutex_t *mutex)
  97. {
  98. return _thread_cond_timedwait(cond, mutex, INFINITE);
  99. }
  100. APR_DECLARE(fspr_status_t) fspr_thread_cond_timedwait(fspr_thread_cond_t *cond,
  101. fspr_thread_mutex_t *mutex,
  102. fspr_interval_time_t timeout)
  103. {
  104. DWORD timeout_ms = (DWORD) fspr_time_as_msec(timeout);
  105. return _thread_cond_timedwait(cond, mutex, timeout_ms);
  106. }
  107. APR_DECLARE(fspr_status_t) fspr_thread_cond_signal(fspr_thread_cond_t *cond)
  108. {
  109. unsigned int wake = 0;
  110. EnterCriticalSection(&cond->csection);
  111. if (cond->num_waiting > cond->num_wake) {
  112. wake = 1;
  113. cond->num_wake++;
  114. cond->generation++;
  115. }
  116. LeaveCriticalSection(&cond->csection);
  117. if (wake) {
  118. ReleaseSemaphore(cond->semaphore, 1, NULL);
  119. }
  120. return APR_SUCCESS;
  121. }
  122. APR_DECLARE(fspr_status_t) fspr_thread_cond_broadcast(fspr_thread_cond_t *cond)
  123. {
  124. unsigned long num_wake = 0;
  125. EnterCriticalSection(&cond->csection);
  126. if (cond->num_waiting > cond->num_wake) {
  127. num_wake = cond->num_waiting - cond->num_wake;
  128. cond->num_wake = cond->num_waiting;
  129. cond->generation++;
  130. }
  131. LeaveCriticalSection(&cond->csection);
  132. if (num_wake) {
  133. ReleaseSemaphore(cond->semaphore, num_wake, NULL);
  134. }
  135. return APR_SUCCESS;
  136. }
  137. APR_POOL_IMPLEMENT_ACCESSOR(thread_cond)