test_bulletin.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2018 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * This test makes a bulletin subscriber subscribe to a bulletin, and uses
  19. * a timer to periodically publish, unsubscribe, and resubscribe to the
  20. * bulletin. A counter is used to test passing values to the notification
  21. * callback function.
  22. */
  23. #include <stdbool.h>
  24. #include <stddef.h>
  25. #include <stdio.h>
  26. #include <kern/bulletin.h>
  27. #include <kern/clock.h>
  28. #include <kern/error.h>
  29. #include <kern/log.h>
  30. #include <kern/timer.h>
  31. #include <test/test.h>
  32. #define TEST_INTERVAL 1
  33. #define TEST_NR_LOOPS 4
  34. static struct bulletin test_bulletin;
  35. static struct bulletin_sub test_bulletin_sub;
  36. static struct timer test_timer;
  37. static unsigned int test_counter;
  38. static void
  39. test_notify(uintptr_t value, void *arg)
  40. {
  41. log_info("test: notify: value:%lu arg:%p", value, arg);
  42. }
  43. static void
  44. test_tick(struct timer *timer)
  45. {
  46. uint64_t ticks;
  47. test_counter++;
  48. bulletin_publish(&test_bulletin, test_counter);
  49. bulletin_unsubscribe(&test_bulletin, &test_bulletin_sub);
  50. if (test_counter == TEST_NR_LOOPS) {
  51. log_info("test: done");
  52. return;
  53. }
  54. bulletin_subscribe(&test_bulletin, &test_bulletin_sub,
  55. test_notify, (void *)0x123);
  56. ticks = timer_get_time(timer) + clock_ticks_from_ms(TEST_INTERVAL * 1000);
  57. timer_schedule(&test_timer, ticks);
  58. }
  59. void __init
  60. test_setup(void)
  61. {
  62. uint64_t ticks;
  63. bulletin_init(&test_bulletin);
  64. bulletin_subscribe(&test_bulletin, &test_bulletin_sub,
  65. test_notify, (void *)0x123);
  66. timer_init(&test_timer, test_tick, TIMER_DETACHED);
  67. ticks = clock_get_time() + clock_ticks_from_ms(TEST_INTERVAL * 1000);
  68. timer_schedule(&test_timer, ticks);
  69. }