test_semaphore.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * semaphore, and one that posts the semaphore as many times as there
  20. * are threads. All threads are bound to the same processor, and posts
  21. * are performed with preemption disabled, to guarantee they occur
  22. * back-to-back in order to make sure that they're not missed.
  23. */
  24. #include <stddef.h>
  25. #include <stdio.h>
  26. #include <kern/cpumap.h>
  27. #include <kern/error.h>
  28. #include <kern/log.h>
  29. #include <kern/macros.h>
  30. #include <kern/semaphore.h>
  31. #include <kern/thread.h>
  32. #include <test/test.h>
  33. #define TEST_NR_WAITERS 2
  34. #if TEST_NR_WAITERS < 2
  35. #error "invalid number of waiters"
  36. #endif /* TEST_NR_WAITERS < 2 */
  37. static struct semaphore test_semaphore;
  38. static struct thread *test_waiters[TEST_NR_WAITERS];
  39. static void
  40. test_wait(void *arg)
  41. {
  42. (void)arg;
  43. semaphore_wait(&test_semaphore);
  44. }
  45. static void
  46. test_post(void *arg)
  47. {
  48. int error;
  49. (void)arg;
  50. for (size_t i = 0; i < ARRAY_SIZE(test_waiters); i++) {
  51. while (thread_state(test_waiters[i]) != THREAD_SLEEPING) {
  52. thread_delay(1, false);
  53. }
  54. }
  55. thread_preempt_disable();
  56. for (size_t i = 0; i < ARRAY_SIZE(test_waiters); i++) {
  57. error = semaphore_post(&test_semaphore);
  58. error_check(error, "semaphore_post");
  59. }
  60. thread_preempt_enable();
  61. for (size_t i = 0; i < ARRAY_SIZE(test_waiters); i++) {
  62. thread_join(test_waiters[i]);
  63. }
  64. log_info("test: done");
  65. }
  66. void __init
  67. test_setup(void)
  68. {
  69. char name[THREAD_NAME_SIZE];
  70. struct thread_attr attr;
  71. struct cpumap *cpumap;
  72. int error;
  73. semaphore_init(&test_semaphore, 0, TEST_NR_WAITERS);
  74. error = cpumap_create(&cpumap);
  75. error_check(error, "cpumap_create");
  76. cpumap_zero(cpumap);
  77. cpumap_set(cpumap, 0);
  78. for (size_t i = 0; i < ARRAY_SIZE(test_waiters); i++) {
  79. snprintf(name, sizeof(name), THREAD_KERNEL_PREFIX "test_wait:%zu", i);
  80. thread_attr_init(&attr, name);
  81. thread_attr_set_cpumap(&attr, cpumap);
  82. error = thread_create(&test_waiters[i], &attr, test_wait, NULL);
  83. error_check(error, "thread_create");
  84. }
  85. thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_post");
  86. thread_attr_set_detached(&attr);
  87. thread_attr_set_cpumap(&attr, cpumap);
  88. error = thread_create(NULL, &attr, test_post, NULL);
  89. error_check(error, "thread_create");
  90. }