/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*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>3738#define NTHREADS 83940static struct semaphore *tsem = NULL;4142static43void44init_sem(void)45{46if (tsem==NULL) {47tsem = sem_create("tsem", 0);48if (tsem == NULL) {49panic("threadtest: sem_create failed\n");50}51}52}5354static55void56loudthread(void *junk, unsigned long num)57{58int ch = '0' + num;59int i;6061(void)junk;6263for (i=0; i<120; i++) {64putch(ch);65}66V(tsem);67}6869/*70* The idea with this is that you should see71*72* 01234567 <pause> 0123456773*74* (possibly with the numbers in different orders)75*76* The delay loop is supposed to be long enough that it should be clear77* if either timeslicing or the scheduler is not working right.78*/79static80void81quietthread(void *junk, unsigned long num)82{83int ch = '0' + num;84volatile int i;8586(void)junk;8788putch(ch);89for (i=0; i<200000; i++);90putch(ch);9192V(tsem);93}9495static96void97runthreads(int doloud)98{99char name[16];100int i, result;101102for (i=0; i<NTHREADS; i++) {103snprintf(name, sizeof(name), "threadtest%d", i);104result = thread_fork(name,105doloud ? loudthread : quietthread,106NULL, i,107NULL);108if (result) {109panic("threadtest: thread_fork failed %s)\n",110strerror(result));111}112}113114for (i=0; i<NTHREADS; i++) {115P(tsem);116}117}118119120int121threadtest(int nargs, char **args)122{123(void)nargs;124(void)args;125126init_sem();127kprintf("Starting thread test...\n");128runthreads(1);129kprintf("\nThread test done.\n");130131return 0;132}133134int135threadtest2(int nargs, char **args)136{137(void)nargs;138(void)args;139140init_sem();141kprintf("Starting thread test 2...\n");142runthreads(0);143kprintf("\nThread test 2 done.\n");144145return 0;146}147148149