BPending.h 3.7 KB

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