os161-1.99
 All Data Structures
threadtest.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * Thread test code.
00032  */
00033 #include <types.h>
00034 #include <lib.h>
00035 #include <thread.h>
00036 #include <synch.h>
00037 #include <test.h>
00038 
00039 #define NTHREADS  8
00040 
00041 static struct semaphore *tsem = NULL;
00042 
00043 static
00044 void
00045 init_sem(void)
00046 {
00047         if (tsem==NULL) {
00048                 tsem = sem_create("tsem", 0);
00049                 if (tsem == NULL) {
00050                         panic("threadtest: sem_create failed\n");
00051                 }
00052         }
00053 }
00054 
00055 static
00056 void
00057 loudthread(void *junk, unsigned long num)
00058 {
00059         int ch = '0' + num;
00060         int i;
00061 
00062         (void)junk;
00063 
00064         for (i=0; i<120; i++) {
00065                 putch(ch);
00066         }
00067         V(tsem);
00068 }
00069 
00070 /*
00071  * The idea with this is that you should see
00072  *
00073  *   01234567 <pause> 01234567
00074  *
00075  * (possibly with the numbers in different orders)
00076  *
00077  * The delay loop is supposed to be long enough that it should be clear
00078  * if either timeslicing or the scheduler is not working right.
00079  */
00080 static
00081 void
00082 quietthread(void *junk, unsigned long num)
00083 {
00084         int ch = '0' + num;
00085         volatile int i;
00086 
00087         (void)junk;
00088 
00089         putch(ch);
00090         for (i=0; i<200000; i++);
00091         putch(ch);
00092 
00093         V(tsem);
00094 }
00095 
00096 static
00097 void
00098 runthreads(int doloud)
00099 {
00100         char name[16];
00101         int i, result;
00102 
00103         for (i=0; i<NTHREADS; i++) {
00104                 snprintf(name, sizeof(name), "threadtest%d", i);
00105                 result = thread_fork(name, NULL,
00106                                      doloud ? loudthread : quietthread,
00107                                      NULL, i);
00108                 if (result) {
00109                         panic("threadtest: thread_fork failed %s)\n", 
00110                               strerror(result));
00111                 }
00112         }
00113 
00114         for (i=0; i<NTHREADS; i++) {
00115                 P(tsem);
00116         }
00117 }
00118 
00119 
00120 int
00121 threadtest(int nargs, char **args)
00122 {
00123         (void)nargs;
00124         (void)args;
00125 
00126         init_sem();
00127         kprintf("Starting thread test...\n");
00128         runthreads(1);
00129         kprintf("\nThread test done.\n");
00130 
00131         return 0;
00132 }
00133 
00134 int
00135 threadtest2(int nargs, char **args)
00136 {
00137         (void)nargs;
00138         (void)args;
00139 
00140         init_sem();
00141         kprintf("Starting thread test 2...\n");
00142         runthreads(0);
00143         kprintf("\nThread test 2 done.\n");
00144 
00145         return 0;
00146 }
 All Data Structures