diff --git a/TODO.md b/TODO.md index 281aec4..a171034 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,5 @@ LibuEv TODO =========== -* Add uev_*_start(), at least for I/O, needed by finit * Make port to *BSD kqueue API, http://en.wikipedia.org/wiki/Kqueue diff --git a/io.c b/io.c index 7dfa15f..e0483c2 100644 --- a/io.c +++ b/io.c @@ -23,6 +23,8 @@ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include + #include "uev.h" @@ -39,12 +41,14 @@ */ int uev_io_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int fd, int events) { + if (fd < 0) { + errno = EINVAL; + return -1; + } + if (uev_watcher_init(ctx, w, UEV_IO_TYPE, cb, arg, fd, events)) return -1; - if (fd < 0) - return 0; - return uev_watcher_start(w); } @@ -67,15 +71,28 @@ int uev_io_set(uev_t *w, int fd, int events) return uev_io_init(w->ctx, w, (uev_cb_t *)w->cb, w->arg, fd, events); } +/** + * Start an I/O watcher + * @param w Watcher to start (again) + * + * @return POSIX OK(0) or non-zero with @param errno set on error. + */ +int uev_io_start(uev_t *w) +{ + return uev_io_set(w, w->fd, w->events); +} + /** * Stop an I/O watcher - * @param w Pointer to an uev_t watcher + * @param w Watcher to stop * * @return POSIX OK(0) or non-zero with @param errno set on error. */ int uev_io_stop(uev_t *w) { - return uev_watcher_stop(w); + int status = uev_watcher_stop(w); + + return status; } /** diff --git a/signal.c b/signal.c index a94660f..6dc0a8b 100644 --- a/signal.c +++ b/signal.c @@ -46,6 +46,12 @@ int uev_signal_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int signo int fd; sigset_t mask; + if (!w || !ctx) { + errno = EINVAL; + return -1; + } + w->fd = -1; + sigemptyset(&mask); fd = signalfd(-1, &mask, SFD_NONBLOCK); if (fd < 0) @@ -81,6 +87,9 @@ int uev_signal_set(uev_t *w, int signo) return -1; } + /* Remember for callbacks and start/stop */ + w->signo = signo; + /* Handle stopped signal watchers */ if (w->fd < 0) { /* Remove from internal list */ @@ -104,6 +113,26 @@ int uev_signal_set(uev_t *w, int signo) return uev_watcher_start(w); } + +/** + * Start a stopped signal watcher + * @param w Watcher to start (again) + * + * @return POSIX OK(0) or non-zero with @param errno set on error. + */ +int uev_signal_start(uev_t *w) +{ + if (!w) { + errno = EINVAL; + return -1; + } + + if (-1 != w->fd) + uev_signal_stop(w); + + return uev_signal_set(w, w->signo); +} + /** * Stop a signal watcher * @param w Watcher to stop diff --git a/timer.c b/timer.c index fdbba81..753862d 100644 --- a/timer.c +++ b/timer.c @@ -130,6 +130,25 @@ int uev_timer_set(uev_t *w, int timeout, int period) return 0; } +/** + * Start a stopped timer watcher + * @param w Watcher to start (again) + * + * @return POSIX OK(0) or non-zero with @param errno set on error. + */ +int uev_timer_start(uev_t *w) +{ + if (!w) { + errno = EINVAL; + return -1; + } + + if (-1 != w->fd) + uev_watcher_stop(w); + + return uev_timer_set(w, w->timeout, w->period); +} + /** * Stop and unregister a timer watcher * @param w Watcher to stop diff --git a/uev.c b/uev.c index 4b6f492..24696f4 100644 --- a/uev.c +++ b/uev.c @@ -231,8 +231,6 @@ int uev_run(uev_ctx_t *ctx, int flags) if (read(w->fd, &fdsi, sz) != sz) goto exit; - - w->signo = fdsi.ssi_signo; } if (w->cb) diff --git a/uev.h b/uev.h index 6ae5f79..f864e2d 100644 --- a/uev.h +++ b/uev.h @@ -98,14 +98,17 @@ int uev_run (uev_ctx_t *ctx, int flags); int uev_io_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int fd, int events); int uev_io_set (uev_t *w, int fd, int events); +int uev_io_start (uev_t *w); int uev_io_stop (uev_t *w); int uev_timer_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int timeout, int period); int uev_timer_set (uev_t *w, int timeout, int period); +int uev_timer_start (uev_t *w); int uev_timer_stop (uev_t *w); int uev_signal_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int signo); int uev_signal_set (uev_t *w, int signo); +int uev_signal_start (uev_t *w); int uev_signal_stop (uev_t *w); #endif /* LIBUEV_UEV_H_ */