BEventLock.h 3.7 KB

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