1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
#include "threadimpl.h"
Pqueue _threadpq; /* list of all procs */
int _threadnprocs; /* count of procs */
static int newthreadid(void);
static int newprocid(void);
/*
* Create and initialize a new Thread structure attached to a given proc.
*/
int
_newthread(Proc *p, void (*f)(void *arg), void *arg, uint stacksize,
char *name, int grp)
{
int id;
Thread *t;
t = _threadmalloc(sizeof(Thread), 1);
t->proc = p;
t->nextproc = p;
t->homeproc = p;
t->grp = grp;
t->id = id = newthreadid();
if(name)
t->name = strdup(name);
_threaddebug(DBGSCHED, "create thread %d.%d name %s", p->id, id, name);
/*
* Allocate and clear stack.
*/
if(stacksize < 1024)
sysfatal("bad stacksize %d", stacksize);
t->stk = _threadmalloc(stacksize, 0);
t->stksize = stacksize;
_threaddebugmemset(t->stk, 0xFE, stacksize);
/*
* Set up t->context to call f(arg).
*/
_threadinitstack(t, f, arg);
/*
* Add thread to proc.
*/
lock(&p->lock);
_procaddthread(p, t);
/*
* Mark thread as ready to run.
*/
t->state = Ready;
_threadready(t);
unlock(&p->lock);
return id;
}
/*
* Free a Thread structure.
*/
void
_threadfree(Thread *t)
{
free(t->stk);
free(t->name);
free(t);
}
/*
* Create and initialize a new Proc structure with a single Thread
* running inside it. Add the Proc to the global process list.
*/
Proc*
_newproc(void)
{
Proc *p;
/*
* Allocate.
*/
p = _threadmalloc(sizeof *p, 1);
p->id = newprocid();
/*
* Add to list. Record if we're now multiprocess.
*/
lock(&_threadpq.lock);
if(_threadpq.head == nil)
_threadpq.head = p;
else
*_threadpq.tail = p;
_threadpq.tail = &p->next;
if(_threadnprocs == 1)
_threadmultiproc();
_threadnprocs++;
unlock(&_threadpq.lock);
return p;
}
/*
* Allocate a new thread running f(arg) on a stack of size stacksize.
* Return the thread id. The thread group inherits from the current thread.
*/
int
threadcreate(void (*f)(void*), void *arg, uint stacksize)
{
return _newthread(_threadgetproc(), f, arg, stacksize, nil, threadgetgrp());
}
/*
* Allocate a new idle thread. Only allowed in a single-proc program.
*/
int
threadcreateidle(void (*f)(void *arg), void *arg, uint stacksize)
{
int id;
assert(_threadpq.head->next == nil); /* only 1 */
id = threadcreate(f, arg, stacksize);
_threaddebug(DBGSCHED, "idle is %d", id);
_threadsetidle(id);
return id;
}
/*
* Threadcreate, but do it inside a fresh proc.
*/
int
proccreate(void (*f)(void*), void *arg, uint stacksize)
{
int id;
Proc *p, *np;
p = _threadgetproc();
np = _newproc();
p->newproc = np;
p->schedfn = _kthreadstartproc;
id = _newthread(np, f, arg, stacksize, nil, p->thread->grp);
_sched(); /* call into scheduler to create proc XXX */
return id;
}
/*
* Allocate a new thread id.
*/
static int
newthreadid(void)
{
static Lock l;
static int id;
int i;
lock(&l);
i = ++id;
unlock(&l);
return i;
}
/*
* Allocate a new proc id.
*/
static int
newprocid(void)
{
static Lock l;
static int id;
int i;
lock(&l);
i = ++id;
unlock(&l);
return i;
}
/*
* Add thread to proc's list.
*/
void
_procaddthread(Proc *p, Thread *t)
{
p->nthreads++;
if(p->threads.head == nil)
p->threads.head = t;
else{
t->prevt = p->threads.tail;
t->prevt->nextt = t;
}
p->threads.tail = t;
t->next = (Thread*)~0;
}
/*
* Remove thread from proc's list.
*/
void
_procdelthread(Proc *p, Thread *t)
{
if(t->prevt)
t->prevt->nextt = t->nextt;
else
p->threads.head = t->nextt;
if(t->nextt)
t->nextt->prevt = t->prevt;
else
p->threads.tail = t->prevt;
p->nthreads--;
}
|