#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <sys/neutrino.h>
#include <sys/syspage.h>


static intrspin_t			spinlock;
static unsigned volatile	sec;


static const struct sigevent *isr(void *data, int id) {
	struct sigevent  *event = data;
	static unsigned  cnt;

	if((++cnt % 1000) == 0) {
		InterruptLock(&spinlock);
		sec = cnt / 1000;
		InterruptUnlock(&spinlock);
		return event;
	}

	return NULL;
}

int main(int argc, char **argv) {
	struct sigevent  event;
	unsigned  s;
	int  id;

//	memset(&spinlock, 1, sizeof spinlock);
	memset(&spinlock, 0, sizeof spinlock);
	SIGEV_INTR_INIT(&event);
	ThreadCtl(_NTO_TCTL_IO, NULL);

	if(-1 == (id = InterruptAttach(SYSPAGE_ENTRY(qtime)->intr, isr, &event, sizeof event, _NTO_INTR_FLAGS_TRK_MSK))) {
		err(EXIT_FAILURE, "InterruptAttach()");
	}

	while(1) {
		if(-1 == InterruptWait(0, NULL)) {
			err(EXIT_FAILURE, "InterruptWait()");
		}

		InterruptLock(&spinlock);
		s = sec;
		InterruptUnlock(&spinlock);
		printf("%u\n", s);
	}
}
