Advertisement
troglobit

Proposed new libuev API

Jul 26th, 2013
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 KB | None | 0 0
  1. /* Improved and simplified libuev API proposal
  2.  */
  3.         uev_ctx_init()
  4.     uev_run()         /* Start a ctx w/ registered watchers */
  5.         uev_exit()        /* Stop a ctx and free all kernel resources, close descriptors, etc. */
  6.  
  7.         uev_timer_init()  /* Initialize a uev_t timer watcher with callback and optional arg. */
  8.         uev_timer_set()   /* Re-set new timeout/period time   */
  9.         uev_timer_start() /* Actually allocate kernel resource and register with context/epoll */
  10.     uev_timer_stop()  /* Free kernel resource and deregister with the context/epoll */
  11.  
  12.         uev_io_init()     /* Initialize a uev_t I/O watcher */
  13.         uev_io_set()
  14.         uev_io_poll()     /* Like uev_timer_start() above */
  15.         uev_io_cancel()   /* Like uev_timer_stop() above */
  16.  
  17.         uev_signal_init() /* Initialize a uev_t signal watcher */
  18.         uev_signal_set()
  19.         uev_signal_wait()
  20.         uev_signal_cancel()
  21.  
  22. /****** EXAMPLE
  23.  ****** Nota bene, the below example is only pseudo code ... it's also a very stupid example.
  24.  ******/
  25.  
  26. int main()
  27. {
  28.     uev_t timer, file;
  29.     uev_ctx_t ctx;
  30.  
  31.         uev_ctx_init(&ctx);
  32.  
  33.         /* Create and connect event watchers to a given context */
  34.         uev_timer_init(&ctx, &timer, timer_cb, NULL, 1000, 1000);
  35.         uev_io_init(&ctx, &file,  read_cb, &timer);
  36.  
  37.         /* Here is where timers are actually started */
  38.         uev_timer_start(&timer);
  39.     uev_io_poll(&file);
  40.  
  41.     /* Enter event loop */
  42.         uev_run(&ctx);
  43. }
  44.  
  45. read_cb()
  46. {
  47.         uev_t *timer = (uev_t *)arg;
  48.  
  49.         /* Stop a timer */
  50.         uev_timer_stop(timer);
  51.  
  52.         /* Start timer with new timeout */
  53.         uev_timer_set(timer, 2000, 2000);
  54.  
  55.         /* Since the loop is already running, and the timer event already
  56.          * connected to a context, it will be started immediately.
  57.          */
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement