BPending.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 BPending_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 BPending} object was
  45. * in set state.
  46. * The {@link BPending} object enters not set state before the handler
  47. * is called.
  48. *
  49. * @param user as in {@link BPending_Init}
  50. */
  51. typedef void (*BPending_handler) (void *user);
  52. /**
  53. * Object that contains a list of jobs pending execution.
  54. */
  55. typedef struct {
  56. BPending__List jobs;
  57. DebugCounter pending_ctr;
  58. DebugObject d_obj;
  59. } BPendingGroup;
  60. /**
  61. * Object for queuing a job for execution.
  62. */
  63. typedef struct BPending_s {
  64. BPendingGroup *g;
  65. BPending_handler handler;
  66. void *user;
  67. BPending__ListNode pending_node; // optimization: if not pending, .next is this
  68. #ifndef NDEBUG
  69. uint8_t pending;
  70. #endif
  71. DebugObject d_obj;
  72. } BPending;
  73. /**
  74. * Initializes the object.
  75. *
  76. * @param g the object
  77. */
  78. void BPendingGroup_Init (BPendingGroup *g);
  79. /**
  80. * Frees the object.
  81. * There must be no {@link BPending} objects using this group.
  82. *
  83. * @param g the object
  84. */
  85. void BPendingGroup_Free (BPendingGroup *g);
  86. /**
  87. * Checks if there is at least one job in the queue.
  88. *
  89. * @param g the object
  90. * @return 1 if there is at least one job, 0 if not
  91. */
  92. int BPendingGroup_HasJobs (BPendingGroup *g);
  93. /**
  94. * Executes the top job on the job list.
  95. * The job is removed from the list and enters
  96. * not set state before being executed.
  97. * There must be at least one job in job list.
  98. *
  99. * @param g the object
  100. */
  101. void BPendingGroup_ExecuteJob (BPendingGroup *g);
  102. /**
  103. * Returns the top job on the job list, or NULL if there are none.
  104. *
  105. * @param g the object
  106. * @return the top job if there is at least one job, NULL if not
  107. */
  108. BPending * BPendingGroup_PeekJob (BPendingGroup *g);
  109. /**
  110. * Initializes the object.
  111. * The object is initialized in not set state.
  112. *
  113. * @param o the object
  114. * @param g pending group to use
  115. * @param handler job execution handler
  116. * @param user value to pass to handler
  117. */
  118. void BPending_Init (BPending *o, BPendingGroup *g, BPending_handler handler, void *user);
  119. /**
  120. * Frees the object.
  121. * The execution handler will not be called after the object
  122. * is freed.
  123. *
  124. * @param o the object
  125. */
  126. void BPending_Free (BPending *o);
  127. /**
  128. * Enables the job, pushing it to the top of the job list.
  129. * If the object was already in set state, the job is removed from its
  130. * current position in the list before being pushed.
  131. * The object enters set state.
  132. *
  133. * @param o the object
  134. */
  135. void BPending_Set (BPending *o);
  136. /**
  137. * Disables the job, removing it from the job list.
  138. * If the object was not in set state, nothing is done.
  139. * The object enters not set state.
  140. *
  141. * @param o the object
  142. */
  143. void BPending_Unset (BPending *o);
  144. /**
  145. * Checks if the job is in set state.
  146. *
  147. * @param o the object
  148. * @return 1 if in set state, 0 if not
  149. */
  150. int BPending_IsSet (BPending *o);
  151. #endif