BPending.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @file BPending.h
  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. * @section DESCRIPTION
  23. *
  24. * Module for managing a queue of jobs pending execution.
  25. */
  26. #ifndef BADVPN_BPENDING_H
  27. #define BADVPN_BPENDING_H
  28. #include <stdint.h>
  29. #include <misc/debugcounter.h>
  30. #include <structure/LinkedList1.h>
  31. #include <base/DebugObject.h>
  32. /**
  33. * Job execution handler.
  34. * It is guaranteed that the associated {@link BPending} object was
  35. * in set state.
  36. * The {@link BPending} object enters not set state before the handler
  37. * is called.
  38. *
  39. * @param user as in {@link BPending_Init}
  40. */
  41. typedef void (*BPending_handler) (void *user);
  42. /**
  43. * Object that contains a list of jobs pending execution.
  44. */
  45. typedef struct {
  46. LinkedList1 jobs;
  47. DebugCounter pending_ctr;
  48. DebugObject d_obj;
  49. } BPendingGroup;
  50. /**
  51. * Object for queuing a job for execution.
  52. */
  53. typedef struct {
  54. BPendingGroup *g;
  55. BPending_handler handler;
  56. void *user;
  57. uint8_t pending;
  58. LinkedList1Node pending_node;
  59. DebugObject d_obj;
  60. } BPending;
  61. /**
  62. * Initializes the object.
  63. *
  64. * @param g the object
  65. */
  66. void BPendingGroup_Init (BPendingGroup *g);
  67. /**
  68. * Frees the object.
  69. * There must be no {@link BPending} objects using this group.
  70. *
  71. * @param g the object
  72. */
  73. void BPendingGroup_Free (BPendingGroup *g);
  74. /**
  75. * Checks if there is at least one job in the queue.
  76. *
  77. * @param g the object
  78. * @return 1 if there is at least one job, 0 if not
  79. */
  80. int BPendingGroup_HasJobs (BPendingGroup *g);
  81. /**
  82. * Executes the top job on the job list.
  83. * The job is removed from the list and enters
  84. * not set state before being executed.
  85. * There must be at least one job in job list.
  86. *
  87. * @param g the object
  88. */
  89. void BPendingGroup_ExecuteJob (BPendingGroup *g);
  90. /**
  91. * Returns the top job on the job list, or NULL if there are none.
  92. *
  93. * @param g the object
  94. * @return the top job if there is at least one job, NULL if not
  95. */
  96. BPending * BPendingGroup_PeekJob (BPendingGroup *g);
  97. /**
  98. * Initializes the object.
  99. * The object is initialized in not set state.
  100. *
  101. * @param o the object
  102. * @param g pending group to use
  103. * @param handler job execution handler
  104. * @param user value to pass to handler
  105. */
  106. void BPending_Init (BPending *o, BPendingGroup *g, BPending_handler handler, void *user);
  107. /**
  108. * Frees the object.
  109. * The execution handler will not be called after the object
  110. * is freed.
  111. *
  112. * @param o the object
  113. */
  114. void BPending_Free (BPending *o);
  115. /**
  116. * Enables the job, pushing it to the top of the job list.
  117. * If the object was already in set state, the job is removed from its
  118. * current position in the list before being pushed.
  119. * The object enters set state.
  120. *
  121. * @param o the object
  122. */
  123. void BPending_Set (BPending *o);
  124. /**
  125. * Disables the job, removing it from the job list.
  126. * If the object was not in set state, nothing is done.
  127. * The object enters not set state.
  128. *
  129. * @param o the object
  130. */
  131. void BPending_Unset (BPending *o);
  132. /**
  133. * Checks if the job is in set state.
  134. *
  135. * @param o the object
  136. * @return 1 if in set state, 0 if not
  137. */
  138. int BPending_IsSet (BPending *o);
  139. #endif