BEventLock.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @file BEventLock.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <misc/offset.h>
  23. #include <system/BEventLock.h>
  24. static void exec_job_handler (BEventLock *o)
  25. {
  26. ASSERT(!LinkedList2_IsEmpty(&o->jobs))
  27. DebugObject_Access(&o->d_obj);
  28. // get job
  29. BEventLockJob *j = UPPER_OBJECT(LinkedList2_GetFirst(&o->jobs), BEventLockJob, pending_node);
  30. ASSERT(j->pending)
  31. // call handler
  32. j->handler(j->user);
  33. return;
  34. }
  35. void BEventLock_Init (BEventLock *o, BPendingGroup *pg)
  36. {
  37. // init jobs list
  38. LinkedList2_Init(&o->jobs);
  39. // init exec job
  40. BPending_Init(&o->exec_job, pg, (BPending_handler)exec_job_handler, o);
  41. DebugObject_Init(&o->d_obj);
  42. DebugCounter_Init(&o->pending_ctr);
  43. }
  44. void BEventLock_Free (BEventLock *o)
  45. {
  46. ASSERT(LinkedList2_IsEmpty(&o->jobs))
  47. DebugCounter_Free(&o->pending_ctr);
  48. DebugObject_Free(&o->d_obj);
  49. // free exec jobs
  50. BPending_Free(&o->exec_job);
  51. }
  52. void BEventLockJob_Init (BEventLockJob *o, BEventLock *l, BEventLock_handler handler, void *user)
  53. {
  54. // init arguments
  55. o->l = l;
  56. o->handler = handler;
  57. o->user = user;
  58. // set not pending
  59. o->pending = 0;
  60. DebugObject_Init(&o->d_obj);
  61. DebugCounter_Increment(&l->pending_ctr);
  62. }
  63. void BEventLockJob_Free (BEventLockJob *o)
  64. {
  65. BEventLock *l = o->l;
  66. DebugCounter_Decrement(&l->pending_ctr);
  67. DebugObject_Free(&o->d_obj);
  68. if (o->pending) {
  69. int was_first = (&o->pending_node == LinkedList2_GetFirst(&l->jobs));
  70. // remove from jobs list
  71. LinkedList2_Remove(&l->jobs, &o->pending_node);
  72. // schedule/unschedule job
  73. if (was_first) {
  74. if (LinkedList2_IsEmpty(&l->jobs)) {
  75. BPending_Unset(&l->exec_job);
  76. } else {
  77. BPending_Set(&l->exec_job);
  78. }
  79. }
  80. }
  81. }
  82. void BEventLockJob_Wait (BEventLockJob *o)
  83. {
  84. BEventLock *l = o->l;
  85. ASSERT(!o->pending)
  86. // append to jobs
  87. LinkedList2_Append(&l->jobs, &o->pending_node);
  88. // set pending
  89. o->pending = 1;
  90. // schedule next job if needed
  91. if (&o->pending_node == LinkedList2_GetFirst(&l->jobs)) {
  92. BPending_Set(&l->exec_job);
  93. }
  94. }
  95. void BEventLockJob_Release (BEventLockJob *o)
  96. {
  97. BEventLock *l = o->l;
  98. ASSERT(o->pending)
  99. int was_first = (&o->pending_node == LinkedList2_GetFirst(&l->jobs));
  100. // remove from jobs list
  101. LinkedList2_Remove(&l->jobs, &o->pending_node);
  102. // set not pending
  103. o->pending = 0;
  104. // schedule/unschedule job
  105. if (was_first) {
  106. if (LinkedList2_IsEmpty(&l->jobs)) {
  107. BPending_Unset(&l->exec_job);
  108. } else {
  109. BPending_Set(&l->exec_job);
  110. }
  111. }
  112. }