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

int
threadid(void)
{
	return _threadgetthr()->id;
}

int
threadpid(int id)
{
	Tproc *pp;
	Thread *t;

	if (id < 0)
		return id;
	if (id == 0)
		return _threadgetproc()->pid;
	lock(&_threadpq.lk);
	for (pp = _threadpq.head; pp->next; pp = pp->next)
		for (t = pp->threads.head; t; t = t->nextt)
			if (t->id == id) {
				unlock(&_threadpq.lk);
				return pp->pid;
			}
	unlock(&_threadpq.lk);
	return -1;
}

int
threadsetgrp(int ng)
{
	int og;
	Thread *t;

	t = _threadgetthr();
	og = t->grp;
	t->grp = ng;
	return og;
}

int
threadgetgrp(void)
{
	return _threadgetthr()->grp;
}

void
threadsetname(char *name)
{
	Thread *t = _threadgetthr();

	if (t->cmdname)
		free(t->cmdname);
	t->cmdname = strdup(name);
}

char*
threadgetname(void)
{
	return _threadgetthr()->cmdname;
}

ulong*
threaddata(void)
{
	return &_threadgetthr()->udata;
}

ulong*
procdata(void)
{
	return &_threadgetproc()->udata;
}

void
threadnowait(void)
{
	_threadgetproc()->nowait = 1;
}

Thread*
_threadgetthr(void)
{
	return _threadgetproc()->curthread;
}


