btimer_example.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @file btimer_example.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <time.h>
  25. #include <system/BReactor.h>
  26. #include <system/BLog.h>
  27. #include <system/BTime.h>
  28. // gives average firing rate 100kHz
  29. #define TIMER_NUM 500
  30. #define TIMER_MODULO 10
  31. BReactor sys;
  32. void handle_timer (BTimer *bt)
  33. {
  34. #ifdef BADVPN_USE_WINAPI
  35. btime_t time = btime_gettime() + rand()%TIMER_MODULO;
  36. #else
  37. btime_t time = btime_gettime() + random()%TIMER_MODULO;
  38. #endif
  39. BReactor_SetTimerAbsolute(&sys, bt, time);
  40. }
  41. int main ()
  42. {
  43. BLog_InitStdout();
  44. #ifdef BADVPN_USE_WINAPI
  45. srand(time(NULL));
  46. #else
  47. srandom(time(NULL));
  48. #endif
  49. // init time
  50. BTime_Init();
  51. if (!BReactor_Init(&sys)) {
  52. DEBUG("BReactor_Init failed");
  53. return 1;
  54. }
  55. BTimer timers[TIMER_NUM];
  56. int i;
  57. for (i=0; i<TIMER_NUM; i++) {
  58. BTimer *timer = &timers[i];
  59. BTimer_Init(timer, 0, (BTimer_handler)handle_timer, timer);
  60. BReactor_SetTimer(&sys, timer);
  61. }
  62. int ret = BReactor_Exec(&sys);
  63. BReactor_Free(&sys);
  64. return ret;
  65. }