thread_cond.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #if APR_HAS_THREADS
  18. #include "fspr_arch_thread_mutex.h"
  19. #include "fspr_arch_thread_cond.h"
  20. static fspr_status_t thread_cond_cleanup(void *data)
  21. {
  22. fspr_thread_cond_t *cond = (fspr_thread_cond_t *)data;
  23. fspr_status_t rv;
  24. rv = pthread_cond_destroy(&cond->cond);
  25. #ifdef PTHREAD_SETS_ERRNO
  26. if (rv) {
  27. rv = errno;
  28. }
  29. #endif
  30. return rv;
  31. }
  32. APR_DECLARE(fspr_status_t) fspr_thread_cond_create(fspr_thread_cond_t **cond,
  33. fspr_pool_t *pool)
  34. {
  35. fspr_thread_cond_t *new_cond;
  36. fspr_status_t rv;
  37. new_cond = fspr_palloc(pool, sizeof(fspr_thread_cond_t));
  38. new_cond->pool = pool;
  39. if ((rv = pthread_cond_init(&new_cond->cond, NULL))) {
  40. #ifdef PTHREAD_SETS_ERRNO
  41. rv = errno;
  42. #endif
  43. return rv;
  44. }
  45. fspr_pool_cleanup_register(new_cond->pool,
  46. (void *)new_cond, thread_cond_cleanup,
  47. fspr_pool_cleanup_null);
  48. *cond = new_cond;
  49. return APR_SUCCESS;
  50. }
  51. APR_DECLARE(fspr_status_t) fspr_thread_cond_wait(fspr_thread_cond_t *cond,
  52. fspr_thread_mutex_t *mutex)
  53. {
  54. fspr_status_t rv;
  55. rv = pthread_cond_wait(&cond->cond, &mutex->mutex);
  56. #ifdef PTHREAD_SETS_ERRNO
  57. if (rv) {
  58. rv = errno;
  59. }
  60. #endif
  61. return rv;
  62. }
  63. APR_DECLARE(fspr_status_t) fspr_thread_cond_timedwait(fspr_thread_cond_t *cond,
  64. fspr_thread_mutex_t *mutex,
  65. fspr_interval_time_t timeout)
  66. {
  67. fspr_status_t rv;
  68. fspr_time_t then;
  69. struct timespec abstime;
  70. then = fspr_time_now() + timeout;
  71. abstime.tv_sec = fspr_time_sec(then);
  72. abstime.tv_nsec = fspr_time_usec(then) * 1000; /* nanoseconds */
  73. rv = pthread_cond_timedwait(&cond->cond, &mutex->mutex, &abstime);
  74. #ifdef PTHREAD_SETS_ERRNO
  75. if (rv) {
  76. rv = errno;
  77. }
  78. #endif
  79. if (ETIMEDOUT == rv) {
  80. return APR_TIMEUP;
  81. }
  82. return rv;
  83. }
  84. APR_DECLARE(fspr_status_t) fspr_thread_cond_signal(fspr_thread_cond_t *cond)
  85. {
  86. fspr_status_t rv;
  87. rv = pthread_cond_signal(&cond->cond);
  88. #ifdef PTHREAD_SETS_ERRNO
  89. if (rv) {
  90. rv = errno;
  91. }
  92. #endif
  93. return rv;
  94. }
  95. APR_DECLARE(fspr_status_t) fspr_thread_cond_broadcast(fspr_thread_cond_t *cond)
  96. {
  97. fspr_status_t rv;
  98. rv = pthread_cond_broadcast(&cond->cond);
  99. #ifdef PTHREAD_SETS_ERRNO
  100. if (rv) {
  101. rv = errno;
  102. }
  103. #endif
  104. return rv;
  105. }
  106. APR_DECLARE(fspr_status_t) fspr_thread_cond_destroy(fspr_thread_cond_t *cond)
  107. {
  108. return fspr_pool_cleanup_run(cond->pool, cond, thread_cond_cleanup);
  109. }
  110. APR_POOL_IMPLEMENT_ACCESSOR(thread_cond)
  111. #endif /* APR_HAS_THREADS */