root/kern/test/threadtest.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. init_sem
  2. loudthread
  3. quietthread
  4. runthreads
  5. threadtest
  6. threadtest2

   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 /*
  31  * Thread test code.
  32  */
  33 #include <types.h>
  34 #include <lib.h>
  35 #include <thread.h>
  36 #include <synch.h>
  37 #include <test.h>
  38 
  39 #define NTHREADS  8
  40 
  41 static struct semaphore *tsem = NULL;
  42 
  43 static
  44 void
  45 init_sem(void)
  46 {
  47         if (tsem==NULL) {
  48                 tsem = sem_create("tsem", 0);
  49                 if (tsem == NULL) {
  50                         panic("threadtest: sem_create failed\n");
  51                 }
  52         }
  53 }
  54 
  55 static
  56 void
  57 loudthread(void *junk, unsigned long num)
  58 {
  59         int ch = '0' + num;
  60         int i;
  61 
  62         (void)junk;
  63 
  64         for (i=0; i<120; i++) {
  65                 putch(ch);
  66         }
  67         V(tsem);
  68 }
  69 
  70 /*
  71  * The idea with this is that you should see
  72  *
  73  *   01234567 <pause> 01234567
  74  *
  75  * (possibly with the numbers in different orders)
  76  *
  77  * The delay loop is supposed to be long enough that it should be clear
  78  * if either timeslicing or the scheduler is not working right.
  79  */
  80 static
  81 void
  82 quietthread(void *junk, unsigned long num)
  83 {
  84         int ch = '0' + num;
  85         volatile int i;
  86 
  87         (void)junk;
  88 
  89         putch(ch);
  90         for (i=0; i<200000; i++);
  91         putch(ch);
  92 
  93         V(tsem);
  94 }
  95 
  96 static
  97 void
  98 runthreads(int doloud)
  99 {
 100         char name[16];
 101         int i, result;
 102 
 103         for (i=0; i<NTHREADS; i++) {
 104                 snprintf(name, sizeof(name), "threadtest%d", i);
 105                 result = thread_fork(name, NULL,
 106                                      doloud ? loudthread : quietthread,
 107                                      NULL, i);
 108                 if (result) {
 109                         panic("threadtest: thread_fork failed %s)\n", 
 110                               strerror(result));
 111                 }
 112         }
 113 
 114         for (i=0; i<NTHREADS; i++) {
 115                 P(tsem);
 116         }
 117 }
 118 
 119 
 120 int
 121 threadtest(int nargs, char **args)
 122 {
 123         (void)nargs;
 124         (void)args;
 125 
 126         init_sem();
 127         kprintf("Starting thread test...\n");
 128         runthreads(1);
 129         kprintf("\nThread test done.\n");
 130 
 131         return 0;
 132 }
 133 
 134 int
 135 threadtest2(int nargs, char **args)
 136 {
 137         (void)nargs;
 138         (void)args;
 139 
 140         init_sem();
 141         kprintf("Starting thread test 2...\n");
 142         runthreads(0);
 143         kprintf("\nThread test 2 done.\n");
 144 
 145         return 0;
 146 }

/* [<][>][^][v][top][bottom][index][help] */