1 #include "threadimpl.h"
5 static int nextID(void);
8 * Create and initialize a new Thread structure attached to a given proc.
11 typedef struct Stack Stack;
16 uchar buf[STKSIZE-12];
19 static Stack *stkfree;
44 while(stkfree == nil){
46 assert(STKSIZE == sizeof(Stack));
47 buf = malloc(STKSIZE+128*STKSIZE);
48 s = (Stack*)(((ulong)buf+STKSIZE)&~(STKSIZE-1));
54 stkfree = stkfree->next;
61 newthread(Proc *p, void (*f)(void *arg), void *arg, uint stacksize, char *name, int grp)
68 sysfatal("bad stacksize %d", stacksize);
69 t = _threadmalloc(sizeof(Thread), 1);
74 _threaddebugmemset(s->buf, 0xFE, sizeof s->buf);
75 _threadinitstack(t, f, arg);
79 t->cmdname = strdup(name);
82 t->next = (Thread*)~0;
83 _threaddebug(DBGSCHED, "create thread %d.%d name %s", p->pid, t->id, name);
86 if(p->threads.head == nil)
89 t->prevt = p->threads.tail;
113 procrfork(void (*f)(void *), void *arg, uint stacksize, int rforkflag)
118 p = _threadgetproc();
119 assert(p->newproc == nil);
120 p->newproc = _newproc(f, arg, stacksize, nil, p->thread->grp, rforkflag);
121 id = p->newproc->threads.head->id;
127 proccreate(void (*f)(void*), void *arg, uint stacksize)
129 return procrfork(f, arg, stacksize, 0);
137 for(t = p->threads.head; t; t = nextt){
140 assert(t->stk != nil);
141 _stackfree((Stack*)t->stk);
149 * Create a new thread and schedule it to run.
150 * The thread grp is inherited from the currently running thread.
153 threadcreate(void (*f)(void *arg), void *arg, uint stacksize)
155 return newthread(_threadgetproc(), f, arg, stacksize, nil, threadgetgrp());
159 * Create and initialize a new Proc structure with a single Thread
160 * running inside it. Add the Proc to the global process list.
163 _newproc(void (*f)(void *arg), void *arg, uint stacksize, char *name, int grp, int rforkflag)
167 p = _threadmalloc(sizeof *p, 1);
169 p->rforkflag = rforkflag;
170 newthread(p, f, arg, stacksize, name, grp);
172 lock(&_threadpq.lock);
173 if(_threadpq.head == nil)
177 _threadpq.tail = &p->next;
178 unlock(&_threadpq.lock);