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 werrstr("cannot create procs once there is an idle thread");
92 return procrfork(f, arg, stacksize, 0);
100 for(t = p->threads.head; t; t = nextt){
103 assert(t->stk != nil);
112 * Create a new thread and schedule it to run.
113 * The thread grp is inherited from the currently running thread.
116 threadcreate(void (*f)(void *arg), void *arg, uint stacksize)
118 return newthread(_threadgetproc(), f, arg, stacksize, nil, threadgetgrp());
122 threadcreateidle(void (*f)(void *arg), void *arg, uint stacksize)
127 werrstr("cannot have idle thread in multi-proc program");
130 id = newthread(_threadgetproc(), f, arg, stacksize, nil, threadgetgrp());
131 _threaddebug(DBGSCHED, "idle is %d", id);
137 * Create and initialize a new Proc structure with a single Thread
138 * running inside it. Add the Proc to the global process list.
141 _newproc(void (*f)(void *arg), void *arg, uint stacksize, char *name, int grp, int rforkflag)
145 p = _threadmalloc(sizeof *p, 1);
147 p->rforkflag = rforkflag;
148 newthread(p, f, arg, stacksize, name, grp);
150 lock(&_threadpq.lock);
151 if(_threadpq.head == nil)
155 _threadpq.tail = &p->next;
157 unlock(&_threadpq.lock);