2015-06-24 22:50:30 +03:00
|
|
|
/* v/behn.c
|
2014-10-15 06:25:23 +04:00
|
|
|
**
|
|
|
|
** This file is in the public domain.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <gmp.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <uv.h>
|
|
|
|
#include <curses.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <term.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "all.h"
|
2015-06-24 04:28:38 +03:00
|
|
|
#include "vere/vere.h"
|
2014-10-15 06:25:23 +04:00
|
|
|
|
2015-06-24 22:50:30 +03:00
|
|
|
/* u3_behn(): initialize time timer.
|
2014-10-15 06:25:23 +04:00
|
|
|
*/
|
|
|
|
void
|
2015-06-24 22:50:30 +03:00
|
|
|
u3_behn_io_init(void)
|
2014-10-15 06:25:23 +04:00
|
|
|
{
|
2015-06-24 22:50:30 +03:00
|
|
|
u3_behn* teh_u = &u3_Host.teh_u;
|
2014-10-15 06:25:23 +04:00
|
|
|
|
2014-11-04 23:22:33 +03:00
|
|
|
uv_timer_init(u3L, &teh_u->tim_u);
|
2014-11-05 04:18:47 +03:00
|
|
|
teh_u->alm = c3n;
|
2014-10-15 06:25:23 +04:00
|
|
|
}
|
|
|
|
|
2015-06-24 22:50:30 +03:00
|
|
|
/* u3_behn_io_exit(): terminate timer.
|
2014-10-15 06:25:23 +04:00
|
|
|
*/
|
|
|
|
void
|
2015-06-24 22:50:30 +03:00
|
|
|
u3_behn_io_exit(void)
|
2014-10-15 06:25:23 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-06-24 22:50:30 +03:00
|
|
|
/* _behn_time_cb(): timer callback.
|
2014-10-15 06:25:23 +04:00
|
|
|
*/
|
|
|
|
static void
|
2015-06-24 22:50:30 +03:00
|
|
|
_behn_time_cb(uv_timer_t* tim_u)
|
2014-10-15 06:25:23 +04:00
|
|
|
{
|
2018-10-24 00:34:10 +03:00
|
|
|
/* there was an exponential backoff on timers, which meant that if you set a
|
|
|
|
* timer for every ~s1 inside urbit, each iteration would be slightly
|
|
|
|
* slower, up until ~25 timers had fired, at which point the exponential
|
|
|
|
* curve really kicked in and you wouldn't get a callback for ~m1 when you
|
|
|
|
* requested one in ~s1. nobody in the office knows why this is the way it
|
|
|
|
* is, and it goes back to the original behn code from 2014.
|
|
|
|
*/
|
|
|
|
/* u3_behn* teh_u = &u3_Host.teh_u;
|
|
|
|
* if(teh_u->run_w < 1024) {
|
|
|
|
* teh_u->run_w++;
|
|
|
|
* }
|
|
|
|
*/
|
2014-10-15 06:25:23 +04:00
|
|
|
|
2014-11-04 23:22:33 +03:00
|
|
|
u3_lo_open();
|
2014-10-15 06:25:23 +04:00
|
|
|
{
|
2014-11-06 03:20:01 +03:00
|
|
|
u3v_plan
|
2015-06-24 22:50:30 +03:00
|
|
|
(u3nt(u3_blip, c3__behn, u3_nul),
|
2014-11-04 23:22:33 +03:00
|
|
|
u3nc(c3__wake, u3_nul));
|
2014-10-15 06:25:23 +04:00
|
|
|
}
|
2014-11-05 04:18:47 +03:00
|
|
|
u3_lo_shut(c3n);
|
2014-10-15 06:25:23 +04:00
|
|
|
}
|
|
|
|
|
2015-06-24 22:50:30 +03:00
|
|
|
/* u3_behn_io_poll(): update behn IO state.
|
2014-10-15 06:25:23 +04:00
|
|
|
*/
|
|
|
|
void
|
2015-06-24 22:50:30 +03:00
|
|
|
u3_behn_io_poll(void)
|
2014-10-15 06:25:23 +04:00
|
|
|
{
|
2015-06-24 22:50:30 +03:00
|
|
|
u3_behn* teh_u = &u3_Host.teh_u;
|
|
|
|
u3_noun wen = u3v_keep(u3nt(u3_blip, c3__behn, u3_nul));
|
2014-10-15 06:25:23 +04:00
|
|
|
|
2014-11-04 23:22:33 +03:00
|
|
|
if ( (u3_nul != wen) &&
|
2014-11-05 04:18:47 +03:00
|
|
|
(c3y == u3du(wen)) &&
|
|
|
|
(c3y == u3ud(u3t(wen))) )
|
2014-10-15 06:25:23 +04:00
|
|
|
{
|
2014-11-04 23:22:33 +03:00
|
|
|
c3_d gap_d = u3_time_gap_ms(u3k(u3A->now), u3k(u3t(wen)));
|
2014-10-15 06:25:23 +04:00
|
|
|
|
2015-01-08 00:26:05 +03:00
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "gap_d %llu, plus %llu\r\n",
|
|
|
|
gap_d, gap_d + (c3_d)teh_u->run_w);
|
|
|
|
#endif
|
2014-10-15 06:25:23 +04:00
|
|
|
gap_d += teh_u->run_w;
|
|
|
|
|
2014-11-05 04:18:47 +03:00
|
|
|
if ( c3y == teh_u->alm ) {
|
2014-10-15 06:25:23 +04:00
|
|
|
uv_timer_stop(&teh_u->tim_u);
|
|
|
|
}
|
2014-11-05 04:18:47 +03:00
|
|
|
else teh_u->alm = c3y;
|
2014-10-15 06:25:23 +04:00
|
|
|
|
2015-06-24 22:50:30 +03:00
|
|
|
uv_timer_start(&teh_u->tim_u, _behn_time_cb, gap_d, 0);
|
2014-10-15 06:25:23 +04:00
|
|
|
}
|
|
|
|
else {
|
2014-11-05 04:18:47 +03:00
|
|
|
if ( c3y == teh_u->alm ) {
|
2014-10-15 06:25:23 +04:00
|
|
|
uv_timer_stop(&teh_u->tim_u);
|
|
|
|
}
|
2014-11-05 04:18:47 +03:00
|
|
|
teh_u->alm = c3n;
|
2014-10-15 06:25:23 +04:00
|
|
|
}
|
2014-11-04 23:22:33 +03:00
|
|
|
u3z(wen);
|
2014-10-15 06:25:23 +04:00
|
|
|
}
|