BPending.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 queue 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 a job.
  82. * There must be at least one job in the queue.
  83. *
  84. * @param g the object
  85. */
  86. void BPendingGroup_ExecuteJob (BPendingGroup *g);
  87. /**
  88. * Initializes the object.
  89. * The object is initialized in not set state.
  90. *
  91. * @param o the object
  92. * @param g pending group to use
  93. * @param handler job execution handler
  94. * @param user value to pass to handler
  95. */
  96. void BPending_Init (BPending *o, BPendingGroup *g, BPending_handler handler, void *user);
  97. /**
  98. * Frees the object.
  99. * The execution handler will not be called after the object
  100. * is freed.
  101. *
  102. * @param o the object
  103. */
  104. void BPending_Free (BPending *o);
  105. /**
  106. * Enables the job, appending it to the end of the group's queue.
  107. * If the object was already in set state, the job is removed from its
  108. * current position in the queue before being appended.
  109. * The object enters set state.
  110. *
  111. * @param o the object
  112. */
  113. void BPending_Set (BPending *o);
  114. /**
  115. * Disables the job, removing it from the group's queue.
  116. * If the object was not in set state, nothing is done.
  117. * The object enters not set state.
  118. *
  119. * @param o the object
  120. */
  121. void BPending_Unset (BPending *o);
  122. #endif