Menu

Jioloop

targezed

For detailed examples please see tests/ directory in the code.

Brief overview:

  • Init jioloop:
jioloop_init();
  • Set up timeouts/timers/file descriptors/signals:
jioloop_signal_handle(SIGINT, JIOLOOP_SIG_HANDLE, sig_int, NULL);

struct timespec interval = {0, 500000000};
jioloop_timeout_add(JIOLOOP_TIMEOUT_INTERVAL, &interval, timeout_cbk, NULL);

time_t now = time(NULL);
jioloop_timer_add(now + 5, timer_cbk, NULL);

int fd;
fd = socket()...
jioloop_fd_add(fd, JIOLOOP_FD_READ, fd_cbk, NULL);
  • Launch the loop:
jioloop_loop();
  • Example SIGINT handler to shutdown the loop:
int
sig_int(int sig, void *data)
{
  jioloop_exit();
  return 0;
}

At this point jioloop_loop(); will return.

  • Example timeout handle:
    You can remove any timeout/timer from within your handler:
int
timeout_cbk(void *id, jioloop_timeout_type type, void *data)
{
  if (count++ > 1000)
    jioloop_timeout_remove(id);
  return 0;
}
  • Example file descriptor handle:
    You can add/remove the file descriptor from within your handler:
int
fd_read(int fd, jioloop_fd_what what, void *data)
{
  jioloop_fd_remove(fd, JIOLOOP_FD_READ, fd_read, data);
  jioloop_fd_add(fd, JIOLOOP_FD_WRITE, fd_write, data);
  return 0;
}