os161-1.99
 All Data Structures
clock.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 #include <types.h>
00031 #include <lib.h>
00032 #include <cpu.h>
00033 #include <wchan.h>
00034 #include <clock.h>
00035 #include <thread.h>
00036 #include <current.h>
00037 
00038 /*
00039  * Time handling.
00040  *
00041  * This is pretty primitive. A real kernel will typically have some
00042  * kind of support for scheduling callbacks to happen at specific
00043  * points in the future, usually with more resolution that one second.
00044  *
00045  * A real kernel also has to maintain the time of day; in OS/161 we
00046  * skimp on that because we have a known-good hardware clock.
00047  */
00048 
00049 /*
00050  * Timing constants. These should be tuned along with any work done on
00051  * the scheduler.
00052  */
00053 #define SCHEDULE_HARDCLOCKS     4       /* Reschedule every 4 hardclocks. */
00054 #define MIGRATE_HARDCLOCKS      16      /* Migrate every 16 hardclocks. */
00055 
00056 /*
00057  * Once a second, everything waiting on lbolt is awakened by CPU 0.
00058  */
00059 static struct wchan *lbolt;
00060 
00061 /*
00062  * Setup.
00063  */
00064 void
00065 hardclock_bootstrap(void)
00066 {
00067         lbolt = wchan_create("lbolt");
00068         if (lbolt == NULL) {
00069                 panic("Couldn't create lbolt\n");
00070         }
00071 }
00072 
00073 /*
00074  * This is called once per second, on one processor, by the timer
00075  * code.
00076  */
00077 void
00078 timerclock(void)
00079 {
00080         /* Just broadcast on lbolt */
00081         wchan_wakeall(lbolt);
00082 }
00083 
00084 /*
00085  * This is called HZ times a second (on each processor) by the timer
00086  * code.
00087  */
00088 void
00089 hardclock(void)
00090 {
00091         /*
00092          * Collect statistics here as desired.
00093          */
00094 
00095         curcpu->c_hardclocks++;
00096         if ((curcpu->c_hardclocks % SCHEDULE_HARDCLOCKS) == 0) {
00097                 schedule();
00098         }
00099         if ((curcpu->c_hardclocks % MIGRATE_HARDCLOCKS) == 0) {
00100                 thread_consider_migration();
00101         }
00102         thread_yield();
00103 }
00104 
00105 /*
00106  * Suspend execution for n seconds.
00107  */
00108 void
00109 clocksleep(int num_secs)
00110 {
00111         while (num_secs > 0) {
00112                 wchan_lock(lbolt);
00113                 wchan_sleep(lbolt);
00114                 num_secs--;
00115         }
00116 }
 All Data Structures