loop.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include "hloop.h"
  2. #include "hbase.h"
  3. void on_idle(hidle_t* idle) {
  4. printf("on_idle: event_id=%lu\tpriority=%d\tuserdata=%ld\n", idle->event_id, idle->priority, (long)idle->userdata);
  5. }
  6. void on_timer(htimer_t* timer) {
  7. printf("on_timer: event_id=%lu\tpriority=%d\tuserdata=%ld\ttime=%lus\thrtime=%luus\n",
  8. timer->event_id, timer->priority, (long)timer->userdata, hloop_now(timer->loop), timer->loop->cur_hrtime);
  9. }
  10. void cron_hourly(htimer_t* timer) {
  11. time_t tt;
  12. time(&tt);
  13. printf("cron_hourly: %s\n", ctime(&tt));
  14. }
  15. int main() {
  16. MEMCHECK;
  17. hloop_t loop;
  18. hloop_init(&loop);
  19. for (int i = HEVENT_LOWEST_PRIORITY; i <= HEVENT_HIGHEST_PRIORITY; ++i) {
  20. hidle_t* idle = hidle_add(&loop, on_idle, 10);
  21. idle->priority = i;
  22. }
  23. for (int i = 1; i <= 10; ++i) {
  24. htimer_t* timer = htimer_add(&loop, on_timer, i*1000, 3);
  25. timer->userdata = (void*)i;
  26. }
  27. int minute = time(NULL)%3600/60;
  28. htimer_add_period(&loop, cron_hourly, minute+1, -1, -1, -1, -1, 1);
  29. hloop_run(&loop);
  30. return 0;
  31. }