BPending.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. LinkedList2_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(LinkedList2_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 !LinkedList2_IsEmpty(&g->jobs);
  45. }
  46. void BPendingGroup_ExecuteJob (BPendingGroup *g)
  47. {
  48. ASSERT(!LinkedList2_IsEmpty(&g->jobs))
  49. DebugObject_Access(&g->d_obj);
  50. // get a job
  51. LinkedList2Node *node = LinkedList2_GetFirst(&g->jobs);
  52. BPending *p = UPPER_OBJECT(node, BPending, pending_node);
  53. ASSERT(p->pending)
  54. // remove from jobs list
  55. LinkedList2_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. void BPending_Init (BPending *o, BPendingGroup *g, BPending_handler handler, void *user)
  63. {
  64. // init arguments
  65. o->g = g;
  66. o->handler = handler;
  67. o->user = user;
  68. // set not pending
  69. o->pending = 0;
  70. // increment pending counter
  71. DebugCounter_Increment(&o->g->pending_ctr);
  72. // init debug object
  73. DebugObject_Init(&o->d_obj);
  74. }
  75. void BPending_Free (BPending *o)
  76. {
  77. DebugCounter_Decrement(&o->g->pending_ctr);
  78. DebugObject_Free(&o->d_obj);
  79. // remove from jobs list
  80. if (o->pending) {
  81. LinkedList2_Remove(&o->g->jobs, &o->pending_node);
  82. }
  83. }
  84. void BPending_Set (BPending *o)
  85. {
  86. DebugObject_Access(&o->d_obj);
  87. // remove from jobs list
  88. if (o->pending) {
  89. LinkedList2_Remove(&o->g->jobs, &o->pending_node);
  90. }
  91. // insert to jobs list
  92. LinkedList2_Append(&o->g->jobs, &o->pending_node);
  93. // set pending
  94. o->pending = 1;
  95. }
  96. void BPending_Unset (BPending *o)
  97. {
  98. DebugObject_Access(&o->d_obj);
  99. if (o->pending) {
  100. // remove from jobs list
  101. LinkedList2_Remove(&o->g->jobs, &o->pending_node);
  102. // set not pending
  103. o->pending = 0;
  104. }
  105. }