BEventLock.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @file BEventLock.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/offset.h>
  30. #include "BEventLock.h"
  31. static void exec_job_handler (BEventLock *o)
  32. {
  33. ASSERT(!LinkedList1_IsEmpty(&o->jobs))
  34. DebugObject_Access(&o->d_obj);
  35. // get job
  36. BEventLockJob *j = UPPER_OBJECT(LinkedList1_GetFirst(&o->jobs), BEventLockJob, pending_node);
  37. ASSERT(j->pending)
  38. // call handler
  39. j->handler(j->user);
  40. return;
  41. }
  42. void BEventLock_Init (BEventLock *o, BPendingGroup *pg)
  43. {
  44. // init jobs list
  45. LinkedList1_Init(&o->jobs);
  46. // init exec job
  47. BPending_Init(&o->exec_job, pg, (BPending_handler)exec_job_handler, o);
  48. DebugObject_Init(&o->d_obj);
  49. DebugCounter_Init(&o->pending_ctr);
  50. }
  51. void BEventLock_Free (BEventLock *o)
  52. {
  53. ASSERT(LinkedList1_IsEmpty(&o->jobs))
  54. DebugCounter_Free(&o->pending_ctr);
  55. DebugObject_Free(&o->d_obj);
  56. // free exec jobs
  57. BPending_Free(&o->exec_job);
  58. }
  59. void BEventLockJob_Init (BEventLockJob *o, BEventLock *l, BEventLock_handler handler, void *user)
  60. {
  61. // init arguments
  62. o->l = l;
  63. o->handler = handler;
  64. o->user = user;
  65. // set not pending
  66. o->pending = 0;
  67. DebugObject_Init(&o->d_obj);
  68. DebugCounter_Increment(&l->pending_ctr);
  69. }
  70. void BEventLockJob_Free (BEventLockJob *o)
  71. {
  72. BEventLock *l = o->l;
  73. DebugCounter_Decrement(&l->pending_ctr);
  74. DebugObject_Free(&o->d_obj);
  75. if (o->pending) {
  76. int was_first = (&o->pending_node == LinkedList1_GetFirst(&l->jobs));
  77. // remove from jobs list
  78. LinkedList1_Remove(&l->jobs, &o->pending_node);
  79. // schedule/unschedule job
  80. if (was_first) {
  81. if (LinkedList1_IsEmpty(&l->jobs)) {
  82. BPending_Unset(&l->exec_job);
  83. } else {
  84. BPending_Set(&l->exec_job);
  85. }
  86. }
  87. }
  88. }
  89. void BEventLockJob_Wait (BEventLockJob *o)
  90. {
  91. BEventLock *l = o->l;
  92. ASSERT(!o->pending)
  93. // append to jobs
  94. LinkedList1_Append(&l->jobs, &o->pending_node);
  95. // set pending
  96. o->pending = 1;
  97. // schedule next job if needed
  98. if (&o->pending_node == LinkedList1_GetFirst(&l->jobs)) {
  99. BPending_Set(&l->exec_job);
  100. }
  101. }
  102. void BEventLockJob_Release (BEventLockJob *o)
  103. {
  104. BEventLock *l = o->l;
  105. ASSERT(o->pending)
  106. int was_first = (&o->pending_node == LinkedList1_GetFirst(&l->jobs));
  107. // remove from jobs list
  108. LinkedList1_Remove(&l->jobs, &o->pending_node);
  109. // set not pending
  110. o->pending = 0;
  111. // schedule/unschedule job
  112. if (was_first) {
  113. if (LinkedList1_IsEmpty(&l->jobs)) {
  114. BPending_Unset(&l->exec_job);
  115. } else {
  116. BPending_Set(&l->exec_job);
  117. }
  118. }
  119. }