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);
27 s = _threadmalloc(stacksize, 0);
29 t->stksize = stacksize;
30 _threaddebugmemset(s, 0xFE, stacksize);
31 _threadinitstack(t, f, arg);
35 t->cmdname = strdup(name);
38 t->next = (Thread*)~0;
39 _threaddebug(DBGSCHED, "create thread %d.%d name %s", p->pid, t->id, name);
42 if(p->threads.head == nil)
45 t->prevt = p->threads.tail;
69 procrfork(void (*f)(void *), void *arg, uint stacksize, int rforkflag)
75 assert(p->newproc == nil);
76 p->newproc = _newproc(f, arg, stacksize, nil, p->thread->grp, rforkflag);
77 id = p->newproc->threads.head->id;
83 proccreate(void (*f)(void*), void *arg, uint stacksize)
89 fprint(2, "cannot create procs once there is an idle thread\n");
90 werrstr("cannot create procs once there is an idle thread");
93 return procrfork(f, arg, stacksize, 0);
101 for(t = p->threads.head; t; t = nextt){
104 assert(t->stk != nil);
113 * Create a new thread and schedule it to run.
114 * The thread grp is inherited from the currently running thread.
117 threadcreate(void (*f)(void *arg), void *arg, uint stacksize)
119 return newthread(_threadgetproc(), f, arg, stacksize, nil, threadgetgrp());
123 threadcreateidle(void (*f)(void *arg), void *arg, uint stacksize)
128 fprint(2, "cannot have idle thread in multi-proc program\n");
129 werrstr("cannot have idle thread in multi-proc program");
132 id = newthread(_threadgetproc(), f, arg, stacksize, nil, threadgetgrp());
133 _threaddebug(DBGSCHED, "idle is %d", id);
139 * Create and initialize a new Proc structure with a single Thread
140 * running inside it. Add the Proc to the global process list.
143 _newproc(void (*f)(void *arg), void *arg, uint stacksize, char *name, int grp, int rforkflag)
147 p = _threadmalloc(sizeof *p, 1);
149 p->rforkflag = rforkflag;
150 newthread(p, f, arg, stacksize, name, grp);
152 lock(&_threadpq.lock);
153 if(_threadpq.head == nil)
157 _threadpq.tail = &p->next;
158 if(_threadprocs == 1)
161 unlock(&_threadpq.lock);