test_semaphore.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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
  37. static struct semaphore test_semaphore;
  38. static struct thread *test_waiters[TEST_NR_WAITERS];
  39. static void
  40. test_wait (void *arg __unused)
  41. {
  42. semaphore_wait (&test_semaphore);
  43. }
  44. static void
  45. test_post (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. thread_preempt_disable ();
  51. for (size_t i = 0; i < ARRAY_SIZE (test_waiters); i++)
  52. {
  53. int error = semaphore_post (&test_semaphore);
  54. error_check (error, "semaphore_post");
  55. }
  56. thread_preempt_enable ();
  57. for (size_t i = 0; i < ARRAY_SIZE (test_waiters); i++)
  58. thread_join (test_waiters[i]);
  59. log_info ("test (semaphore): OK");
  60. }
  61. TEST_INLINE (semaphore)
  62. {
  63. semaphore_init (&test_semaphore, 0, TEST_NR_WAITERS);
  64. struct cpumap *cpumap;
  65. int error = cpumap_create (&cpumap);
  66. error_check (error, "cpumap_create");
  67. cpumap_zero (cpumap);
  68. cpumap_set (cpumap, 0);
  69. struct thread_attr attr;
  70. for (size_t i = 0; i < ARRAY_SIZE (test_waiters); i++)
  71. {
  72. char name[THREAD_NAME_SIZE];
  73. snprintf (name, sizeof (name), THREAD_KERNEL_PREFIX "test_semwait:%zu", i);
  74. thread_attr_init (&attr, name);
  75. thread_attr_set_cpumap (&attr, cpumap);
  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_sempost");
  80. thread_attr_set_detached (&attr);
  81. thread_attr_set_cpumap (&attr, cpumap);
  82. error = thread_create (NULL, &attr, test_post, NULL);
  83. error_check (error, "thread_create");
  84. return (TEST_RUNNING);
  85. }