ae.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* A simple event-driven programming library. Originally I wrote this code
  2. * for the Jim's event-loop (Jim is a Tcl interpreter) but later translated
  3. * it in form of a library for easy reuse.
  4. *
  5. * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * * Neither the name of Redis nor the names of its contributors may be used
  17. * to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #ifndef __AE_H__
  33. #define __AE_H__
  34. #include <time.h>
  35. #define AE_OK 0
  36. #define AE_ERR -1
  37. #define AE_NONE 0
  38. #define AE_READABLE 1
  39. #define AE_WRITABLE 2
  40. #define AE_FILE_EVENTS 1
  41. #define AE_TIME_EVENTS 2
  42. #define AE_ALL_EVENTS (AE_FILE_EVENTS | AE_TIME_EVENTS)
  43. #define AE_DONT_WAIT 4
  44. #define AE_CALL_AFTER_SLEEP 8
  45. #define AE_NOMORE -1
  46. #define AE_DELETED_EVENT_ID -1
  47. /* Macros */
  48. #define AE_NOTUSED(V) ((void) V)
  49. #define zmalloc malloc
  50. #define zfree free
  51. #define zrealloc realloc
  52. struct aeEventLoop;
  53. /* Types and data structures */
  54. typedef void aeFileProc(
  55. struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);
  56. typedef int aeTimeProc(
  57. struct aeEventLoop *eventLoop, long long id, void *clientData);
  58. typedef void aeEventFinalizerProc(
  59. struct aeEventLoop *eventLoop, void *clientData);
  60. typedef void aeBeforeSleepProc(struct aeEventLoop *eventLoop);
  61. /* File event structure */
  62. typedef struct aeFileEvent {
  63. int mask; /* one of AE_(READABLE|WRITABLE) */
  64. aeFileProc *rfileProc;
  65. aeFileProc *wfileProc;
  66. void *clientData;
  67. } aeFileEvent;
  68. /* Time event structure */
  69. typedef struct aeTimeEvent {
  70. long long id; /* time event identifier. */
  71. long when_sec; /* seconds */
  72. long when_ms; /* milliseconds */
  73. aeTimeProc *timeProc;
  74. aeEventFinalizerProc *finalizerProc;
  75. void *clientData;
  76. struct aeTimeEvent *next;
  77. } aeTimeEvent;
  78. /* A fired event */
  79. typedef struct aeFiredEvent {
  80. int fd;
  81. int mask;
  82. } aeFiredEvent;
  83. /* State of an event based program */
  84. typedef struct aeEventLoop {
  85. int maxfd; /* highest file descriptor currently registered */
  86. int setsize; /* max number of file descriptors tracked */
  87. long long timeEventNextId;
  88. time_t lastTime; /* Used to detect system clock skew */
  89. aeFileEvent *events; /* Registered events */
  90. aeFiredEvent *fired; /* Fired events */
  91. aeTimeEvent *timeEventHead;
  92. int stop;
  93. void *apidata; /* This is used for polling API specific data */
  94. aeBeforeSleepProc *beforesleep;
  95. aeBeforeSleepProc *aftersleep;
  96. } aeEventLoop;
  97. /* Prototypes */
  98. CS_API aeEventLoop *aeCreateEventLoop(int setsize);
  99. CS_API void aeDeleteEventLoop(aeEventLoop *eventLoop);
  100. CS_API void aeStop(aeEventLoop *eventLoop);
  101. CS_API int aeCreateFileEvent(
  102. aeEventLoop *eventLoop,
  103. int fd,
  104. int mask,
  105. aeFileProc *proc,
  106. void *clientData);
  107. CS_API void aeDeleteFileEvent(
  108. aeEventLoop *eventLoop, int fd, int mask);
  109. CS_API int aeGetFileEvents(aeEventLoop *eventLoop, int fd);
  110. CS_API long long aeCreateTimeEvent(
  111. aeEventLoop *eventLoop,
  112. long long milliseconds,
  113. aeTimeProc *proc,
  114. void *clientData,
  115. aeEventFinalizerProc *finalizerProc);
  116. CS_API int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id);
  117. CS_API int aeProcessEvents(aeEventLoop *eventLoop, int flags);
  118. CS_API int aeWait(int fd, int mask, long long milliseconds);
  119. CS_API void aeMain(aeEventLoop *eventLoop);
  120. CS_API char *aeGetApiName(void);
  121. CS_API void aeSetBeforeSleepProc(
  122. aeEventLoop *eventLoop,
  123. aeBeforeSleepProc *beforesleep);
  124. CS_API void aeSetAfterSleepProc(
  125. aeEventLoop *eventLoop,
  126. aeBeforeSleepProc *aftersleep);
  127. CS_API int aeGetSetSize(aeEventLoop *eventLoop);
  128. CS_API int aeResizeSetSize(aeEventLoop *eventLoop, int setsize);
  129. #endif