threadtest.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Thread test code.
  31. */
  32. #include <types.h>
  33. #include <lib.h>
  34. #include <thread.h>
  35. #include <synch.h>
  36. #include <test.h>
  37. #define NTHREADS 8
  38. static struct semaphore *tsem = NULL;
  39. static
  40. void
  41. init_sem(void)
  42. {
  43. if (tsem==NULL) {
  44. tsem = sem_create("tsem", 0);
  45. if (tsem == NULL) {
  46. panic("threadtest: sem_create failed\n");
  47. }
  48. }
  49. }
  50. static
  51. void
  52. loudthread(void *junk, unsigned long num)
  53. {
  54. int ch = '0' + num;
  55. int i;
  56. (void)junk;
  57. for (i=0; i<120; i++) {
  58. putch(ch);
  59. }
  60. V(tsem);
  61. }
  62. /*
  63. * The idea with this is that you should see
  64. *
  65. * 01234567 <pause> 01234567
  66. *
  67. * (possibly with the numbers in different orders)
  68. *
  69. * The delay loop is supposed to be long enough that it should be clear
  70. * if either timeslicing or the scheduler is not working right.
  71. */
  72. static
  73. void
  74. quietthread(void *junk, unsigned long num)
  75. {
  76. int ch = '0' + num;
  77. volatile int i;
  78. (void)junk;
  79. putch(ch);
  80. for (i=0; i<200000; i++);
  81. putch(ch);
  82. V(tsem);
  83. }
  84. static
  85. void
  86. runthreads(int doloud)
  87. {
  88. char name[16];
  89. int i, result;
  90. for (i=0; i<NTHREADS; i++) {
  91. snprintf(name, sizeof(name), "threadtest%d", i);
  92. result = thread_fork(name, NULL,
  93. doloud ? loudthread : quietthread,
  94. NULL, i);
  95. if (result) {
  96. panic("threadtest: thread_fork failed %s)\n",
  97. strerror(result));
  98. }
  99. }
  100. for (i=0; i<NTHREADS; i++) {
  101. P(tsem);
  102. }
  103. }
  104. int
  105. threadtest(int nargs, char **args)
  106. {
  107. (void)nargs;
  108. (void)args;
  109. init_sem();
  110. kprintf("Starting thread test...\n");
  111. runthreads(1);
  112. kprintf("\nThread test done.\n");
  113. return 0;
  114. }
  115. int
  116. threadtest2(int nargs, char **args)
  117. {
  118. (void)nargs;
  119. (void)args;
  120. init_sem();
  121. kprintf("Starting thread test 2...\n");
  122. runthreads(0);
  123. kprintf("\nThread test 2 done.\n");
  124. return 0;
  125. }