BEventLock.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @file BEventLock.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. * A FIFO lock for events using the job queue ({@link BPending}).
  25. */
  26. #ifndef BADVPN_SYSTEM_BEVENTLOCK_H
  27. #define BADVPN_SYSTEM_BEVENTLOCK_H
  28. #include <misc/debugcounter.h>
  29. #include <structure/LinkedList2.h>
  30. #include <system/DebugObject.h>
  31. #include <system/BPending.h>
  32. /**
  33. * Event context handler called when the lock job has acquired the lock
  34. * after requesting the lock with {@link BEventLockJob_Wait}.
  35. * The object was in waiting state.
  36. * The object enters locked state before the handler is called.
  37. *
  38. * @param user as in {@link BEventLockJob_Init}
  39. */
  40. typedef void (*BEventLock_handler) (void *user);
  41. /**
  42. * A FIFO lock for events using the job queue ({@link BPending}).
  43. */
  44. typedef struct {
  45. LinkedList2 jobs;
  46. BPending exec_job;
  47. DebugObject d_obj;
  48. DebugCounter pending_ctr;
  49. } BEventLock;
  50. /**
  51. * An object that can request a {@link BEventLock} lock.
  52. */
  53. typedef struct {
  54. BEventLock *l;
  55. BEventLock_handler handler;
  56. void *user;
  57. int pending;
  58. LinkedList2Node pending_node;
  59. DebugObject d_obj;
  60. } BEventLockJob;
  61. /**
  62. * Initializes the object.
  63. *
  64. * @param o the object
  65. * @param pg pending group
  66. */
  67. void BEventLock_Init (BEventLock *o, BPendingGroup *pg);
  68. /**
  69. * Frees the object.
  70. * There must be no {@link BEventLockJob} objects using this lock
  71. * (regardless of their state).
  72. *
  73. * @param o the object
  74. */
  75. void BEventLock_Free (BEventLock *o);
  76. /**
  77. * Initializes the object.
  78. * The object is initialized in idle state.
  79. *
  80. * @param o the object
  81. * @param l the lock
  82. * @param handler handler to call when the lock is aquired
  83. * @param user value to pass to handler
  84. */
  85. void BEventLockJob_Init (BEventLockJob *o, BEventLock *l, BEventLock_handler handler, void *user);
  86. /**
  87. * Frees the object.
  88. *
  89. * @param o the object
  90. */
  91. void BEventLockJob_Free (BEventLockJob *o);
  92. /**
  93. * Requests the lock.
  94. * The object must be in idle state.
  95. * The object enters waiting state.
  96. *
  97. * @param o the object
  98. */
  99. void BEventLockJob_Wait (BEventLockJob *o);
  100. /**
  101. * Aborts the lock request or releases the lock.
  102. * The object must be in waiting or locked state.
  103. * The object enters idle state.
  104. *
  105. * @param o the object
  106. */
  107. void BEventLockJob_Release (BEventLockJob *o);
  108. #endif