Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/kern/thread/clock.c
2093 views
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
#include <types.h>
31
#include <lib.h>
32
#include <cpu.h>
33
#include <wchan.h>
34
#include <clock.h>
35
#include <thread.h>
36
#include <current.h>
37
38
/*
39
* Time handling.
40
*
41
* This is pretty primitive. A real kernel will typically have some
42
* kind of support for scheduling callbacks to happen at specific
43
* points in the future, usually with more resolution that one second.
44
*
45
* A real kernel also has to maintain the time of day; in OS/161 we
46
* skimp on that because we have a known-good hardware clock.
47
*/
48
49
/*
50
* Timing constants. These should be tuned along with any work done on
51
* the scheduler.
52
*/
53
#define SCHEDULE_HARDCLOCKS 4 /* Reschedule every 4 hardclocks. */
54
#define MIGRATE_HARDCLOCKS 16 /* Migrate every 16 hardclocks. */
55
56
/*
57
* Once a second, everything waiting on lbolt is awakened by CPU 0.
58
*/
59
static struct wchan *lbolt;
60
61
/*
62
* Setup.
63
*/
64
void
65
hardclock_bootstrap(void)
66
{
67
lbolt = wchan_create("lbolt");
68
if (lbolt == NULL) {
69
panic("Couldn't create lbolt\n");
70
}
71
}
72
73
/*
74
* This is called once per second, on one processor, by the timer
75
* code.
76
*/
77
void
78
timerclock(void)
79
{
80
/* Just broadcast on lbolt */
81
wchan_wakeall(lbolt);
82
}
83
84
/*
85
* This is called HZ times a second (on each processor) by the timer
86
* code.
87
*/
88
void
89
hardclock(void)
90
{
91
/*
92
* Collect statistics here as desired.
93
*/
94
95
curcpu->c_hardclocks++;
96
if ((curcpu->c_hardclocks % SCHEDULE_HARDCLOCKS) == 0) {
97
schedule();
98
}
99
if ((curcpu->c_hardclocks % MIGRATE_HARDCLOCKS) == 0) {
100
thread_consider_migration();
101
}
102
thread_yield();
103
}
104
105
/*
106
* Suspend execution for n seconds.
107
*/
108
void
109
clocksleep(int num_secs)
110
{
111
while (num_secs > 0) {
112
wchan_lock(lbolt);
113
wchan_sleep(lbolt);
114
num_secs--;
115
}
116
}
117
118