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.
 
 
 
 
 
 

426 lines
9.7 KiB

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