BPending.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * @file BPending.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 <stddef.h>
  30. #include <misc/debug.h>
  31. #include <misc/offset.h>
  32. #include "BPending.h"
  33. #include "BPending_list.h"
  34. #include <structure/SLinkedList_impl.h>
  35. void BPendingGroup_Init (BPendingGroup *g)
  36. {
  37. // init jobs list
  38. BPending__List_Init(&g->jobs);
  39. // init pending counter
  40. DebugCounter_Init(&g->pending_ctr);
  41. // init debug object
  42. DebugObject_Init(&g->d_obj);
  43. }
  44. void BPendingGroup_Free (BPendingGroup *g)
  45. {
  46. DebugCounter_Free(&g->pending_ctr);
  47. ASSERT(BPending__List_IsEmpty(&g->jobs))
  48. DebugObject_Free(&g->d_obj);
  49. }
  50. int BPendingGroup_HasJobs (BPendingGroup *g)
  51. {
  52. DebugObject_Access(&g->d_obj);
  53. return !BPending__List_IsEmpty(&g->jobs);
  54. }
  55. void BPendingGroup_ExecuteJob (BPendingGroup *g)
  56. {
  57. ASSERT(!BPending__List_IsEmpty(&g->jobs))
  58. DebugObject_Access(&g->d_obj);
  59. // get a job
  60. BSmallPending *p = BPending__List_First(&g->jobs);
  61. ASSERT(p->pending_node.next != p)
  62. ASSERT(p->pending)
  63. // remove from jobs list
  64. BPending__List_Remove(&g->jobs, p);
  65. // set not pending
  66. p->pending_node.next = p;
  67. #ifndef NDEBUG
  68. p->pending = 0;
  69. #endif
  70. // execute job
  71. p->handler(p->user);
  72. return;
  73. }
  74. BSmallPending * BPendingGroup_PeekJob (BPendingGroup *g)
  75. {
  76. DebugObject_Access(&g->d_obj);
  77. return BPending__List_First(&g->jobs);
  78. }
  79. void BSmallPending_Init (BSmallPending *o, BPendingGroup *g, BSmallPending_handler handler, void *user)
  80. {
  81. // init arguments
  82. o->handler = handler;
  83. o->user = user;
  84. // set not pending
  85. o->pending_node.next = o;
  86. #ifndef NDEBUG
  87. o->pending = 0;
  88. #endif
  89. // increment pending counter
  90. DebugCounter_Increment(&g->pending_ctr);
  91. // init debug object
  92. DebugObject_Init(&o->d_obj);
  93. }
  94. void BSmallPending_Free (BSmallPending *o, BPendingGroup *g)
  95. {
  96. DebugCounter_Decrement(&g->pending_ctr);
  97. DebugObject_Free(&o->d_obj);
  98. ASSERT(o->pending == (o->pending_node.next != o))
  99. // remove from jobs list
  100. if (o->pending_node.next != o) {
  101. BPending__List_Remove(&g->jobs, o);
  102. }
  103. }
  104. void BSmallPending_SetHandler (BSmallPending *o, BSmallPending_handler handler, void *user)
  105. {
  106. DebugObject_Access(&o->d_obj);
  107. // set handler
  108. o->handler = handler;
  109. o->user = user;
  110. }
  111. void BSmallPending_Set (BSmallPending *o, BPendingGroup *g)
  112. {
  113. DebugObject_Access(&o->d_obj);
  114. ASSERT(o->pending == (o->pending_node.next != o))
  115. // remove from jobs list
  116. if (o->pending_node.next != o) {
  117. BPending__List_Remove(&g->jobs, o);
  118. }
  119. // insert to jobs list
  120. BPending__List_Prepend(&g->jobs, o);
  121. // set pending
  122. #ifndef NDEBUG
  123. o->pending = 1;
  124. #endif
  125. }
  126. void BSmallPending_Unset (BSmallPending *o, BPendingGroup *g)
  127. {
  128. DebugObject_Access(&o->d_obj);
  129. ASSERT(o->pending == (o->pending_node.next != o))
  130. if (o->pending_node.next != o) {
  131. // remove from jobs list
  132. BPending__List_Remove(&g->jobs, o);
  133. // set not pending
  134. o->pending_node.next = o;
  135. #ifndef NDEBUG
  136. o->pending = 0;
  137. #endif
  138. }
  139. }
  140. int BSmallPending_IsSet (BSmallPending *o)
  141. {
  142. DebugObject_Access(&o->d_obj);
  143. ASSERT(o->pending == (o->pending_node.next != o))
  144. return (o->pending_node.next != o);
  145. }
  146. void BPending_Init (BPending *o, BPendingGroup *g, BPending_handler handler, void *user)
  147. {
  148. BSmallPending_Init(&o->base, g, handler, user);
  149. o->g = g;
  150. }
  151. void BPending_Free (BPending *o)
  152. {
  153. BSmallPending_Free(&o->base, o->g);
  154. }
  155. void BPending_Set (BPending *o)
  156. {
  157. BSmallPending_Set(&o->base, o->g);
  158. }
  159. void BPending_Unset (BPending *o)
  160. {
  161. BSmallPending_Unset(&o->base, o->g);
  162. }
  163. int BPending_IsSet (BPending *o)
  164. {
  165. return BSmallPending_IsSet(&o->base);
  166. }