thread_cond.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <nks/errno.h>
  17. #include "apr.h"
  18. #include "apr_private.h"
  19. #include "apr_general.h"
  20. #include "apr_strings.h"
  21. #include "apr_arch_thread_mutex.h"
  22. #include "apr_arch_thread_cond.h"
  23. #include "apr_portable.h"
  24. static apr_status_t thread_cond_cleanup(void *data)
  25. {
  26. apr_thread_cond_t *cond = (apr_thread_cond_t *)data;
  27. NXCondFree(cond->cond);
  28. return APR_SUCCESS;
  29. }
  30. APR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond,
  31. apr_pool_t *pool)
  32. {
  33. apr_thread_cond_t *new_cond = NULL;
  34. new_cond = (apr_thread_cond_t *)apr_pcalloc(pool, sizeof(apr_thread_cond_t));
  35. if(new_cond ==NULL) {
  36. return APR_ENOMEM;
  37. }
  38. new_cond->pool = pool;
  39. new_cond->cond = NXCondAlloc(NULL);
  40. if(new_cond->cond == NULL)
  41. return APR_ENOMEM;
  42. apr_pool_cleanup_register(new_cond->pool, new_cond,
  43. (void*)thread_cond_cleanup,
  44. apr_pool_cleanup_null);
  45. *cond = new_cond;
  46. return APR_SUCCESS;
  47. }
  48. APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond,
  49. apr_thread_mutex_t *mutex)
  50. {
  51. if (NXCondWait(cond->cond, mutex->mutex) != 0)
  52. return APR_EINTR;
  53. return APR_SUCCESS;
  54. }
  55. APR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond,
  56. apr_thread_mutex_t *mutex,
  57. apr_interval_time_t timeout){
  58. if (NXCondTimedWait(cond->cond, mutex->mutex,
  59. (timeout*1000)/NXGetSystemTick()) == NX_ETIMEDOUT) {
  60. return APR_TIMEUP;
  61. }
  62. return APR_SUCCESS;
  63. }
  64. APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond)
  65. {
  66. NXCondSignal(cond->cond);
  67. return APR_SUCCESS;
  68. }
  69. APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond)
  70. {
  71. NXCondBroadcast(cond->cond);
  72. return APR_SUCCESS;
  73. }
  74. APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond)
  75. {
  76. apr_status_t stat;
  77. if ((stat = thread_cond_cleanup(cond)) == APR_SUCCESS) {
  78. apr_pool_cleanup_kill(cond->pool, cond, thread_cond_cleanup);
  79. return APR_SUCCESS;
  80. }
  81. return stat;
  82. }
  83. APR_POOL_IMPLEMENT_ACCESSOR(thread_cond)