test_thread_suspend.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2018 Agustina Arzille.
  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 checks that thread state transitions are correctly performed
  19. * when a thread is suspended and resumed. It does so by creating three
  20. * threads :
  21. *
  22. * - The first thread spins on an atomic integer used as a lock, which
  23. * puts it in the running state. The lock is released while the thread
  24. * is suspended, and the thread is then resumed.
  25. *
  26. * - The second thread waits on a zero-valued semaphore, which puts it
  27. * in the sleeping state. The semaphore is signalled while the thread
  28. * is suspended, and the thread is then resumed.
  29. *
  30. * - The third suspends itself and is then resumed.
  31. *
  32. * As a result, the following transitions are tested :
  33. * o CREATED -> RUNNING (*) -> SUSPENDED -> RUNNING
  34. * o CREATED -> RUNNING -> SLEEPING (*) -> SUSPENDED -> RUNNING.
  35. *
  36. * (*) Step where a suspend request is made
  37. */
  38. #include <stdbool.h>
  39. #include <stddef.h>
  40. #include <stdio.h>
  41. #include <kern/atomic.h>
  42. #include <kern/error.h>
  43. #include <kern/init.h>
  44. #include <kern/log.h>
  45. #include <kern/panic.h>
  46. #include <kern/semaphore.h>
  47. #include <kern/thread.h>
  48. #include <machine/cpu.h>
  49. #include <test/test.h>
  50. static void
  51. test_wait_for_state(const struct thread *thread, unsigned int state)
  52. {
  53. while (thread_state(thread) != state) {
  54. cpu_pause();
  55. }
  56. }
  57. static void
  58. test_spin(void *arg)
  59. {
  60. unsigned long *lock;
  61. lock = arg;
  62. while (atomic_cas(lock, 0UL, 1UL, ATOMIC_ACQ_REL) != 0) {
  63. cpu_pause();
  64. }
  65. }
  66. static void
  67. test_sleep(void *arg)
  68. {
  69. struct semaphore *sem;
  70. sem = arg;
  71. semaphore_wait(sem);
  72. }
  73. static void
  74. test_suspend_self(void *arg)
  75. {
  76. (void)arg;
  77. thread_suspend(thread_self());
  78. }
  79. static void
  80. test_run(void *arg)
  81. {
  82. struct thread *thread;
  83. struct thread_attr attr;
  84. unsigned long lock;
  85. struct semaphore sem;
  86. int error;
  87. (void)arg;
  88. lock = 1;
  89. thread_attr_init(&attr, "test_spin");
  90. error = thread_create(&thread, &attr, test_spin, &lock);
  91. error_check(error, "thread_create");
  92. test_wait_for_state(thread, THREAD_RUNNING);
  93. thread_suspend(thread);
  94. test_wait_for_state(thread, THREAD_SUSPENDED);
  95. atomic_store(&lock, 0, ATOMIC_RELEASE);
  96. thread_resume(thread);
  97. thread_join(thread);
  98. semaphore_init(&sem, 0, 0xff);
  99. thread_attr_init(&attr, "test_sleep");
  100. error = thread_create(&thread, &attr, test_sleep, &sem);
  101. error_check(error, "thread_create");
  102. test_wait_for_state(thread, THREAD_SLEEPING);
  103. thread_suspend(thread);
  104. test_wait_for_state(thread, THREAD_SUSPENDED);
  105. thread_wakeup(thread);
  106. if (thread_state(thread) != THREAD_SUSPENDED) {
  107. panic("test: unexpected thread state");
  108. }
  109. semaphore_post(&sem);
  110. thread_resume(thread);
  111. thread_join(thread);
  112. thread_attr_init(&attr, "test_suspend_self");
  113. error = thread_create(&thread, &attr, test_suspend_self, NULL);
  114. test_wait_for_state(thread, THREAD_SUSPENDED);
  115. thread_resume(thread);
  116. thread_join(thread);
  117. log_info("done");
  118. }
  119. void __init
  120. test_setup(void)
  121. {
  122. struct thread_attr attr;
  123. int error;
  124. thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_run");
  125. thread_attr_set_detached(&attr);
  126. error = thread_create(NULL, &attr, test_run, NULL);
  127. error_check(error, "thread_create");
  128. }