test_thread_suspend.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_spin (void *arg)
  52. {
  53. unsigned long *lock = arg;
  54. while (atomic_cas (lock, 0, 1, ATOMIC_ACQ_REL) != 0)
  55. cpu_pause ();
  56. }
  57. static void
  58. test_sleep (void *arg)
  59. {
  60. semaphore_wait ((struct semaphore *)arg);
  61. }
  62. static void
  63. test_suspend_self (void *arg __unused)
  64. {
  65. thread_suspend (thread_self ());
  66. }
  67. TEST_DEFERRED (thread_suspend)
  68. {
  69. unsigned long lock = 1;
  70. struct thread_attr attr;
  71. struct thread *thread;
  72. thread_attr_init (&attr, "test_spin");
  73. int error = thread_create (&thread, &attr, test_spin, &lock);
  74. error_check (error, "thread_create");
  75. test_thread_wait_state (thread, THREAD_RUNNING);
  76. thread_suspend (thread);
  77. test_thread_wait_state (thread, THREAD_SUSPENDED);
  78. atomic_store_rel (&lock, 0);
  79. thread_resume (thread);
  80. thread_join (thread);
  81. struct semaphore sem;
  82. semaphore_init (&sem, 0, 0xff);
  83. thread_attr_init (&attr, "test_sleep");
  84. error = thread_create (&thread, &attr, test_sleep, &sem);
  85. error_check (error, "thread_create");
  86. test_thread_wait_state (thread, THREAD_SLEEPING);
  87. thread_suspend (thread);
  88. test_thread_wait_state (thread, THREAD_SUSPENDED);
  89. thread_wakeup (thread);
  90. if (thread_state (thread) != THREAD_SUSPENDED)
  91. panic ("test: unexpected thread state");
  92. semaphore_post (&sem);
  93. thread_resume (thread);
  94. thread_join (thread);
  95. thread_attr_init (&attr, "test_suspend_self");
  96. error = thread_create (&thread, &attr, test_suspend_self, NULL);
  97. test_thread_wait_state (thread, THREAD_SUSPENDED);
  98. thread_resume (thread);
  99. thread_join (thread);
  100. return (TEST_OK);
  101. }