BPending.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. void BPendingGroup_Init (BPendingGroup *g)
  34. {
  35. // init jobs list
  36. LinkedList1_Init(&g->jobs);
  37. // init pending counter
  38. DebugCounter_Init(&g->pending_ctr);
  39. // init debug object
  40. DebugObject_Init(&g->d_obj);
  41. }
  42. void BPendingGroup_Free (BPendingGroup *g)
  43. {
  44. DebugCounter_Free(&g->pending_ctr);
  45. ASSERT(LinkedList1_IsEmpty(&g->jobs))
  46. DebugObject_Free(&g->d_obj);
  47. }
  48. int BPendingGroup_HasJobs (BPendingGroup *g)
  49. {
  50. DebugObject_Access(&g->d_obj);
  51. return !LinkedList1_IsEmpty(&g->jobs);
  52. }
  53. void BPendingGroup_ExecuteJob (BPendingGroup *g)
  54. {
  55. ASSERT(!LinkedList1_IsEmpty(&g->jobs))
  56. DebugObject_Access(&g->d_obj);
  57. // get a job
  58. LinkedList1Node *node = LinkedList1_GetLast(&g->jobs);
  59. BPending *p = UPPER_OBJECT(node, BPending, pending_node);
  60. ASSERT(p->pending)
  61. // remove from jobs list
  62. LinkedList1_Remove(&g->jobs, &p->pending_node);
  63. // set not pending
  64. p->pending = 0;
  65. // execute job
  66. p->handler(p->user);
  67. return;
  68. }
  69. BPending * BPendingGroup_PeekJob (BPendingGroup *g)
  70. {
  71. DebugObject_Access(&g->d_obj);
  72. LinkedList1Node *node = LinkedList1_GetLast(&g->jobs);
  73. if (!node) {
  74. return NULL;
  75. }
  76. return UPPER_OBJECT(node, BPending, pending_node);
  77. }
  78. void BPending_Init (BPending *o, BPendingGroup *g, BPending_handler handler, void *user)
  79. {
  80. // init arguments
  81. o->g = g;
  82. o->handler = handler;
  83. o->user = user;
  84. // set not pending
  85. o->pending = 0;
  86. // increment pending counter
  87. DebugCounter_Increment(&o->g->pending_ctr);
  88. // init debug object
  89. DebugObject_Init(&o->d_obj);
  90. }
  91. void BPending_Free (BPending *o)
  92. {
  93. DebugCounter_Decrement(&o->g->pending_ctr);
  94. DebugObject_Free(&o->d_obj);
  95. // remove from jobs list
  96. if (o->pending) {
  97. LinkedList1_Remove(&o->g->jobs, &o->pending_node);
  98. }
  99. }
  100. void BPending_Set (BPending *o)
  101. {
  102. DebugObject_Access(&o->d_obj);
  103. // remove from jobs list
  104. if (o->pending) {
  105. LinkedList1_Remove(&o->g->jobs, &o->pending_node);
  106. }
  107. // insert to jobs list
  108. LinkedList1_Append(&o->g->jobs, &o->pending_node);
  109. // set pending
  110. o->pending = 1;
  111. }
  112. void BPending_Unset (BPending *o)
  113. {
  114. DebugObject_Access(&o->d_obj);
  115. if (o->pending) {
  116. // remove from jobs list
  117. LinkedList1_Remove(&o->g->jobs, &o->pending_node);
  118. // set not pending
  119. o->pending = 0;
  120. }
  121. }
  122. int BPending_IsSet (BPending *o)
  123. {
  124. DebugObject_Access(&o->d_obj);
  125. return o->pending;
  126. }