4 #include "threadimpl.h"
22 * Create and initialize a new Thread structure attached to a given proc.
25 newthread(Proc *p, void (*f)(void *arg), void *arg, uint stacksize, char *name, int grp)
31 sysfatal("bad stacksize %d", stacksize);
32 t = _threadmalloc(sizeof(Thread), 1);
33 t->stksize = stacksize;
34 t->stk = _threadmalloc(stacksize, 0);
35 memset(t->stk, 0xFE, stacksize);
36 _threadinitstack(t, f, arg);
39 t->cmdname = strdup(name);
42 t->next = (Thread*)~0;
44 _threaddebug(DBGSCHED, "create thread %d.%d name %s", p->pid, t->id, name);
47 if(p->threads.head == nil)
51 p->threads.tail = &t->nextt;
60 * Create a new thread and schedule it to run.
61 * The thread grp is inherited from the currently running thread.
64 threadcreate(void (*f)(void *arg), void *arg, uint stacksize)
66 return newthread(_threadgetproc(), f, arg, stacksize, nil, threadgetgrp());
70 * Create and initialize a new Proc structure with a single Thread
71 * running inside it. Add the Proc to the global process list.
74 _newproc(void (*f)(void *arg), void *arg, uint stacksize, char *name, int grp, int rforkflag)
78 p = _threadmalloc(sizeof *p, 1);
80 p->rforkflag = rforkflag;
81 newthread(p, f, arg, stacksize, name, grp);
83 lock(&_threadpq.lock);
84 if(_threadpq.head == nil)
88 _threadpq.tail = &p->next;
89 unlock(&_threadpq.lock);
94 procrfork(void (*f)(void *), void *arg, uint stacksize, int rforkflag)
100 assert(p->newproc == nil);
101 p->newproc = _newproc(f, arg, stacksize, nil, p->thread->grp, rforkflag);
102 id = p->newproc->threads.head->id;
108 proccreate(void (*f)(void*), void *arg, uint stacksize)
110 return procrfork(f, arg, stacksize, 0);