Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add %jinx hint to Vere. #648

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/c3/motes.h
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@
# define c3__jato c3_s4('j','a','t','o')
# define c3__jet c3_s3('j','e','t')
# define c3__jetd c3_s4('j','e','t','d')
# define c3__jinx c3_s4('j','i','n','x')
# define c3__just c3_s4('j','u','s','t')
# define c3__k c3_s1('k')
# define c3__khan c3_s4('k','h','a','n')
Expand Down
26 changes: 25 additions & 1 deletion pkg/noun/manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,20 @@ _cm_signal_deep(c3_w mil_w)
);
}

u3m_timer_set(mil_w);

// factor into own function, call here and in _n_hint_fore() TODO
// _n_hint_hind() will look like this w/ deinstall handler instead
// return void b/c just clearing timer, no args

u3t_boot();
}

/* u3m_timer_set
*/
void
u3m_timer_set(c3_w mil_w)
{
if ( mil_w ) {
struct itimerval itm_u;

Expand All @@ -381,8 +395,18 @@ _cm_signal_deep(c3_w mil_w)
rsignal_install_handler(SIGVTALRM, _cm_signal_handle_alrm);
}
}
}

u3t_boot();
/* u3m_timer_clear
*/
void
u3m_timer_clear()
{
struct itimerval itm_u;

timerclear(&itm_u.it_interval);

rsignal_deinstall_handler(SIGVTALRM);
}

/* _cm_signal_done():
Expand Down
10 changes: 10 additions & 0 deletions pkg/noun/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,14 @@
c3_w
u3m_pack(void);

/* u3m_timer_set: set the timer.
*/
void
u3m_timer_set(c3_w mil_w);

/* u3m_timer_clear: clear the timer.
*/
void
u3m_timer_clear(void);

#endif /* ifndef U3_MANAGE_H */
28 changes: 27 additions & 1 deletion pkg/noun/nock.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "xtract.h"
#include "zave.h"

#include <stdio.h>

// define to have each opcode printed as it executes,
// along with some other debugging info
# undef VERBOSE_BYTECODE
Expand Down Expand Up @@ -1060,6 +1062,7 @@ _n_bint(u3_noun* ops, u3_noun hif, u3_noun nef, c3_o los_o, c3_o tel_o)
case c3__meme:
case c3__nara:
case c3__hela:
case c3__jinx:
case c3__bout: {
u3_noun fen = u3_nul;
c3_w nef_w = _n_comp(&fen, nef, los_o, c3n);
Expand Down Expand Up @@ -1924,6 +1927,26 @@ _n_hint_fore(u3_cell hin, u3_noun bus, u3_noun* clu)
*clu = u3nt(u3k(tag), *clu, now);
} break;

case c3__jinx: {
if (c3y == u3a_is_atom(*clu)) {
// clu is in Urbit time, but we need Unix time
mpz_t clu_mp;
u3r_mp(clu_mp, *clu);
mpz_t urs_mp, tim_mp;
mpz_init(urs_mp);
mpz_init(tim_mp);
mpz_tdiv_q_2exp(tim_mp, clu_mp, 48);
mpz_mul_ui(tim_mp, tim_mp, 1000);
mpz_tdiv_q_2exp(urs_mp, tim_mp, 16);
c3_w mil_w = u3i_mp(urs_mp);
u3m_timer_set(mil_w); // set ITIMER (mil_w is in microseconds)
mpz_clear(clu_mp);
mpz_clear(tim_mp);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace this math with u3_time_msc_out(c3_d).

c3_d tim_d;
if ( c3y == u3r_safe_chub(*clu, &tim_d) ) {
  c3_w mic_w = u3_time_msc_out(tim_d);
  c3_w mil_w = mic_w / 1000;
  u3m_timer_set(mil_w);
}

(Or you could, if it was defined in pkg/vere. I'd be inclined to copy it into this file as a static function rather than use gmp.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Urbit time is bigger than that, though, so the u3r_safe_chub always fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could retrieve two chubs for the time, very unlikely to be too big for that.

u3z(*clu);
*clu = c3__jinx;
} break;

case c3__nara: {
u3_noun pri, tan;
if ( c3y == u3r_cell(*clu, &pri, &tan) ) {
Expand Down Expand Up @@ -1985,7 +2008,10 @@ static void
_n_hint_hind(u3_noun tok, u3_noun pro)
{
u3_noun p_tok, q_tok, r_tok;
if ( (c3y == u3r_trel(tok, &p_tok, &q_tok, &r_tok)) && (c3__bout == p_tok) ) {
if (c3__jinx == tok) {
u3m_timer_clear();
}
else if ( (c3y == u3r_trel(tok, &p_tok, &q_tok, &r_tok)) && (c3__bout == p_tok) ) {
// get the microseconds elapsed
u3_atom delta = u3ka_sub(u3i_chub(u3t_trace_time()), u3k(r_tok));

Expand Down
Loading