Plan 9 from Bell Labs’s /usr/web/sources/contrib/axel/8021x/v210/timer.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <thread.h>
#include <bio.h>
#include <ip.h>
#include <mp.h>
#include <libsec.h>
#include <auth.h>
#include "dat.h"
#include "fns.h"

void
initTimers(Timers *s)
{
	if (s == nil)
		return;

	s->timer = nil;
	s->ntimer = 0;
	s->timerchan = chancreate(sizeof(int), 0);
}

Timer*
addTimer(Timers *s, char *name)
{
	Timer *t;

	if (s == nil)
		return nil;

	s->timer = realloc(s->timer, sizeof(Timer*)*(s->ntimer+1));
	if (s == nil)
		logfatal(0, "cannot reallocate Timers");

	t = malloc(sizeof(Timer));
	if (t == nil)
		logfatal(0, "cannot allocate Timer");

	loglog("addTimer %s", name);
	t->counter = 0;
	t->name = strdup(name);
	t->state = Off;
	s->timer[s->ntimer] = t;
	s->ntimer++;

	return t;
}

void
startTimer(Timer *t, int val)
{
	if (t == nil)
		return;

	loglog("startTimer %s to %d", t->name, val);
	if (t->state == Ticking)
		logall("startTimer oops: %s runs, val=%d", t->name, t->counter/TickPerSec);
	t->state = Ticking;
	t->counter = val * TickPerSec;
}

void
resetTimer(Timer *t)
{
	if (t == nil)
		return;

	loglog("resetTimer %s (val was %d)", t->name, t->counter/TickPerSec);
	if (t->state == Off)
		logall("startTimer oops: %s already off", t->name);
	t->state = Off;
	t->counter = 0;
}

void
tickTimer(Timer *t)
{
	if (t == nil)
		return;

	if (t->state == Ticking && t->counter > 0)
		t->counter--;
	else if (t->state != Ticking)
		logall("tickTimer oops: %s is not ticking, val=%d", t->name, t->counter/TickPerSec);
}

int
timerVal(Timer *t)
{
	if (t == nil)
		return -1;

	return t->counter/TickPerSec;
}

void
tickproc(void *arg)
{
	Timers *s;

	s = arg;

	for(;;) {
//		loglog("tickproc timer=%p ntimer=%d", s->timer, s->ntimer);
		sleep(Tick);
		send(s->timerchan, nil);  // was nbsend
	}
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].