Mirror of :pserver:anonymous@cvs.schmorp.de/schmorpforge libev http://software.schmorp.de/pkg/libev.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

407 lines
8.7 KiB

14 years ago
14 years ago
  1. /*
  2. * libevent compatibility layer
  3. *
  4. * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer in the documentation and/or other materials provided
  17. * with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <stddef.h>
  32. #include <stdlib.h>
  33. #include <assert.h>
  34. #ifndef WIN32
  35. # include <sys/time.h>
  36. #endif
  37. #ifdef EV_EVENT_H
  38. # include EV_EVENT_H
  39. #else
  40. # include "event.h"
  41. #endif
  42. #if EV_MULTIPLICITY
  43. # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base
  44. # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base
  45. #else
  46. # define dLOOPev
  47. # define dLOOPbase
  48. #endif
  49. /* never accessed, will always be cast from/to ev_loop */
  50. struct event_base
  51. {
  52. int dummy;
  53. };
  54. static struct event_base *x_cur;
  55. static void
  56. tv_set (struct timeval *tv, ev_tstamp at)
  57. {
  58. tv->tv_sec = (long)at;
  59. tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
  60. }
  61. static ev_tstamp
  62. tv_get (struct timeval *tv)
  63. {
  64. if (tv)
  65. return tv->tv_sec + tv->tv_usec * 1e-6;
  66. else
  67. return -1.;
  68. }
  69. #define EVENT_VERSION(a,b) # a "." # b
  70. const char *event_get_version (void)
  71. {
  72. return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR);
  73. }
  74. const char *event_get_method (void)
  75. {
  76. return "libev";
  77. }
  78. void *event_init (void)
  79. {
  80. #if EV_MULTIPLICITY
  81. if (x_cur)
  82. x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO);
  83. else
  84. x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO);
  85. #else
  86. assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur));
  87. x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO);
  88. #endif
  89. return x_cur;
  90. }
  91. void event_base_free (struct event_base *base)
  92. {
  93. dLOOPbase;
  94. #if EV_MULTIPLICITY
  95. if (ev_default_loop (EVFLAG_AUTO) != loop)
  96. ev_loop_destroy (loop);
  97. #endif
  98. }
  99. int event_dispatch (void)
  100. {
  101. return event_base_dispatch (x_cur);
  102. }
  103. #ifdef EV_STANDALONE
  104. void event_set_log_callback (event_log_cb cb)
  105. {
  106. /* nop */
  107. }
  108. #endif
  109. int event_loop (int flags)
  110. {
  111. return event_base_loop (x_cur, flags);
  112. }
  113. int event_loopexit (struct timeval *tv)
  114. {
  115. return event_base_loopexit (x_cur, tv);
  116. }
  117. static void
  118. x_cb (struct event *ev, int revents)
  119. {
  120. revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL;
  121. ev->ev_res = revents;
  122. ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg);
  123. }
  124. static void
  125. x_cb_sig (EV_P_ struct ev_signal *w, int revents)
  126. {
  127. struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig));
  128. if (revents & EV_ERROR)
  129. event_del (ev);
  130. x_cb (ev, revents);
  131. }
  132. static void
  133. x_cb_io (EV_P_ struct ev_io *w, int revents)
  134. {
  135. struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
  136. if (revents & EV_ERROR)
  137. event_del (ev);
  138. else if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w))
  139. {
  140. ev_io_stop (EV_A_ w);
  141. ev->ev_flags &= ~EVLIST_INSERTED;
  142. }
  143. x_cb (ev, revents);
  144. }
  145. static void
  146. x_cb_to (EV_P_ struct ev_timer *w, int revents)
  147. {
  148. struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to));
  149. event_del (ev);
  150. x_cb (ev, revents);
  151. }
  152. void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
  153. {
  154. if (events & EV_SIGNAL)
  155. ev_init (&ev->iosig.sig, x_cb_sig);
  156. else
  157. ev_init (&ev->iosig.io, x_cb_io);
  158. ev_init (&ev->to, x_cb_to);
  159. ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */
  160. ev->ev_fd = fd;
  161. ev->ev_events = events;
  162. ev->ev_pri = 0;
  163. ev->ev_callback = cb;
  164. ev->ev_arg = arg;
  165. ev->ev_res = 0;
  166. ev->ev_flags = EVLIST_INIT;
  167. }
  168. int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
  169. {
  170. return event_base_once (x_cur, fd, events, cb, arg, tv);
  171. }
  172. int event_add (struct event *ev, struct timeval *tv)
  173. {
  174. dLOOPev;
  175. if (ev->ev_events & EV_SIGNAL)
  176. {
  177. if (!ev_is_active (&ev->iosig.sig))
  178. {
  179. ev_signal_set (&ev->iosig.sig, ev->ev_fd);
  180. ev_signal_start (EV_A_ &ev->iosig.sig);
  181. ev->ev_flags |= EVLIST_SIGNAL;
  182. }
  183. }
  184. else if (ev->ev_events & (EV_READ | EV_WRITE))
  185. {
  186. if (!ev_is_active (&ev->iosig.io))
  187. {
  188. ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
  189. ev_io_start (EV_A_ &ev->iosig.io);
  190. ev->ev_flags |= EVLIST_INSERTED;
  191. }
  192. }
  193. if (tv)
  194. {
  195. ev->to.repeat = tv_get (tv);
  196. ev_timer_again (EV_A_ &ev->to);
  197. ev->ev_flags |= EVLIST_TIMEOUT;
  198. }
  199. else
  200. if (ev_is_active (&ev->to))
  201. ev_timer_stop (EV_A_ &ev->to);
  202. ev->ev_flags |= EVLIST_ACTIVE;
  203. return 0;
  204. }
  205. int event_del (struct event *ev)
  206. {
  207. dLOOPev;
  208. if (ev->ev_events & EV_SIGNAL)
  209. {
  210. /* sig */
  211. if (ev_is_active (&ev->iosig.sig))
  212. ev_signal_stop (EV_A_ &ev->iosig.sig);
  213. }
  214. else if (ev->ev_events & (EV_READ | EV_WRITE))
  215. {
  216. /* io */
  217. if (ev_is_active (&ev->iosig.io))
  218. ev_io_stop (EV_A_ &ev->iosig.io);
  219. }
  220. if (ev_is_active (&ev->to))
  221. ev_timer_stop (EV_A_ &ev->to);
  222. ev->ev_flags = EVLIST_INIT;
  223. return 0;
  224. }
  225. void event_active (struct event *ev, int res, short ncalls)
  226. {
  227. dLOOPev;
  228. if (res & EV_TIMEOUT)
  229. ev_feed_event (EV_A_ &ev->to, res & EV_TIMEOUT);
  230. if (res & EV_SIGNAL)
  231. ev_feed_event (EV_A_ &ev->iosig.sig, res & EV_SIGNAL);
  232. if (res & (EV_READ | EV_WRITE))
  233. ev_feed_event (EV_A_ &ev->iosig.io, res & (EV_READ | EV_WRITE));
  234. }
  235. int event_pending (struct event *ev, short events, struct timeval *tv)
  236. {
  237. short revents = 0;
  238. dLOOPev;
  239. if (ev->ev_events & EV_SIGNAL)
  240. {
  241. /* sig */
  242. if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig))
  243. revents |= EV_SIGNAL;
  244. }
  245. else if (ev->ev_events & (EV_READ | EV_WRITE))
  246. {
  247. /* io */
  248. if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io))
  249. revents |= ev->ev_events & (EV_READ | EV_WRITE);
  250. }
  251. if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to))
  252. {
  253. revents |= EV_TIMEOUT;
  254. if (tv)
  255. tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */
  256. }
  257. return events & revents;
  258. }
  259. int event_priority_init (int npri)
  260. {
  261. return event_base_priority_init (x_cur, npri);
  262. }
  263. int event_priority_set (struct event *ev, int pri)
  264. {
  265. ev->ev_pri = pri;
  266. return 0;
  267. }
  268. int event_base_set (struct event_base *base, struct event *ev)
  269. {
  270. ev->ev_base = base;
  271. return 0;
  272. }
  273. int event_base_loop (struct event_base *base, int flags)
  274. {
  275. dLOOPbase;
  276. ev_loop (EV_A_ flags);
  277. return 0;
  278. }
  279. int event_base_dispatch (struct event_base *base)
  280. {
  281. return event_base_loop (base, 0);
  282. }
  283. static void
  284. x_loopexit_cb (int revents, void *base)
  285. {
  286. dLOOPbase;
  287. ev_unloop (EV_A_ EVUNLOOP_ONE);
  288. }
  289. int event_base_loopexit (struct event_base *base, struct timeval *tv)
  290. {
  291. ev_tstamp after = tv_get (tv);
  292. dLOOPbase;
  293. ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base);
  294. return -1;
  295. }
  296. struct x_once
  297. {
  298. int fd;
  299. void (*cb)(int, short, void *);
  300. void *arg;
  301. };
  302. static void
  303. x_once_cb (int revents, void *arg)
  304. {
  305. struct x_once *once = (struct x_once *)arg;
  306. once->cb (once->fd, (short)revents, once->arg);
  307. free (once);
  308. }
  309. int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
  310. {
  311. struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once));
  312. dLOOPbase;
  313. if (!once)
  314. return -1;
  315. once->fd = fd;
  316. once->cb = cb;
  317. once->arg = arg;
  318. ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once);
  319. return 0;
  320. }
  321. int event_base_priority_init (struct event_base *base, int npri)
  322. {
  323. /*dLOOPbase;*/
  324. return 0;
  325. }