BPending.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_SYSTEM_BPENDING_H
  27. #define BADVPN_SYSTEM_BPENDING_H
  28. #include <misc/debugcounter.h>
  29. #include <structure/LinkedList2.h>
  30. #include <system/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. LinkedList2 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. LinkedList2Node 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. * Initializes the object.
  91. * The object is initialized in not set state.
  92. *
  93. * @param o the object
  94. * @param g pending group to use
  95. * @param handler job execution handler
  96. * @param user value to pass to handler
  97. */
  98. void BPending_Init (BPending *o, BPendingGroup *g, BPending_handler handler, void *user);
  99. /**
  100. * Frees the object.
  101. * The execution handler will not be called after the object
  102. * is freed.
  103. *
  104. * @param o the object
  105. */
  106. void BPending_Free (BPending *o);
  107. /**
  108. * Enables the job, pushing it to the top of the job list.
  109. * If the object was already in set state, the job is removed from its
  110. * current position in the list before being pushed.
  111. * The object enters set state.
  112. *
  113. * @param o the object
  114. */
  115. void BPending_Set (BPending *o);
  116. /**
  117. * Disables the job, removing it from the job list.
  118. * If the object was not in set state, nothing is done.
  119. * The object enters not set state.
  120. *
  121. * @param o the object
  122. */
  123. void BPending_Unset (BPending *o);
  124. /**
  125. * Checks if the job is in set state.
  126. *
  127. * @param o the object
  128. * @return 1 if in set state, 0 if not
  129. */
  130. int BPending_IsSet (BPending *o);
  131. #endif