Advertisement
troglobit

Proposed new libuev API

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