BReactor_glib.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <structure/LinkedList1.h>
  36. #include <base/DebugObject.h>
  37. #include <base/BPending.h>
  38. #include <system/BTime.h>
  39. typedef struct BReactor_s BReactor;
  40. typedef void (*BTimer_handler) (void *user);
  41. typedef struct BTimer_t {
  42. btime_t msTime;
  43. BTimer_handler handler;
  44. void *handler_pointer;
  45. uint8_t active;
  46. BReactor *reactor;
  47. GSource *source;
  48. } BTimer;
  49. void BTimer_Init (BTimer *bt, btime_t msTime, BTimer_handler handler, void *user);
  50. int BTimer_IsRunning (BTimer *bt);
  51. struct BFileDescriptor_t;
  52. #define BREACTOR_READ (1 << 0)
  53. #define BREACTOR_WRITE (1 << 1)
  54. #define BREACTOR_ERROR (1 << 2)
  55. typedef void (*BFileDescriptor_handler) (void *user, int events);
  56. typedef struct BFileDescriptor_t {
  57. int fd;
  58. BFileDescriptor_handler handler;
  59. void *user;
  60. int active;
  61. int waitEvents;
  62. BReactor *reactor;
  63. GSource *source;
  64. GPollFD pollfd;
  65. } BFileDescriptor;
  66. void BFileDescriptor_Init (BFileDescriptor *bs, int fd, BFileDescriptor_handler handler, void *user);
  67. struct BReactor_s {
  68. int exiting;
  69. int exit_code;
  70. GMainLoop *gloop;
  71. int unref_gloop_on_free;
  72. GSourceFuncs fd_source_funcs;
  73. BPendingGroup pending_jobs;
  74. LinkedList1 active_limits_list;
  75. DebugCounter d_fds_counter;
  76. DebugCounter d_limits_ctr;
  77. DebugCounter d_timers_ctr;
  78. DebugObject d_obj;
  79. };
  80. int BReactor_Init (BReactor *bsys) WARN_UNUSED;
  81. void BReactor_Free (BReactor *bsys);
  82. int BReactor_Exec (BReactor *bsys);
  83. void BReactor_Quit (BReactor *bsys, int code);
  84. void BReactor_SetTimer (BReactor *bsys, BTimer *bt);
  85. void BReactor_SetTimerAfter (BReactor *bsys, BTimer *bt, btime_t after);
  86. void BReactor_SetTimerAbsolute (BReactor *bsys, BTimer *bt, btime_t time);
  87. void BReactor_RemoveTimer (BReactor *bsys, BTimer *bt);
  88. BPendingGroup * BReactor_PendingGroup (BReactor *bsys);
  89. int BReactor_Synchronize (BReactor *bsys, BPending *ref);
  90. int BReactor_AddFileDescriptor (BReactor *bsys, BFileDescriptor *bs) WARN_UNUSED;
  91. void BReactor_RemoveFileDescriptor (BReactor *bsys, BFileDescriptor *bs);
  92. void BReactor_SetFileDescriptorEvents (BReactor *bsys, BFileDescriptor *bs, int events);
  93. int BReactor_InitFromExistingGMainLoop (BReactor *bsys, GMainLoop *gloop, int unref_gloop_on_free);
  94. GMainLoop * BReactor_GetGMainLoop (BReactor *bsys);
  95. int BReactor_SynchronizeAll (BReactor *bsys);
  96. typedef struct {
  97. BReactor *reactor;
  98. int limit;
  99. int count;
  100. LinkedList1Node active_limits_list_node;
  101. DebugObject d_obj;
  102. } BReactorLimit;
  103. void BReactorLimit_Init (BReactorLimit *o, BReactor *reactor, int limit);
  104. void BReactorLimit_Free (BReactorLimit *o);
  105. int BReactorLimit_Increment (BReactorLimit *o);
  106. void BReactorLimit_SetLimit (BReactorLimit *o, int limit);
  107. #endif