Skip to content

Commit

Permalink
WIP have time working correctly now
Browse files Browse the repository at this point in the history
  • Loading branch information
sigilante committed Oct 1, 2024
1 parent 047105a commit c7ceca4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
40 changes: 17 additions & 23 deletions pkg/noun/manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ _cm_signal_handle_intr(int x)
static void
_cm_signal_handle_alrm(int x)
{
stk_u->top = (timer*)NULL; // clear the timer stack
_cm_signal_handle(c3__alrm);
}

Expand Down Expand Up @@ -451,19 +452,16 @@ u3m_timer_clear()
void
u3m_timer_push(c3_w mil_w)
{
fprintf(stderr, "pushing timer %d\n", mil_w);
// get the request timer interval
struct itimerval itm_u;
timerclear(&itm_u.it_interval);
itm_u.it_value.tv_sec = (mil_w / 1000);
itm_u.it_value.tv_usec = 1000 * (mil_w % 1000);
fprintf(stderr, "interval: %d s %d us\n", itm_u.it_value.tv_sec, itm_u.it_value.tv_usec);

fprintf(stderr, "pushing timer for %u ms at %lx\r\n", mil_w, (c3_d)stk_u->top);

// does the stack have anything on it? if it's clean, this is easy:
fprintf(stderr, "checking stack\n");
fprintf(stderr, "checking stack %x\n", stk_u->top);
if (stk_u->top == NULL) {
fprintf(stderr, "no timer on stack\n");
// if not, set the timer and return
if ( rsignal_setitimer(ITIMER_VIRTUAL, &itm_u, 0) ) {
u3l_log("loom: push timer failed %s", strerror(errno));
Expand All @@ -473,11 +471,7 @@ u3m_timer_push(c3_w mil_w)
struct timeval tim_u;
gettimeofday(&tim_u, 0);

// debugging, TODO remove
c3_d tim_d = 1000000ULL * tim_u.tv_sec + tim_u.tv_usec;
fprintf(stderr, "expiry: %lx us\n", tim_d);

struct timer* new_u = (timer*)u3a_malloc(sizeof(timer));
struct timer* new_u = (timer*)c3_malloc(sizeof(timer));
new_u->wal_u = tim_u;
new_u->nex_u = stk_u->top;
stk_u->top = new_u;
Expand All @@ -492,7 +486,7 @@ u3m_timer_push(c3_w mil_w)

rsignal_getitimer(ITIMER_VIRTUAL, &cur_u);
// zero if no timer is set
fprintf(stderr, "current interval: %d s %d us\n", cur_u.it_value.tv_sec, cur_u.it_value.tv_usec);
fprintf(stderr, "current interval: %d s %d us\r\n", cur_u.it_value.tv_sec, cur_u.it_value.tv_usec);

if (timercmp(&cur_u.it_value, &itm_u.it_value, <)) {
u3l_log("loom: nest timer failed, too large for remaining time %s",
Expand All @@ -501,7 +495,6 @@ u3m_timer_push(c3_w mil_w)

// otherwise set the timer
struct itimerval rem_u;

timersub(&cur_u.it_value, &itm_u.it_value, &rem_u.it_value);

if ( rsignal_setitimer(ITIMER_VIRTUAL, &itm_u, 0) ) {
Expand All @@ -514,9 +507,9 @@ u3m_timer_push(c3_w mil_w)

// debugging, TODO remove
c3_d tim_d = 1000000ULL * tim_u.tv_sec + tim_u.tv_usec;
fprintf(stderr, "expiry: %lx us\n", tim_d);
fprintf(stderr, "expiry: %lx us\r\n", tim_d);
c3_d cur_d = cur_u.it_value.tv_sec * 1000 + cur_u.it_value.tv_usec / 1000;
fprintf(stderr, "remaining: %lx ms\n", cur_d);
fprintf(stderr, "remaining: %lx ms\r\n", cur_d);

struct timer* new_u = (timer*)u3a_malloc(sizeof(timer));
new_u->wal_u = __add_itimer(tim_u, cur_u);
Expand All @@ -532,17 +525,18 @@ u3m_timer_push(c3_w mil_w)
void
u3m_timer_pop()
{
fprintf(stderr, "popping timer at %lx\r\n", (c3_d)stk_u->top);
if (stk_u->top == NULL) {
u3l_log("loom: no \%jinx timer to pop");
u3m_timer_clear();
return;
}

// if (stk_u->top == NULL) {
// u3l_log("loom: no \%jinx timer to pop");
// }
else {
timer *old_u = stk_u->top;
stk_u->top = stk_u->top->nex_u;

if (stk_u->top == NULL) {
u3m_timer_clear();
return;
}
stk_u->top = old_u->nex_u;

struct timeval nex_u = stk_u->top->wal_u;
struct timeval tim_u;
Expand Down Expand Up @@ -2256,8 +2250,8 @@ u3m_init(size_t len_i)

// start %jinx timer stack
//
fprintf(stderr, "jinx: start\r\n");
// stk_u->top = (timer*)NULL;
stk_u = (struct timer_stack*)c3_malloc(sizeof(struct timer_stack));
stk_u->top = (timer*)NULL;
}

extern void u3je_secp_stop(void);
Expand Down
11 changes: 5 additions & 6 deletions pkg/noun/nock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1930,12 +1930,11 @@ _n_hint_fore(u3_cell hin, u3_noun bus, u3_noun* clu)
case c3__jinx: {
if (c3y == u3a_is_atom(*clu)) {
// clu is in Urbit time, but we need Unix time
c3_d tim_d;
if ( c3y == u3r_safe_chub(*clu, &tim_d) ) {
c3_w mil_w = u3_time_msc_out(tim_d);
fprintf(stderr, "u3t_jinx_time 2: %lld -> %u\n", tim_d, mil_w);
// u3m_timer_push(mil_w);
}
c3_d tim_d[2];
u3r_chubs(0, 2, &tim_d, *clu);
c3_w sec_w = tim_d[1];
c3_w mil_w = u3_time_msc_out(tim_d[0]);
u3m_timer_push(sec_w * 1000 + mil_w);
}
u3z(*clu);
*clu = c3__jinx;
Expand Down

0 comments on commit c7ceca4

Please sign in to comment.