BPending.c 3.4 KB

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