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

void
_threaddebug(ulong l, char *fmt, ...)
{
	va_list arg;
	Tproc *p;
	int n;
	char buf[256];

	p = _threadgetproc();
	if ((l & _threaddebuglevel) == 0) return;
	if(p->curthread)
		n = pm_sprint(buf, "%d.%d ", p->pid, p->curthread->id);
	else
		n = pm_sprint(buf, "%d.nothread ", p->pid);

	va_start(arg, fmt);
	n = pm_doprint(buf+n, buf+sizeof(buf)-1, fmt, arg) - buf;
	va_end(arg);
	buf[n] = '\n';
	buf[n+1] = '\0';
	dwrite(buf);
}

void
_threadassert(char *s, char *file, int line)
{
	char buf[256];
	Tproc *p;

	p = _threadgetproc();
	if(p && p->curthread){
		pm_sprint(buf, "%d.%d ", p->pid, p->curthread->id);
		dwrite(buf);
	}
	if(file){
		pm_snprint(buf, sizeof buf, "%s:%d ", file, line);
		dwrite(buf);
	}
	pm_snprint(buf, sizeof(buf), "libthread: %s: assertion failed\n", s);
	dwrite(buf);
	abort();
}
