code: mafs

ref: 56bd332994102b4e194819abb5e087a8895452f9
dir: /misc.c/

View raw version
#include <u.h>
#include <libc.h>
#include "dat.h"
#include "fns.h"

void
chkrunlock(RWLock *q)
{
	if(q->readers <= 0)
		print("runlock(%#p): not locked: pc %#p\n",
			q, getcallerpc(&q));
	else
		runlock(q);
}

u8
chkwunlock(RWLock *q)
{
	if(q->writer <= 0){
		panic("wunlock(%#p): not locked: pc %#p\n",
			q, getcallerpc(&q));
		return 0;
	}
	wunlock(q);
	return 1;
}

void
chkqunlock(QLock *q)
{
	if(q->locked == 0)
		print("qunlock(%#p): not locked: pc %#p\n",
			q, getcallerpc(&q));
	else
		qunlock(q);
}

int
dprint(char *fmt, ...)
{
	static char buf[2048];
	static QLock lk;
	va_list va;
	int rc;

	qlock(&lk);
	va_start(va, fmt);
	snprint(buf, 2048, "%d %llud: %s", getpid(), nsec(), fmt);
	rc = vfprint(2, buf, va);
	va_end(va);
	qunlock(&lk);
	return rc;
}

void
panic(char *fmt, ...)
{
	char buf[8192], *s;
	va_list arg;


	s = buf;
	s += sprint(s, "%s %d: ", argv0, getpid());
	va_start(arg, fmt);
	s = vseprint(s, buf + sizeof(buf) / sizeof(*buf), fmt, arg);
	va_end(arg);
	*s++ = '\n';
	write(2, buf, s - buf);
	abort();
	exits(buf);
}