Slight reorder and renaming to clarify, also update README.

This commit is contained in:
Joachim Nilsson committed 2013-07-20 22:48:52 +02:00
1 parent 7e87644bd1
commit ab8c70d501
4 files changed
+86 -85

No files matched your search

+3 -3
View File
@@ -14,9 +14,9 @@ The C API
---------
The typical application will do the following steps
1. Create the event context with lueCtxtCreate()
2. Register initial callbacks with lueAddInput, lueAddOutput and lueAddTimer
3. Enter the event loop
1. Create the event context with uev_ctx_create()
2. Register callbacks with uev_io_create() and uev_timer_create()
3. Enter the event loop with uev_run()
Building
--------
+74 -73
View File
@@ -161,71 +161,6 @@ int uev_timer_delete(uev_t *ctx, uev_timer_t *w)
return 0;
}
/**
* Create an application context
*/
uev_t *uev_create(void)
{
uev_t *ctx;
ctx = (uev_t *)malloc(sizeof(*ctx));
if (!ctx)
return NULL;
lListInit(&ctx->io_list);
lListInit(&ctx->timer_list);
lListInit(&ctx->io_delist);
lListInit(&ctx->timer_delist);
ctx->base_time = times(NULL);
ctx->next_time = 0;
ctx->use_next = false;
ctx->exiting = false;
if (0 == clock_tick)
clock_tick = sysconf(_SC_CLK_TCK);
return ctx;
}
/**
* Destroy an application context
*/
void uev_delete(uev_t *ctx)
{
lListPurge(&ctx->io_list);
lListPurge(&ctx->timer_list);
free(ctx);
}
/**
* Drive the timeouts ourself (from now on until an increment of -1)
* This is intended for use in testing frameworks
* Caveat emptor: Currently, interactions with the scheduler (agent) API is undefined.
* Not for use in uevs that use the agent scheduler
* @param ctx UEVlication context object
* @param msec Ms to advance. Use -1 to return to realtime
*/
void uev_run_tick(uev_t *ctx, int msec)
{
// Obviously we are executing a callback right now (arent we always)
// The time leap is taken into account before falling back into poll() below
if (msec >= 0) {
if (!ctx->use_next) {
ctx->use_next = true;
ctx->next_time = times(NULL) + msec;
} else {
ctx->next_time += msec;
}
} else if (msec == -1) {
/* Go back to realtime */
ctx->use_next = false;
ctx->base_time += times(NULL) - ctx->next_time;
ctx->next_time = 0;
}
}
/**
* Process pending events
*/
@@ -319,14 +254,6 @@ static int do_run_pending(uev_t *ctx, int immediate)
return timeout;
}
/**
* Process pending events
*/
int uev_run_pending(uev_t *ctx)
{
return do_run_pending(ctx, 1);
}
/**
* Run the application
*/
@@ -343,6 +270,41 @@ void uev_run(uev_t *ctx)
ctx->exiting = false;
}
/**
* Drive the timeouts ourself (from now on until an increment of -1)
* This is intended for use in testing frameworks
* Caveat emptor: Currently, interactions with the scheduler (agent) API is undefined.
* Not for use in uevs that use the agent scheduler
* @param ctx UEVlication context object
* @param msec Ms to advance. Use -1 to return to realtime
*/
void uev_run_tick(uev_t *ctx, int msec)
{
// Obviously we are executing a callback right now (arent we always)
// The time leap is taken into account before falling back into poll() below
if (msec >= 0) {
if (!ctx->use_next) {
ctx->use_next = true;
ctx->next_time = times(NULL) + msec;
} else {
ctx->next_time += msec;
}
} else if (msec == -1) {
/* Go back to realtime */
ctx->use_next = false;
ctx->base_time += times(NULL) - ctx->next_time;
ctx->next_time = 0;
}
}
/**
* Process pending events
*/
int uev_run_pending(uev_t *ctx)
{
return do_run_pending(ctx, 1);
}
/**
* Terminate the application
*/
@@ -351,6 +313,45 @@ void uev_exit(uev_t *ctx)
ctx->exiting = true;
}
/**
* Create an application context
*/
uev_t *uev_ctx_create(void)
{
uev_t *ctx;
ctx = (uev_t *)malloc(sizeof(*ctx));
if (!ctx)
return NULL;
lListInit(&ctx->io_list);
lListInit(&ctx->timer_list);
lListInit(&ctx->io_delist);
lListInit(&ctx->timer_delist);
ctx->base_time = times(NULL);
ctx->next_time = 0;
ctx->use_next = false;
ctx->exiting = false;
if (0 == clock_tick)
clock_tick = sysconf(_SC_CLK_TCK);
return ctx;
}
/**
* Destroy an application context
*/
void uev_ctx_delete(uev_t *ctx)
{
lListPurge(&ctx->io_list);
lListPurge(&ctx->timer_list);
free(ctx);
}
/**
* Local Variables:
* version-control: t
+7 -7
View File
@@ -28,6 +28,7 @@
#include "llist.h"
/* Forward declarations due to dependencys, don't try this at home. */
struct uev;
struct uev_io;
struct uev_timer;
@@ -38,7 +39,7 @@ typedef enum {
UEV_FHOUT = 1
} uev_dir_t;
/* I/O event type */
/* I/O event watcher */
typedef struct uev_io_rem {
LListField(struct uev_io_rem);
struct uev_io *ent;
@@ -56,7 +57,7 @@ typedef struct uev_io {
void *data; ///< Callback optional parameter
} uev_io_t;
/* Timer event type */
/* Timer event watcher */
typedef struct uev_timer {
LListField(struct uev_timer); ///< Linked list elements
@@ -102,15 +103,14 @@ int uev_io_delete (uev_t *ctx, uev_io_t *io);
uev_timer_t *uev_timer_create (uev_t *ctx, int msec, uev_timer_cb_t handler, void *data);
int uev_timer_delete (uev_t *ctx, uev_timer_t *hdl);
uev_t *uev_create (void);
void uev_delete (uev_t *uev);
void uev_run (uev_t *ctx);
void uev_run_tick (uev_t *ctx, int msec);
int uev_run_pending (uev_t *ctx);
void uev_run (uev_t *ctx);
void uev_exit (uev_t *ctx);
uev_t *uev_ctx_create (void);
void uev_ctx_delete (uev_t *uev);
#endif /* LIBUEV_H_ */
/**
+2 -2
View File
@@ -89,7 +89,7 @@ static int pipe_write_cb(uev_t *ctx, uev_timer_t *handle, void *data)
int main(void)
{
int fd[2];
uev_t *ctx = uev_create();
uev_t *ctx = uev_ctx_create();
/* Total program execution time */
uev_timer_create(ctx, 4000, lifetime_cb, (void *)(intptr_t)2);
@@ -111,7 +111,7 @@ int main(void)
uev_run(ctx);
/* Tear down event context */
uev_delete(ctx);
uev_ctx_delete(ctx);
fprintf(stderr, "Period is %d must be 10: %s\n", period, 10 == period ? "OK" : "ERROR!");
if (10 != period)