1 #include "threadimpl.h"
6 static int nextID(void);
9 * Create and initialize a new Thread structure attached to a given proc.
12 typedef struct Stack Stack;
17 uchar buf[STKSIZE-12];
20 static Stack *stkfree;
45 while(stkfree == nil){
47 assert(STKSIZE == sizeof(Stack));
48 buf = malloc(STKSIZE+128*STKSIZE);
49 s = (Stack*)(((ulong)buf+STKSIZE)&~(STKSIZE-1));
55 stkfree = stkfree->next;
62 newthread(Proc *p, void (*f)(void *arg), void *arg, uint stacksize, char *name, int grp)
69 sysfatal("bad stacksize %d", stacksize);
70 t = _threadmalloc(sizeof(Thread), 1);
75 _threaddebugmemset(s->buf, 0xFE, sizeof s->buf);
76 _threadinitstack(t, f, arg);
80 t->cmdname = strdup(name);
83 t->next = (Thread*)~0;
84 _threaddebug(DBGSCHED, "create thread %d.%d name %s", p->pid, t->id, name);
87 if(p->threads.head == nil)
90 t->prevt = p->threads.tail;
114 procrfork(void (*f)(void *), void *arg, uint stacksize, int rforkflag)
119 p = _threadgetproc();
120 assert(p->newproc == nil);
121 p->newproc = _newproc(f, arg, stacksize, nil, p->thread->grp, rforkflag);
122 id = p->newproc->threads.head->id;
128 proccreate(void (*f)(void*), void *arg, uint stacksize)
130 return procrfork(f, arg, stacksize, 0);
138 for(t = p->threads.head; t; t = nextt){
141 assert(t->stk != nil);
142 _stackfree((Stack*)t->stk);
150 * Create a new thread and schedule it to run.
151 * The thread grp is inherited from the currently running thread.
154 threadcreate(void (*f)(void *arg), void *arg, uint stacksize)
156 return newthread(_threadgetproc(), f, arg, stacksize, nil, threadgetgrp());
160 * Create and initialize a new Proc structure with a single Thread
161 * running inside it. Add the Proc to the global process list.
164 _newproc(void (*f)(void *arg), void *arg, uint stacksize, char *name, int grp, int rforkflag)
168 p = _threadmalloc(sizeof *p, 1);
170 p->rforkflag = rforkflag;
171 newthread(p, f, arg, stacksize, name, grp);
173 lock(&_threadpq.lock);
174 if(_threadpq.head == nil)
178 _threadpq.tail = &p->next;
179 unlock(&_threadpq.lock);