#include <9pm/u.h>
#include <9pm/libc.h>
#include <9pm/ns.h>
#include <9pm/thread.h>

void
launcherpower(void (*f)(void *arg), void *arg)
{
	Thread *t;

	t = _threadgetthr();
	if(t->garbage){
		_freethread(t->garbage);
		t->garbage = nil;
	}
	(*f)(arg);
	threadexits(nil);
}

void
_threadinitstack(Thread *t, void (*f)(void*), void *arg)
{
	ulong *tos;

	tos = (ulong*)&t->stack[t->stacksize&~7];
	*--tos = (ulong)arg;
	*--tos = (ulong)f;
	t->env[JMPBUFPC] = (ulong)launcherpower+JMPBUFDPC;
	t->env[JMPBUFSP] = (ulong)tos - 8;		/* old PC and new PC */
}


syntax highlighted by Code2HTML, v. 0.9.1