BLockReactor.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @file BLockReactor.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <misc/debug.h>
  30. #include <misc/offset.h>
  31. #include <base/BLog.h>
  32. #include "BLockReactor.h"
  33. #include <generated/blog_channel_BLockReactor.h>
  34. static void thread_signal_handler (BThreadSignal *thread_signal)
  35. {
  36. BLockReactor *o = UPPER_OBJECT(thread_signal, BLockReactor, thread_signal);
  37. DebugObject_Access(&o->d_obj);
  38. ASSERT_FORCE(sem_post(&o->sem1) == 0)
  39. ASSERT_FORCE(sem_wait(&o->sem2) == 0)
  40. }
  41. int BLockReactor_Init (BLockReactor *o, BReactor *reactor)
  42. {
  43. o->reactor = reactor;
  44. if (!BThreadSignal_Init(&o->thread_signal, reactor, thread_signal_handler)) {
  45. BLog(BLOG_ERROR, "BThreadSignal_Init failed");
  46. goto fail0;
  47. }
  48. if (sem_init(&o->sem1, 0, 0) < 0) {
  49. BLog(BLOG_ERROR, "sem_init failed");
  50. goto fail1;
  51. }
  52. if (sem_init(&o->sem2, 0, 0) < 0) {
  53. BLog(BLOG_ERROR, "sem_init failed");
  54. goto fail2;
  55. }
  56. #ifndef NDEBUG
  57. o->locked = 0;
  58. #endif
  59. DebugObject_Init(&o->d_obj);
  60. return 1;
  61. fail2:
  62. if (sem_close(&o->sem1) < 0) {
  63. BLog(BLOG_ERROR, "sem_close failed");
  64. }
  65. fail1:
  66. BThreadSignal_Free(&o->thread_signal);
  67. fail0:
  68. return 0;
  69. }
  70. void BLockReactor_Free (BLockReactor *o)
  71. {
  72. DebugObject_Free(&o->d_obj);
  73. #ifndef NDEBUG
  74. ASSERT(!o->locked)
  75. #endif
  76. if (sem_destroy(&o->sem2) < 0) {
  77. BLog(BLOG_ERROR, "sem_close failed");
  78. }
  79. if (sem_destroy(&o->sem1) < 0) {
  80. BLog(BLOG_ERROR, "sem_close failed");
  81. }
  82. BThreadSignal_Free(&o->thread_signal);
  83. }
  84. int BLockReactor_Thread_Lock (BLockReactor *o)
  85. {
  86. DebugObject_Access(&o->d_obj);
  87. #ifndef NDEBUG
  88. ASSERT(!o->locked)
  89. #endif
  90. if (!BThreadSignal_Thread_Signal(&o->thread_signal)) {
  91. return 0;
  92. }
  93. ASSERT_FORCE(sem_wait(&o->sem1) == 0)
  94. #ifndef NDEBUG
  95. o->locked = 1;
  96. #endif
  97. return 1;
  98. }
  99. void BLockReactor_Thread_Unlock (BLockReactor *o)
  100. {
  101. DebugObject_Access(&o->d_obj);
  102. #ifndef NDEBUG
  103. ASSERT(o->locked)
  104. #endif
  105. #ifndef NDEBUG
  106. o->locked = 0;
  107. #endif
  108. ASSERT_FORCE(sem_post(&o->sem2) == 0)
  109. }