BPending.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * @file BPending.h
  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. * @section DESCRIPTION
  30. *
  31. * Module for managing a queue of jobs pending execution.
  32. */
  33. #ifndef BADVPN_BPENDING_H
  34. #define BADVPN_BPENDING_H
  35. #include <stdint.h>
  36. #include <misc/debugcounter.h>
  37. #include <structure/SLinkedList.h>
  38. #include <base/DebugObject.h>
  39. struct BSmallPending_s;
  40. #include "BPending_list.h"
  41. #include <structure/SLinkedList_decl.h>
  42. /**
  43. * Job execution handler.
  44. * It is guaranteed that the associated {@link BSmallPending} object was
  45. * in set state.
  46. * The {@link BSmallPending} object enters not set state before the handler
  47. * is called.
  48. *
  49. * @param user as in {@link BSmallPending_Init}
  50. */
  51. typedef void (*BSmallPending_handler) (void *user);
  52. /**
  53. * Job execution handler.
  54. * It is guaranteed that the associated {@link BPending} object was
  55. * in set state.
  56. * The {@link BPending} object enters not set state before the handler
  57. * is called.
  58. *
  59. * @param user as in {@link BPending_Init}
  60. */
  61. typedef void (*BPending_handler) (void *user);
  62. /**
  63. * Object that contains a list of jobs pending execution.
  64. */
  65. typedef struct {
  66. BPending__List jobs;
  67. DebugCounter pending_ctr;
  68. DebugObject d_obj;
  69. } BPendingGroup;
  70. /**
  71. * Object for queuing a job for execution.
  72. */
  73. typedef struct BSmallPending_s {
  74. BPending_handler handler;
  75. void *user;
  76. BPending__ListNode pending_node; // optimization: if not pending, .next is this
  77. #ifndef NDEBUG
  78. uint8_t pending;
  79. #endif
  80. DebugObject d_obj;
  81. } BSmallPending;
  82. /**
  83. * Object for queuing a job for execution. This is a convenience wrapper
  84. * around {@link BSmallPending} with an extra field to remember the
  85. * {@link BPendingGroup} being used.
  86. */
  87. typedef struct {
  88. BSmallPending base;
  89. BPendingGroup *g;
  90. } BPending;
  91. /**
  92. * Initializes the object.
  93. *
  94. * @param g the object
  95. */
  96. void BPendingGroup_Init (BPendingGroup *g);
  97. /**
  98. * Frees the object.
  99. * There must be no {@link BPending} or {@link BSmallPending} objects using
  100. * this group.
  101. *
  102. * @param g the object
  103. */
  104. void BPendingGroup_Free (BPendingGroup *g);
  105. /**
  106. * Checks if there is at least one job in the queue.
  107. *
  108. * @param g the object
  109. * @return 1 if there is at least one job, 0 if not
  110. */
  111. int BPendingGroup_HasJobs (BPendingGroup *g);
  112. /**
  113. * Executes the top job on the job list.
  114. * The job is removed from the list and enters
  115. * not set state before being executed.
  116. * There must be at least one job in job list.
  117. *
  118. * @param g the object
  119. */
  120. void BPendingGroup_ExecuteJob (BPendingGroup *g);
  121. /**
  122. * Returns the top job on the job list, or NULL if there are none.
  123. *
  124. * @param g the object
  125. * @return the top job if there is at least one job, NULL if not
  126. */
  127. BSmallPending * BPendingGroup_PeekJob (BPendingGroup *g);
  128. /**
  129. * Initializes the object.
  130. * The object is initialized in not set state.
  131. *
  132. * @param o the object
  133. * @param g pending group to use
  134. * @param handler job execution handler
  135. * @param user value to pass to handler
  136. */
  137. void BSmallPending_Init (BSmallPending *o, BPendingGroup *g, BSmallPending_handler handler, void *user);
  138. /**
  139. * Frees the object.
  140. * The execution handler will not be called after the object
  141. * is freed.
  142. *
  143. * @param o the object
  144. * @param g pending group. Must be the same as was used in {@link BSmallPending_Init}.
  145. */
  146. void BSmallPending_Free (BSmallPending *o, BPendingGroup *g);
  147. /**
  148. * Enables the job, pushing it to the top of the job list.
  149. * If the object was already in set state, the job is removed from its
  150. * current position in the list before being pushed.
  151. * The object enters set state.
  152. *
  153. * @param o the object
  154. * @param g pending group. Must be the same as was used in {@link BSmallPending_Init}.
  155. */
  156. void BSmallPending_Set (BSmallPending *o, BPendingGroup *g);
  157. /**
  158. * Disables the job, removing it from the job list.
  159. * If the object was not in set state, nothing is done.
  160. * The object enters not set state.
  161. *
  162. * @param o the object
  163. * @param g pending group. Must be the same as was used in {@link BSmallPending_Init}.
  164. */
  165. void BSmallPending_Unset (BSmallPending *o, BPendingGroup *g);
  166. /**
  167. * Checks if the job is in set state.
  168. *
  169. * @param o the object
  170. * @return 1 if in set state, 0 if not
  171. */
  172. int BSmallPending_IsSet (BSmallPending *o);
  173. /**
  174. * Initializes the object.
  175. * The object is initialized in not set state.
  176. *
  177. * @param o the object
  178. * @param g pending group to use
  179. * @param handler job execution handler
  180. * @param user value to pass to handler
  181. */
  182. void BPending_Init (BPending *o, BPendingGroup *g, BPending_handler handler, void *user);
  183. /**
  184. * Frees the object.
  185. * The execution handler will not be called after the object
  186. * is freed.
  187. *
  188. * @param o the object
  189. */
  190. void BPending_Free (BPending *o);
  191. /**
  192. * Enables the job, pushing it to the top of the job list.
  193. * If the object was already in set state, the job is removed from its
  194. * current position in the list before being pushed.
  195. * The object enters set state.
  196. *
  197. * @param o the object
  198. */
  199. void BPending_Set (BPending *o);
  200. /**
  201. * Disables the job, removing it from the job list.
  202. * If the object was not in set state, nothing is done.
  203. * The object enters not set state.
  204. *
  205. * @param o the object
  206. */
  207. void BPending_Unset (BPending *o);
  208. /**
  209. * Checks if the job is in set state.
  210. *
  211. * @param o the object
  212. * @return 1 if in set state, 0 if not
  213. */
  214. int BPending_IsSet (BPending *o);
  215. #endif