test_sleepq_broadcast.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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
  35. static uint32_t test_dummy_sync_obj;
  36. static struct thread *test_waiters[TEST_NR_WAITERS];
  37. static void
  38. test_wait (void *arg __unused)
  39. {
  40. struct sleepq *sleepq = sleepq_lend (&test_dummy_sync_obj, false);
  41. sleepq_wait (sleepq, "test");
  42. sleepq_return (sleepq);
  43. }
  44. static void
  45. test_broadcast (void *arg __unused)
  46. {
  47. for (size_t i = 0; i < ARRAY_SIZE (test_waiters); i++)
  48. while (thread_state (test_waiters[i]) != THREAD_SLEEPING)
  49. thread_delay (1, false);
  50. struct sleepq *sleepq = sleepq_acquire (&test_dummy_sync_obj, false);
  51. if (! sleepq)
  52. panic ("test: unable to acquire sleep queue");
  53. sleepq_broadcast (sleepq);
  54. sleepq_release (sleepq);
  55. for (size_t i = 0; i < ARRAY_SIZE (test_waiters); i++)
  56. thread_join (test_waiters[i]);
  57. log_info ("test (sleepq_broadcast): done");
  58. }
  59. TEST_INLINE (sleepq_broadcast)
  60. {
  61. struct thread_attr attr;
  62. int error;
  63. for (size_t i = 0; i < ARRAY_SIZE (test_waiters); i++)
  64. {
  65. char name[THREAD_NAME_SIZE];
  66. snprintf (name, sizeof (name), THREAD_KERNEL_PREFIX "test_wait:%zu", i);
  67. thread_attr_init (&attr, name);
  68. error = thread_create (&test_waiters[i], &attr, test_wait, NULL);
  69. error_check (error, "thread_create");
  70. }
  71. thread_attr_init (&attr, THREAD_KERNEL_PREFIX "test_broadcast");
  72. thread_attr_set_detached (&attr);
  73. error = thread_create (NULL, &attr, test_broadcast, NULL);
  74. error_check (error, "thread_create");
  75. return (TEST_RUNNING);
  76. }