test_sleepq_broadcast.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2019 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 creates a number of threads (at least 2) which wait on a
  19. * sleep queue, and one that broadcasts the sleep queue after making
  20. * sure all waiters are sleeping.
  21. */
  22. #include <stdbool.h>
  23. #include <stddef.h>
  24. #include <stdio.h>
  25. #include <kern/error.h>
  26. #include <kern/init.h>
  27. #include <kern/log.h>
  28. #include <kern/sleepq.h>
  29. #include <kern/thread.h>
  30. #include <test/test.h>
  31. #define TEST_NR_WAITERS 2
  32. #if TEST_NR_WAITERS < 2
  33. #error "invalid number of waiters"
  34. #endif /* TEST_NR_WAITERS < 2 */
  35. static unsigned int test_dummy_sync_obj;
  36. static struct thread *test_waiters[TEST_NR_WAITERS];
  37. static void
  38. test_wait(void *arg)
  39. {
  40. struct sleepq *sleepq;
  41. (void)arg;
  42. sleepq = sleepq_lend(&test_dummy_sync_obj, false);
  43. sleepq_wait(sleepq, "test");
  44. sleepq_return(sleepq);
  45. }
  46. static void
  47. test_broadcast(void *arg)
  48. {
  49. struct sleepq *sleepq;
  50. (void)arg;
  51. for (size_t i = 0; i < ARRAY_SIZE(test_waiters); i++) {
  52. while (thread_state(test_waiters[i]) != THREAD_SLEEPING) {
  53. thread_delay(1, false);
  54. }
  55. }
  56. sleepq = sleepq_acquire(&test_dummy_sync_obj, false);
  57. if (!sleepq) {
  58. panic("test: unable to acquire sleep queue");
  59. }
  60. sleepq_broadcast(sleepq);
  61. sleepq_release(sleepq);
  62. for (size_t i = 0; i < ARRAY_SIZE(test_waiters); i++) {
  63. thread_join(test_waiters[i]);
  64. }
  65. log_info("test: done");
  66. }
  67. void __init
  68. test_setup(void)
  69. {
  70. char name[THREAD_NAME_SIZE];
  71. struct thread_attr attr;
  72. int error;
  73. for (size_t i = 0; i < ARRAY_SIZE(test_waiters); i++) {
  74. snprintf(name, sizeof(name), THREAD_KERNEL_PREFIX "test_wait:%zu", i);
  75. thread_attr_init(&attr, name);
  76. error = thread_create(&test_waiters[i], &attr, test_wait, NULL);
  77. error_check(error, "thread_create");
  78. }
  79. thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_broadcast");
  80. thread_attr_set_detached(&attr);
  81. error = thread_create(NULL, &attr, test_broadcast, NULL);
  82. error_check(error, "thread_create");
  83. }