Rename uev_t --> uev_ctx_t and uev_watcher_t --> uev_t

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2013-08-06 02:00:24 +02:00
1 parent 689b407c8f
commit c69ce87eb8
6 files changed
+61 -61

No files matched your search

+2 -2
View File
@@ -36,7 +36,7 @@
* *
* @return The new I/O watcher, or %NULL if invalid pointers or out or memory. * @return The new I/O watcher, or %NULL if invalid pointers or out or memory.
*/ */
uev_watcher_t *uev_io_create(uev_t *ctx, uev_cb_t *handler, void *data, int fd, uev_dir_t dir) uev_t *uev_io_create(uev_ctx_t *ctx, uev_cb_t *handler, void *data, int fd, uev_dir_t dir)
{ {
return uev_watcher_create(ctx, UEV_FILE_TYPE, fd, dir, handler, data); return uev_watcher_create(ctx, UEV_FILE_TYPE, fd, dir, handler, data);
} }
@@ -48,7 +48,7 @@ uev_watcher_t *uev_io_create(uev_t *ctx, uev_cb_t *handler, void *data, int fd,
* *
* @return POSIX OK(0) or non-zero with @param errno set. * @return POSIX OK(0) or non-zero with @param errno set.
*/ */
int uev_io_delete(uev_t *ctx, uev_watcher_t *w) int uev_io_delete(uev_ctx_t *ctx, uev_t *w)
{ {
return uev_watcher_delete(ctx, w); return uev_watcher_delete(ctx, w);
} }
+14 -14
View File
@@ -32,9 +32,9 @@
#include "uev.h" #include "uev.h"
/* Private to libuev, do not use directly! */ /* Private to libuev, do not use directly! */
uev_watcher_t *uev_watcher_create(uev_t *ctx, uev_type_t type, int fd, uev_dir_t dir, uev_cb_t *cb, void *arg) uev_t *uev_watcher_create(uev_ctx_t *ctx, uev_type_t type, int fd, uev_dir_t dir, uev_cb_t *cb, void *arg)
{ {
uev_watcher_t *w; uev_t *w;
struct epoll_event ev; struct epoll_event ev;
if (!ctx || !cb) { if (!ctx || !cb) {
@@ -42,7 +42,7 @@ uev_watcher_t *uev_watcher_create(uev_t *ctx, uev_type_t type, int fd, uev_dir_t
return NULL; return NULL;
} }
w = (uev_watcher_t *)calloc(1, sizeof(*w)); w = (uev_t *)calloc(1, sizeof(*w));
if (!w) if (!w)
return NULL; return NULL;
@@ -64,7 +64,7 @@ uev_watcher_t *uev_watcher_create(uev_t *ctx, uev_type_t type, int fd, uev_dir_t
} }
/* Private to libuev, do not use directly! */ /* Private to libuev, do not use directly! */
int uev_watcher_delete(uev_t *ctx, uev_watcher_t *w) int uev_watcher_delete(uev_ctx_t *ctx, uev_t *w)
{ {
if (!ctx || !w) { if (!ctx || !w) {
errno = EINVAL; errno = EINVAL;
@@ -84,18 +84,18 @@ int uev_watcher_delete(uev_t *ctx, uev_watcher_t *w)
/** /**
* Create an event loop context * Create an event loop context
* *
* @return Returns a new uev_t context, or %NULL on error. * @return Returns a new uev_ctx_t context, or %NULL on error.
*/ */
uev_t *uev_ctx_create(void) uev_ctx_t *uev_ctx_create(void)
{ {
int fd; int fd;
uev_t *ctx; uev_ctx_t *ctx;
fd = epoll_create(1); fd = epoll_create(1);
if (fd < 0) if (fd < 0)
return NULL; return NULL;
ctx = (uev_t *)calloc(1, sizeof(*ctx)); ctx = (uev_ctx_t *)calloc(1, sizeof(*ctx));
if (!ctx) { if (!ctx) {
close(fd); close(fd);
return NULL; return NULL;
@@ -111,10 +111,10 @@ uev_t *uev_ctx_create(void)
* Destroy an event loop context * Destroy an event loop context
* @param ctx A valid libuev context * @param ctx A valid libuev context
*/ */
void uev_ctx_delete(uev_t *ctx) void uev_ctx_delete(uev_ctx_t *ctx)
{ {
while (!LIST_EMPTY(&ctx->watchers)) { while (!LIST_EMPTY(&ctx->watchers)) {
uev_watcher_t *w = LIST_FIRST(&ctx->watchers); uev_t *w = LIST_FIRST(&ctx->watchers);
if (UEV_TIMER_TYPE == w->type) if (UEV_TIMER_TYPE == w->type)
uev_timer_delete(ctx, w); uev_timer_delete(ctx, w);
@@ -132,10 +132,10 @@ void uev_ctx_delete(uev_t *ctx)
* *
* @return POSIX OK(0) upon successful termination of the event loop, or non-zero on error. * @return POSIX OK(0) upon successful termination of the event loop, or non-zero on error.
*/ */
int uev_run(uev_t *ctx) int uev_run(uev_ctx_t *ctx)
{ {
int result = 0; int result = 0;
uev_watcher_t *w; uev_t *w;
if (!ctx) { if (!ctx) {
errno = EINVAL; errno = EINVAL;
@@ -165,7 +165,7 @@ int uev_run(uev_t *ctx)
} }
for (i = 0; i < nfds; i++) { for (i = 0; i < nfds; i++) {
w = (uev_watcher_t *)events[i].data.ptr; w = (uev_t *)events[i].data.ptr;
if (UEV_TIMER_TYPE == w->type) { if (UEV_TIMER_TYPE == w->type) {
uint64_t exp; uint64_t exp;
@@ -201,7 +201,7 @@ int uev_run(uev_t *ctx)
* Terminate the event loop * Terminate the event loop
* @param ctx A valid libuev context * @param ctx A valid libuev context
*/ */
void uev_exit(uev_t *ctx) void uev_exit(uev_ctx_t *ctx)
{ {
ctx->running = 0; ctx->running = 0;
} }
+4 -4
View File
@@ -40,11 +40,11 @@
* *
* @return The new watcher, or %NULL with @param errno set on error. * @return The new watcher, or %NULL with @param errno set on error.
*/ */
uev_watcher_t *uev_signal_create(uev_t *ctx, uev_cb_t *handler, void *data, int signo) uev_t *uev_signal_create(uev_ctx_t *ctx, uev_cb_t *handler, void *data, int signo)
{ {
int fd; int fd;
sigset_t mask; sigset_t mask;
uev_watcher_t *w; uev_t *w;
sigemptyset(&mask); sigemptyset(&mask);
fd = signalfd(-1, &mask, SFD_NONBLOCK); fd = signalfd(-1, &mask, SFD_NONBLOCK);
@@ -73,7 +73,7 @@ uev_watcher_t *uev_signal_create(uev_t *ctx, uev_cb_t *handler, void *data, int
* *
* @return POSIX OK(0) or non-zero with @param errno set. * @return POSIX OK(0) or non-zero with @param errno set.
*/ */
int uev_signal_set(uev_t *ctx, uev_watcher_t *w, int signo) int uev_signal_set(uev_ctx_t *ctx, uev_t *w, int signo)
{ {
sigset_t mask; sigset_t mask;
@@ -104,7 +104,7 @@ int uev_signal_set(uev_t *ctx, uev_watcher_t *w, int signo)
* *
* @return POSIX OK(0) or non-zero with @param errno set. * @return POSIX OK(0) or non-zero with @param errno set.
*/ */
int uev_signal_delete(uev_t *ctx, uev_watcher_t *w) int uev_signal_delete(uev_ctx_t *ctx, uev_t *w)
{ {
int fd; int fd;
+8 -8
View File
@@ -32,16 +32,16 @@
static int in, out; static int in, out;
static int period = 0; static int period = 0;
static uev_watcher_t *timer = NULL; static uev_t *timer = NULL;
static void lifetime_cb(uev_t *ctx, uev_watcher_t *w __attribute__ ((unused)), void *data) static void lifetime_cb(uev_ctx_t *ctx, uev_t *w __attribute__ ((unused)), void *data)
{ {
fprintf(stderr, "\nLifetime exceeded %p\n", data); fprintf(stderr, "\nLifetime exceeded %p\n", data);
uev_exit(ctx); uev_exit(ctx);
} }
/* The pipe watchdog, if it triggers we haven't received data in time. */ /* The pipe watchdog, if it triggers we haven't received data in time. */
static void timeout_cb(uev_t *ctx, uev_watcher_t *w, void *data) static void timeout_cb(uev_ctx_t *ctx, uev_t *w, void *data)
{ {
timer = NULL; timer = NULL;
fprintf(stderr, "\nTimeout exceeded %p\n", data); fprintf(stderr, "\nTimeout exceeded %p\n", data);
@@ -50,17 +50,17 @@ static void timeout_cb(uev_t *ctx, uev_watcher_t *w, void *data)
uev_exit(ctx); uev_exit(ctx);
} }
static void periodic_task(uev_t *ctx __attribute__ ((unused)), uev_watcher_t *w __attribute__ ((unused)), void *data __attribute__ ((unused))) static void periodic_task(uev_ctx_t *ctx __attribute__ ((unused)), uev_t *w __attribute__ ((unused)), void *data __attribute__ ((unused)))
{ {
fprintf(stderr, "|"); fprintf(stderr, "|");
} }
static void signal_cb(uev_t *ctx __attribute__ ((unused)), uev_watcher_t *w, void *data __attribute__ ((unused))) static void signal_cb(uev_ctx_t *ctx __attribute__ ((unused)), uev_t *w, void *data __attribute__ ((unused)))
{ {
fprintf(stderr, w->signo == SIGINT ? "^Cv" : "^\v"); fprintf(stderr, w->signo == SIGINT ? "^Cv" : "^\v");
} }
static void pipe_read_cb(uev_t *ctx, uev_watcher_t *w __attribute__ ((unused)), void *data __attribute__ ((unused))) static void pipe_read_cb(uev_ctx_t *ctx, uev_t *w __attribute__ ((unused)), void *data __attribute__ ((unused)))
{ {
int cnt; int cnt;
char msg[50]; char msg[50];
@@ -74,7 +74,7 @@ static void pipe_read_cb(uev_t *ctx, uev_watcher_t *w __attribute__ ((unused)),
fprintf(stderr, "%.*s.%d ", cnt, msg, cnt); fprintf(stderr, "%.*s.%d ", cnt, msg, cnt);
} }
static void pipe_write_cb(uev_t *ctx, uev_watcher_t *w __attribute__ ((unused)), void *data) static void pipe_write_cb(uev_ctx_t *ctx, uev_t *w __attribute__ ((unused)), void *data)
{ {
int cnt = (int)(intptr_t)data; int cnt = (int)(intptr_t)data;
char *msg = "TESTING"; char *msg = "TESTING";
@@ -94,7 +94,7 @@ static void pipe_write_cb(uev_t *ctx, uev_watcher_t *w __attribute__ ((unused)),
int main(void) int main(void)
{ {
int fd[2]; int fd[2];
uev_t *ctx = uev_ctx_create(); uev_ctx_t *ctx = uev_ctx_create();
/* Total program execution time */ /* Total program execution time */
uev_timer_create(ctx, lifetime_cb, (void *)(intptr_t)2, 4000, 0); uev_timer_create(ctx, lifetime_cb, (void *)(intptr_t)2, 4000, 0);
+4 -4
View File
@@ -58,10 +58,10 @@ static struct timespec msec2tspec(int msec)
* *
* @return The new watcher, or %NULL with @param errno set on error. * @return The new watcher, or %NULL with @param errno set on error.
*/ */
uev_watcher_t *uev_timer_create(uev_t *ctx, uev_cb_t *handler, void *data, int timeout, int period) uev_t *uev_timer_create(uev_ctx_t *ctx, uev_cb_t *handler, void *data, int timeout, int period)
{ {
int fd; int fd;
uev_watcher_t *w; uev_t *w;
fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
if (fd < 0) if (fd < 0)
@@ -90,7 +90,7 @@ uev_watcher_t *uev_timer_create(uev_t *ctx, uev_cb_t *handler, void *data, int t
* *
* @return POSIX OK(0) or non-zero with @param errno set on error. * @return POSIX OK(0) or non-zero with @param errno set on error.
*/ */
int uev_timer_set(uev_t *ctx, uev_watcher_t *w, int timeout, int period) int uev_timer_set(uev_ctx_t *ctx, uev_t *w, int timeout, int period)
{ {
struct itimerspec time; struct itimerspec time;
@@ -118,7 +118,7 @@ int uev_timer_set(uev_t *ctx, uev_watcher_t *w, int timeout, int period)
* *
* @return POSIX OK(0) or non-zero with @param errno set on error. * @return POSIX OK(0) or non-zero with @param errno set on error.
*/ */
int uev_timer_delete(uev_t *ctx, uev_watcher_t *w) int uev_timer_delete(uev_ctx_t *ctx, uev_t *w)
{ {
int fd; int fd;
+29 -29
View File
@@ -48,56 +48,56 @@ typedef enum {
} uev_type_t; } uev_type_t;
/* I/O event watcher */ /* I/O event watcher */
typedef struct uev_watcher { typedef struct uev {
LIST_ENTRY(uev_watcher) link; /* For queue.h linked list */ LIST_ENTRY(uev) link; /* For queue.h linked list */
/* Common to all watchers */ /* Common to all watchers */
uev_type_t type; uev_type_t type;
int fd; int fd;
/* Watcher callback with optional argument */ /* Watcher callback with optional argument */
void (*cb)(struct uev *, struct uev_watcher *, void *); void (*cb)(struct uev *, struct uev *, void *);
void *arg; void *arg;
/* Timer watchers, time in milliseconds */ /* Timer watchers, time in milliseconds */
int timeout; int timeout;
int period; int period;
/* Signal watchers */ /* Signal watchers */
int signo; int signo;
} uev_watcher_t; } uev_t;
/* Main libuev context type */ /* Main libuev context type */
typedef struct { typedef struct {
int running; int running;
int efd; /* For epoll() */ int efd; /* For epoll() */
LIST_HEAD(, uev_watcher) watchers; LIST_HEAD(, uev) watchers;
} uev_t; } uev_ctx_t;
/* Generic callback for watchers */ /* Generic callback for watchers */
typedef void (uev_cb_t) (uev_t *ctx, uev_watcher_t *w, void *data); typedef void (uev_cb_t) (uev_ctx_t *ctx, uev_t *w, void *data);
/* Private methods, do not use directly! */ /* Private methods, do not use directly! */
uev_watcher_t *uev_watcher_create(uev_t *ctx, uev_type_t type, int fd, uev_dir_t dir, uev_cb_t *cb, void *data); uev_t *uev_watcher_create(uev_ctx_t *ctx, uev_type_t type, int fd, uev_dir_t dir, uev_cb_t *cb, void *data);
int uev_watcher_delete(uev_t *ctx, uev_watcher_t *w); int uev_watcher_delete(uev_ctx_t *ctx, uev_t *w);
/* Public interface */ /* Public interface */
uev_watcher_t *uev_io_create (uev_t *ctx, uev_cb_t *cb, void *data, int fd, uev_dir_t dir); uev_t *uev_io_create (uev_ctx_t *ctx, uev_cb_t *cb, void *data, int fd, uev_dir_t dir);
int uev_io_delete (uev_t *ctx, uev_watcher_t *w); int uev_io_delete (uev_ctx_t *ctx, uev_t *w);
int uev_timer_set (uev_t *ctx, uev_watcher_t *w, int timeout, int period); int uev_timer_set (uev_ctx_t *ctx, uev_t *w, int timeout, int period);
uev_watcher_t *uev_timer_create (uev_t *ctx, uev_cb_t *cb, void *data, int timeout, int period); uev_t *uev_timer_create (uev_ctx_t *ctx, uev_cb_t *cb, void *data, int timeout, int period);
int uev_timer_delete (uev_t *ctx, uev_watcher_t *w); int uev_timer_delete (uev_ctx_t *ctx, uev_t *w);
uev_watcher_t *uev_signal_create (uev_t *ctx, uev_cb_t *handler, void *data, int signo); uev_t *uev_signal_create (uev_ctx_t *ctx, uev_cb_t *handler, void *data, int signo);
int uev_signal_set (uev_t *ctx, uev_watcher_t *w, int signo); int uev_signal_set (uev_ctx_t *ctx, uev_t *w, int signo);
int uev_signal_delete (uev_t *ctx, uev_watcher_t *w); int uev_signal_delete (uev_ctx_t *ctx, uev_t *w);
uev_t *uev_ctx_create (void); uev_ctx_t *uev_ctx_create (void);
void uev_ctx_delete (uev_t *uev); void uev_ctx_delete (uev_ctx_t *uev);
int uev_run (uev_t *ctx); int uev_run (uev_ctx_t *ctx);
void uev_exit (uev_t *ctx); void uev_exit (uev_ctx_t *ctx);
#endif /* LIBUEV_UEV_H_ */ #endif /* LIBUEV_UEV_H_ */