1 #include "threadimpl.h"
6 static int nextID(void);
9 * Create and initialize a new Thread structure attached to a given proc.
18 newthread(Proc *p, void (*f)(void *arg), void *arg, uint stacksize, char *name, int grp)
25 sysfatal("bad stacksize %d", stacksize);
26 t = _threadmalloc(sizeof(Thread), 1);
28 s = _threadmalloc(stacksize, 0);
30 t->stksize = stacksize;
31 _threaddebugmemset(s, 0xFE, stacksize);
32 _threadinitstack(t, f, arg);
36 t->cmdname = strdup(name);
39 t->next = (Thread*)~0;
40 _threaddebug(DBGSCHED, "create thread %d.%d name %s", p->pid, t->id, name);
43 if(p->threads.head == nil)
46 t->prevt = p->threads.tail;
70 procrfork(void (*f)(void *), void *arg, uint stacksize, int rforkflag)
76 assert(p->newproc == nil);
77 p->newproc = _newproc(f, arg, stacksize, nil, p->thread->grp, rforkflag);
78 id = p->newproc->threads.head->id;
84 proccreate(void (*f)(void*), void *arg, uint stacksize)
90 fprint(2, "cannot create procs once there is an idle thread\n");
91 werrstr("cannot create procs once there is an idle thread");
94 return procrfork(f, arg, stacksize, 0);
102 for(t = p->threads.head; t; t = nextt){
105 assert(t->stk != nil);
114 * Create a new thread and schedule it to run.
115 * The thread grp is inherited from the currently running thread.
118 threadcreate(void (*f)(void *arg), void *arg, uint stacksize)
120 return newthread(_threadgetproc(), f, arg, stacksize, nil, threadgetgrp());
124 threadcreateidle(void (*f)(void *arg), void *arg, uint stacksize)
129 fprint(2, "cannot have idle thread in multi-proc program\n");
130 werrstr("cannot have idle thread in multi-proc program");
133 id = newthread(_threadgetproc(), f, arg, stacksize, nil, threadgetgrp());
134 _threaddebug(DBGSCHED, "idle is %d", id);
140 * Create and initialize a new Proc structure with a single Thread
141 * running inside it. Add the Proc to the global process list.
144 _newproc(void (*f)(void *arg), void *arg, uint stacksize, char *name, int grp, int rforkflag)
148 p = _threadmalloc(sizeof *p, 1);
150 p->rforkflag = rforkflag;
151 newthread(p, f, arg, stacksize, name, grp);
153 lock(&_threadpq.lock);
154 if(_threadpq.head == nil)
158 _threadpq.tail = &p->next;
160 if(_threadprocs == 1)
163 unlock(&_threadpq.lock);