test_semaphore.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/log.h>
  28. #include <kern/macros.h>
  29. #include <kern/semaphore.h>
  30. #include <kern/thread.h>
  31. #include <test/test.h>
  32. #define TEST_NR_WAITERS 2
  33. #if TEST_NR_WAITERS < 2
  34. #error "invalid number of waiters"
  35. #endif
  36. static struct semaphore test_semaphore;
  37. static struct thread *test_waiters[TEST_NR_WAITERS];
  38. static void
  39. test_wait (void *arg __unused)
  40. {
  41. semaphore_wait (&test_semaphore);
  42. }
  43. static void
  44. test_post (void *arg __unused)
  45. {
  46. for (size_t i = 0; i < ARRAY_SIZE (test_waiters); i++)
  47. while (thread_state (test_waiters[i]) != THREAD_SLEEPING)
  48. thread_delay (1, false);
  49. thread_preempt_disable ();
  50. for (size_t i = 0; i < ARRAY_SIZE (test_waiters); i++)
  51. {
  52. int error = semaphore_post (&test_semaphore);
  53. test_assert_zero (error);
  54. }
  55. thread_preempt_enable ();
  56. for (size_t i = 0; i < ARRAY_SIZE (test_waiters); i++)
  57. thread_join (test_waiters[i]);
  58. log_info ("test (semaphore): OK");
  59. }
  60. TEST_INLINE (semaphore)
  61. {
  62. semaphore_init (&test_semaphore, 0, TEST_NR_WAITERS);
  63. struct cpumap *cpumap;
  64. int error = cpumap_create (&cpumap);
  65. test_assert_zero (error);
  66. cpumap_zero (cpumap);
  67. cpumap_set (cpumap, 0);
  68. struct thread_attr attr;
  69. for (size_t i = 0; i < ARRAY_SIZE (test_waiters); i++)
  70. {
  71. char name[THREAD_NAME_SIZE];
  72. snprintf (name, sizeof (name), THREAD_KERNEL_PREFIX "test_semwait:%zu", i);
  73. thread_attr_init (&attr, name);
  74. thread_attr_set_cpumap (&attr, cpumap);
  75. error = thread_create (&test_waiters[i], &attr, test_wait, NULL);
  76. test_assert_zero (error);
  77. }
  78. thread_attr_init (&attr, THREAD_KERNEL_PREFIX "test_sempost");
  79. thread_attr_set_detached (&attr);
  80. thread_attr_set_cpumap (&attr, cpumap);
  81. error = thread_create (NULL, &attr, test_post, NULL);
  82. test_assert_zero (error);
  83. return (TEST_RUNNING);
  84. }