BReactor_glib.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @file BReactor_glib.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. #ifndef BADVPN_SYSTEM_BREACTOR_H
  30. #define BADVPN_SYSTEM_BREACTOR_H
  31. #include <stdint.h>
  32. #include <glib.h>
  33. #include <misc/debug.h>
  34. #include <misc/debugcounter.h>
  35. #include <misc/offset.h>
  36. #include <structure/LinkedList1.h>
  37. #include <base/DebugObject.h>
  38. #include <base/BPending.h>
  39. #include <system/BTime.h>
  40. typedef struct BReactor_s BReactor;
  41. struct BSmallTimer_t;
  42. #define BTIMER_SET_ABSOLUTE 1
  43. #define BTIMER_SET_RELATIVE 2
  44. typedef void (*BSmallTimer_handler) (struct BSmallTimer_t *timer);
  45. typedef void (*BTimer_handler) (void *user);
  46. typedef struct BSmallTimer_t {
  47. union {
  48. BSmallTimer_handler smalll; // MSVC doesn't like "small"
  49. BTimer_handler heavy;
  50. } handler;
  51. BReactor *reactor;
  52. GSource *source;
  53. uint8_t active;
  54. uint8_t is_small;
  55. } BSmallTimer;
  56. void BSmallTimer_Init (BSmallTimer *bt, BSmallTimer_handler handler);
  57. int BSmallTimer_IsRunning (BSmallTimer *bt);
  58. typedef struct {
  59. BSmallTimer base;
  60. void *user;
  61. btime_t msTime;
  62. } BTimer;
  63. void BTimer_Init (BTimer *bt, btime_t msTime, BTimer_handler handler, void *user);
  64. int BTimer_IsRunning (BTimer *bt);
  65. struct BFileDescriptor_t;
  66. #define BREACTOR_READ (1 << 0)
  67. #define BREACTOR_WRITE (1 << 1)
  68. #define BREACTOR_ERROR (1 << 2)
  69. #define BREACTOR_HUP (1 << 3)
  70. typedef void (*BFileDescriptor_handler) (void *user, int events);
  71. typedef struct BFileDescriptor_t {
  72. int fd;
  73. BFileDescriptor_handler handler;
  74. void *user;
  75. int active;
  76. int waitEvents;
  77. BReactor *reactor;
  78. GSource *source;
  79. GPollFD pollfd;
  80. } BFileDescriptor;
  81. void BFileDescriptor_Init (BFileDescriptor *bs, int fd, BFileDescriptor_handler handler, void *user);
  82. struct BReactor_s {
  83. int exiting;
  84. int exit_code;
  85. GMainLoop *gloop;
  86. int unref_gloop_on_free;
  87. GSourceFuncs fd_source_funcs;
  88. BPendingGroup pending_jobs;
  89. LinkedList1 active_limits_list;
  90. DebugCounter d_fds_counter;
  91. DebugCounter d_limits_ctr;
  92. DebugCounter d_timers_ctr;
  93. DebugObject d_obj;
  94. };
  95. int BReactor_Init (BReactor *bsys) WARN_UNUSED;
  96. void BReactor_Free (BReactor *bsys);
  97. int BReactor_Exec (BReactor *bsys);
  98. void BReactor_Quit (BReactor *bsys, int code);
  99. void BReactor_SetSmallTimer (BReactor *bsys, BSmallTimer *bt, int mode, btime_t time);
  100. void BReactor_RemoveSmallTimer (BReactor *bsys, BSmallTimer *bt);
  101. void BReactor_SetTimer (BReactor *bsys, BTimer *bt);
  102. void BReactor_SetTimerAfter (BReactor *bsys, BTimer *bt, btime_t after);
  103. void BReactor_SetTimerAbsolute (BReactor *bsys, BTimer *bt, btime_t time);
  104. void BReactor_RemoveTimer (BReactor *bsys, BTimer *bt);
  105. BPendingGroup * BReactor_PendingGroup (BReactor *bsys);
  106. int BReactor_Synchronize (BReactor *bsys, BSmallPending *ref);
  107. int BReactor_AddFileDescriptor (BReactor *bsys, BFileDescriptor *bs) WARN_UNUSED;
  108. void BReactor_RemoveFileDescriptor (BReactor *bsys, BFileDescriptor *bs);
  109. void BReactor_SetFileDescriptorEvents (BReactor *bsys, BFileDescriptor *bs, int events);
  110. int BReactor_InitFromExistingGMainLoop (BReactor *bsys, GMainLoop *gloop, int unref_gloop_on_free);
  111. GMainLoop * BReactor_GetGMainLoop (BReactor *bsys);
  112. int BReactor_SynchronizeAll (BReactor *bsys);
  113. typedef struct {
  114. BReactor *reactor;
  115. int limit;
  116. int count;
  117. LinkedList1Node active_limits_list_node;
  118. DebugObject d_obj;
  119. } BReactorLimit;
  120. void BReactorLimit_Init (BReactorLimit *o, BReactor *reactor, int limit);
  121. void BReactorLimit_Free (BReactorLimit *o);
  122. int BReactorLimit_Increment (BReactorLimit *o);
  123. void BReactorLimit_SetLimit (BReactorLimit *o, int limit);
  124. #endif