#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
+#include <stddef.h>
#include <stdio.h>
static void
event (W w, int events)
{
- w->pending = ++pendingcnt;
- array_needsize (pendings, pendingmax, pendingcnt, );
- pendings [pendingcnt - 1].w = w;
- pendings [pendingcnt - 1].events = events;
+ if (w->active)
+ {
+ w->pending = ++pendingcnt;
+ array_needsize (pendings, pendingmax, pendingcnt, );
+ pendings [pendingcnt - 1].w = w;
+ pendings [pendingcnt - 1].events = events;
+ }
}
static void
{
struct ev_timer *w = timers [0];
+ event ((W)w, EV_TIMEOUT);
+
/* first reschedule or stop timer */
if (w->repeat)
{
}
else
evtimer_stop (w); /* nonrepeating: stop timer */
-
- event ((W)w, EV_TIMEOUT);
}
}
}
}
+static void
+ev_clear (W w)
+{
+ if (w->pending)
+ {
+ pendings [w->pending - 1].w = 0;
+ w->pending = 0;
+ }
+}
+
static void
ev_start (W w, int active)
{
- w->pending = 0;
w->active = active;
}
static void
ev_stop (W w)
{
- if (w->pending)
- pendings [w->pending - 1].w = 0;
-
w->active = 0;
}
void
evio_stop (struct ev_io *w)
{
+ ev_clear ((W)w);
if (!ev_is_active (w))
return;
fdchanges [fdchangecnt - 1] = w->fd;
}
-
void
evtimer_start (struct ev_timer *w)
{
void
evtimer_stop (struct ev_timer *w)
{
+ ev_clear ((W)w);
if (!ev_is_active (w))
return;
void
evperiodic_stop (struct ev_periodic *w)
{
+ ev_clear ((W)w);
if (!ev_is_active (w))
return;
void
evsignal_stop (struct ev_signal *w)
{
+ ev_clear ((W)w);
if (!ev_is_active (w))
return;
void evidle_stop (struct ev_idle *w)
{
+ ev_clear ((W)w);
+ if (ev_is_active (w))
+ return;
+
idles [w->active - 1] = idles [--idlecnt];
ev_stop ((W)w);
}
void evcheck_stop (struct ev_check *w)
{
+ ev_clear ((W)w);
+ if (ev_is_active (w))
+ return;
+
checks [w->active - 1] = checks [--checkcnt];
ev_stop ((W)w);
}
/*****************************************************************************/
+struct ev_once
+{
+ struct ev_io io;
+ struct ev_timer to;
+ void (*cb)(int revents, void *arg);
+ void *arg;
+};
+
+static void
+once_cb (struct ev_once *once, int revents)
+{
+ void (*cb)(int revents, void *arg) = once->cb;
+ void *arg = once->arg;
+
+ evio_stop (&once->io);
+ evtimer_stop (&once->to);
+ free (once);
+
+ cb (revents, arg);
+}
+
+static void
+once_cb_io (struct ev_io *w, int revents)
+{
+ once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
+}
+
+static void
+once_cb_to (struct ev_timer *w, int revents)
+{
+ once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
+}
+
+void
+ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
+{
+ struct ev_once *once = malloc (sizeof (struct ev_once));
+
+ if (!once)
+ cb (EV_ERROR, arg);
+ else
+ {
+ once->cb = cb;
+ once->arg = arg;
+
+ evw_init (&once->io, once_cb_io);
+
+ if (fd >= 0)
+ {
+ evio_set (&once->io, fd, events);
+ evio_start (&once->io);
+ }
+
+ evw_init (&once->to, once_cb_to);
+
+ if (timeout >= 0.)
+ {
+ evtimer_set (&once->to, timeout, 0.);
+ evtimer_start (&once->to);
+ }
+ }
+}
+
+/*****************************************************************************/
+
#if 0
struct ev_io wio;
#define EV_SIGNAL 0x08
#define EV_IDLE 0x10
#define EV_CHECK 0x20
+#define EV_ERROR (0x3f|0x80)
/* can be used to add custom fields to all watchers */
#ifndef EV_COMMON
# define EV_COMMON void *data
#endif
+#ifndef EV_PROTOTYPES
+# define EV_PROTOTYPES 1
+#endif
/*
* struct member types:
#define EVMETHOD_NONE 0
#define EVMETHOD_SELECT 1
#define EVMETHOD_EPOLL 2
-int ev_init (int flags); /* returns ev_method */
+#if EV_PROTOTYPES
extern int ev_method;
+int ev_init (int flags); /* returns ev_method */
/* these three calls are suitable for plugging into pthread_atfork */
void ev_prefork (void);
extern ev_tstamp ev_now; /* time w.r.t. timers and the eventloop, updated after each poll */
ev_tstamp ev_time (void);
+#endif
#define EVLOOP_NONBLOCK 1 /* do not block/wait */
#define EVLOOP_ONESHOT 2 /* block *once* only */
+#if EV_PROTOTYPES
void ev_loop (int flags);
extern int ev_loop_done; /* set to 1 to break out of event loop, set to 2 to break out of all event loops */
+/* convinience function, wait for a single event, without registering an event watcher */
+/* if timeout is < 0, do wait indefinitely */
+void ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg);
+#endif
+
/* these may evaluate ev multiple times, and the other arguments at most once */
/* either use evw_init + evXXX_set, or the evXXX_init macro, below, to first initialise a watcher */
-#define evw_init(ev,cb_) do { (ev)->active = 0; (ev)->cb = (cb_); } while (0)
+#define evw_init(ev,cb_) do { (ev)->active = 0; (ev)->pending = 0; (ev)->cb = (cb_); } while (0)
#define evio_set(ev,fd_,events_) do { (ev)->fd = (fd_); (ev)->events = (events_); } while (0)
#define evtimer_set(ev,after_,repeat_) do { (ev)->at = (after_); (ev)->repeat = (repeat_); } while (0)
#define evperiodic_set(ev,at_,interval_) do { (ev)->at = (at_); (ev)->interval = (interval_); } while (0)
#define evsignal_set(ev,signum_) do { (ev)->signum = (signum_); } while (0)
-#define evcheck_set(ev) /* nop, yes this is a serious in-joke */
-#define evidle_set(ev) /* nop, yes this is a serious in-joke */
+#define evcheck_set(ev) /* nop, yes, this is a serious in-joke */
+#define evidle_set(ev) /* nop, yes, this is a serious in-joke */
#define evio_init(ev,cb,fd,events) do { evw_init ((ev), (cb)); evio_set ((ev),(fd),(events)); } while (0)
#define evtimer_init(ev,cb,after,repeat) do { evw_init ((ev), (cb)); evtimer_set ((ev),(after),(repeat)); } while (0)
/* stopping (enabling, adding) a watcher does nothing if it is already running */
/* stopping (disabling, deleting) a watcher does nothing unless its already running */
+#if EV_PROTOTYPES
void evio_start (struct ev_io *w);
void evio_stop (struct ev_io *w);
void evcheck_start (struct ev_check *w);
void evcheck_stop (struct ev_check *w);
+#endif
#endif