BPending.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. BPending *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. BPending * BPendingGroup_PeekJob (BPendingGroup *g)
  75. {
  76. DebugObject_Access(&g->d_obj);
  77. return BPending__List_First(&g->jobs);
  78. }
  79. void BPending_Init (BPending *o, BPendingGroup *g, BPending_handler handler, void *user)
  80. {
  81. // init arguments
  82. o->g = g;
  83. o->handler = handler;
  84. o->user = user;
  85. // set not pending
  86. o->pending_node.next = o;
  87. #ifndef NDEBUG
  88. o->pending = 0;
  89. #endif
  90. // increment pending counter
  91. DebugCounter_Increment(&o->g->pending_ctr);
  92. // init debug object
  93. DebugObject_Init(&o->d_obj);
  94. }
  95. void BPending_Free (BPending *o)
  96. {
  97. DebugCounter_Decrement(&o->g->pending_ctr);
  98. DebugObject_Free(&o->d_obj);
  99. ASSERT(o->pending == (o->pending_node.next != o))
  100. // remove from jobs list
  101. if (o->pending_node.next != o) {
  102. BPending__List_Remove(&o->g->jobs, o);
  103. }
  104. }
  105. void BPending_Set (BPending *o)
  106. {
  107. DebugObject_Access(&o->d_obj);
  108. ASSERT(o->pending == (o->pending_node.next != o))
  109. // remove from jobs list
  110. if (o->pending_node.next != o) {
  111. BPending__List_Remove(&o->g->jobs, o);
  112. }
  113. // insert to jobs list
  114. BPending__List_Prepend(&o->g->jobs, o);
  115. // set pending
  116. #ifndef NDEBUG
  117. o->pending = 1;
  118. #endif
  119. }
  120. void BPending_Unset (BPending *o)
  121. {
  122. DebugObject_Access(&o->d_obj);
  123. ASSERT(o->pending == (o->pending_node.next != o))
  124. if (o->pending_node.next != o) {
  125. // remove from jobs list
  126. BPending__List_Remove(&o->g->jobs, o);
  127. // set not pending
  128. o->pending_node.next = o;
  129. #ifndef NDEBUG
  130. o->pending = 0;
  131. #endif
  132. }
  133. }
  134. int BPending_IsSet (BPending *o)
  135. {
  136. DebugObject_Access(&o->d_obj);
  137. ASSERT(o->pending == (o->pending_node.next != o))
  138. return (o->pending_node.next != o);
  139. }