1 76193d7c 2003-09-30 devnull #include "threadimpl.h"
3 76193d7c 2003-09-30 devnull Pqueue _threadpq;
4 32f69c36 2003-12-11 devnull int _threadprocs;
6 76193d7c 2003-09-30 devnull static int nextID(void);
9 76193d7c 2003-09-30 devnull * Create and initialize a new Thread structure attached to a given proc.
12 76193d7c 2003-09-30 devnull _stackfree(void *v)
17 76193d7c 2003-09-30 devnull static int
18 76193d7c 2003-09-30 devnull newthread(Proc *p, void (*f)(void *arg), void *arg, uint stacksize, char *name, int grp)
21 76193d7c 2003-09-30 devnull Thread *t;
24 76193d7c 2003-09-30 devnull if(stacksize < 32)
25 76193d7c 2003-09-30 devnull sysfatal("bad stacksize %d", stacksize);
26 76193d7c 2003-09-30 devnull t = _threadmalloc(sizeof(Thread), 1);
27 cd7ddc9b 2003-11-23 devnull s = _threadmalloc(stacksize, 0);
28 a0f1e21f 2004-04-20 devnull t->stk = (uchar*)s;
29 cd7ddc9b 2003-11-23 devnull t->stksize = stacksize;
30 cd7ddc9b 2003-11-23 devnull _threaddebugmemset(s, 0xFE, stacksize);
31 76193d7c 2003-09-30 devnull _threadinitstack(t, f, arg);
32 76193d7c 2003-09-30 devnull t->proc = p;
33 76193d7c 2003-09-30 devnull t->grp = grp;
35 76193d7c 2003-09-30 devnull t->cmdname = strdup(name);
36 76193d7c 2003-09-30 devnull t->id = nextID();
37 76193d7c 2003-09-30 devnull id = t->id;
38 76193d7c 2003-09-30 devnull t->next = (Thread*)~0;
39 76193d7c 2003-09-30 devnull _threaddebug(DBGSCHED, "create thread %d.%d name %s", p->pid, t->id, name);
40 76193d7c 2003-09-30 devnull lock(&p->lock);
41 76193d7c 2003-09-30 devnull p->nthreads++;
42 76193d7c 2003-09-30 devnull if(p->threads.head == nil)
43 76193d7c 2003-09-30 devnull p->threads.head = t;
45 76193d7c 2003-09-30 devnull t->prevt = p->threads.tail;
46 76193d7c 2003-09-30 devnull t->prevt->nextt = t;
48 76193d7c 2003-09-30 devnull p->threads.tail = t;
49 76193d7c 2003-09-30 devnull t->state = Ready;
50 76193d7c 2003-09-30 devnull _threadready(t);
51 76193d7c 2003-09-30 devnull unlock(&p->lock);
52 76193d7c 2003-09-30 devnull return id;
55 76193d7c 2003-09-30 devnull static int
56 76193d7c 2003-09-30 devnull nextID(void)
58 76193d7c 2003-09-30 devnull static Lock l;
59 76193d7c 2003-09-30 devnull static int id;
62 76193d7c 2003-09-30 devnull lock(&l);
63 76193d7c 2003-09-30 devnull i = ++id;
64 76193d7c 2003-09-30 devnull unlock(&l);
65 76193d7c 2003-09-30 devnull return i;
69 76193d7c 2003-09-30 devnull procrfork(void (*f)(void *), void *arg, uint stacksize, int rforkflag)
74 76193d7c 2003-09-30 devnull p = _threadgetproc();
75 76193d7c 2003-09-30 devnull assert(p->newproc == nil);
76 76193d7c 2003-09-30 devnull p->newproc = _newproc(f, arg, stacksize, nil, p->thread->grp, rforkflag);
77 76193d7c 2003-09-30 devnull id = p->newproc->threads.head->id;
78 76193d7c 2003-09-30 devnull _sched();
79 76193d7c 2003-09-30 devnull return id;
83 76193d7c 2003-09-30 devnull proccreate(void (*f)(void*), void *arg, uint stacksize)
87 e97ceade 2003-12-06 devnull p = _threadgetproc();
88 e97ceade 2003-12-06 devnull if(p->idle){
89 5a8e63b2 2004-02-29 devnull fprint(2, "cannot create procs once there is an idle thread\n");
90 e97ceade 2003-12-06 devnull werrstr("cannot create procs once there is an idle thread");
91 e97ceade 2003-12-06 devnull return -1;
93 76193d7c 2003-09-30 devnull return procrfork(f, arg, stacksize, 0);
97 76193d7c 2003-09-30 devnull _freeproc(Proc *p)
99 76193d7c 2003-09-30 devnull Thread *t, *nextt;
101 76193d7c 2003-09-30 devnull for(t = p->threads.head; t; t = nextt){
102 76193d7c 2003-09-30 devnull if(t->cmdname)
103 76193d7c 2003-09-30 devnull free(t->cmdname);
104 76193d7c 2003-09-30 devnull assert(t->stk != nil);
105 cd7ddc9b 2003-11-23 devnull _stackfree(t->stk);
106 76193d7c 2003-09-30 devnull nextt = t->nextt;
107 76193d7c 2003-09-30 devnull free(t);
109 76193d7c 2003-09-30 devnull free(p);
113 76193d7c 2003-09-30 devnull * Create a new thread and schedule it to run.
114 76193d7c 2003-09-30 devnull * The thread grp is inherited from the currently running thread.
117 76193d7c 2003-09-30 devnull threadcreate(void (*f)(void *arg), void *arg, uint stacksize)
119 76193d7c 2003-09-30 devnull return newthread(_threadgetproc(), f, arg, stacksize, nil, threadgetgrp());
123 e97ceade 2003-12-06 devnull threadcreateidle(void (*f)(void *arg), void *arg, uint stacksize)
127 32f69c36 2003-12-11 devnull if(_threadprocs!=1){
128 5a8e63b2 2004-02-29 devnull fprint(2, "cannot have idle thread in multi-proc program\n");
129 e97ceade 2003-12-06 devnull werrstr("cannot have idle thread in multi-proc program");
130 e97ceade 2003-12-06 devnull return -1;
132 e97ceade 2003-12-06 devnull id = newthread(_threadgetproc(), f, arg, stacksize, nil, threadgetgrp());
133 32f69c36 2003-12-11 devnull _threaddebug(DBGSCHED, "idle is %d", id);
134 e97ceade 2003-12-06 devnull _threadidle();
135 e97ceade 2003-12-06 devnull return id;
139 76193d7c 2003-09-30 devnull * Create and initialize a new Proc structure with a single Thread
140 76193d7c 2003-09-30 devnull * running inside it. Add the Proc to the global process list.
143 76193d7c 2003-09-30 devnull _newproc(void (*f)(void *arg), void *arg, uint stacksize, char *name, int grp, int rforkflag)
145 76193d7c 2003-09-30 devnull Proc *p;
147 76193d7c 2003-09-30 devnull p = _threadmalloc(sizeof *p, 1);
148 76193d7c 2003-09-30 devnull p->pid = -1;
149 76193d7c 2003-09-30 devnull p->rforkflag = rforkflag;
150 76193d7c 2003-09-30 devnull newthread(p, f, arg, stacksize, name, grp);
152 76193d7c 2003-09-30 devnull lock(&_threadpq.lock);
153 76193d7c 2003-09-30 devnull if(_threadpq.head == nil)
154 76193d7c 2003-09-30 devnull _threadpq.head = p;
156 76193d7c 2003-09-30 devnull *_threadpq.tail = p;
157 76193d7c 2003-09-30 devnull _threadpq.tail = &p->next;
158 5a8e63b2 2004-02-29 devnull if(_threadprocs == 1)
159 5a8e63b2 2004-02-29 devnull _threadmultiproc();
160 32f69c36 2003-12-11 devnull _threadprocs++;
161 76193d7c 2003-09-30 devnull unlock(&_threadpq.lock);
162 76193d7c 2003-09-30 devnull return p;