1 /* The SpiderMonkey ECMAScript backend heartbeat fuctionality. */
7 #include <sys/time.h> /* setitimer(2) */
11 #include "ecmascript/spidermonkey/util.h"
13 #include "config/options.h"
14 #include "document/view.h"
15 #include "ecmascript/ecmascript.h"
16 #include "ecmascript/spidermonkey.h"
17 #include "ecmascript/spidermonkey/heartbeat.h"
18 #include "osdep/signals.h"
19 #include "session/session.h"
20 #include "util/lists.h"
21 #include "util/math.h" /* int_upper_bound */
22 #include "util/memory.h"
23 #include "viewer/text/vs.h"
27 static INIT_LIST_OF(struct heartbeat
, heartbeats
);
29 static struct itimerval heartbeat_timer
= { { 1, 0 }, { 1, 0 } };
32 /* This callback is installed by JS_SetOperationCallback and triggered
33 * by JS_TriggerOperationCallback in the heartbeat code below. Returning
34 * JS_FALSE terminates script execution immediately. */
36 heartbeat_callback(JSContext
*ctx
)
41 /* Callback for SIGVTALRM. Go through all heartbeats, decrease each
42 * one's TTL, and call JS_TriggerOperationCallback if a heartbeat's TTL
45 check_heartbeats(void *data
)
49 foreach (hb
, heartbeats
) {
50 assert(hb
->interpreter
);
55 if (hb
->interpreter
->vs
56 && hb
->interpreter
->vs
->doc_view
57 && hb
->interpreter
->vs
->doc_view
->session
58 && hb
->interpreter
->vs
->doc_view
->session
->tab
59 && hb
->interpreter
->vs
->doc_view
->session
->tab
->term
) {
60 struct session
*ses
= hb
->interpreter
->vs
->doc_view
->session
;
61 struct terminal
*term
= ses
->tab
->term
;
62 int max_exec_time
= get_opt_int("ecmascript.max_exec_time", ses
);
64 ecmascript_timeout_dialog(term
, max_exec_time
);
67 JS_TriggerOperationCallback(hb
->interpreter
->backend_data
);
71 install_signal_handler(SIGVTALRM
, check_heartbeats
, NULL
, 1);
74 /* Create a new heartbeat for the given interpreter. */
76 add_heartbeat(struct ecmascript_interpreter
*interpreter
)
83 if (!interpreter
->vs
|| !interpreter
->vs
->doc_view
)
86 ses
= interpreter
->vs
->doc_view
->session
;
88 hb
= mem_alloc(sizeof(struct heartbeat
));
91 hb
->ttl
= get_opt_int("ecmascript.max_exec_time", ses
);
92 hb
->interpreter
= interpreter
;
94 add_to_list(heartbeats
, hb
);
96 /* Update the heartbeat timer. */
97 if (list_is_singleton(*hb
)) {
98 heartbeat_timer
.it_value
.tv_sec
= 1;
99 setitimer(ITIMER_VIRTUAL
, &heartbeat_timer
, NULL
);
102 /* We install the handler every call to add_heartbeat instead of only on
103 * module initialisation because other code may set other handlers for
105 install_signal_handler(SIGVTALRM
, check_heartbeats
, NULL
, 1);
110 /* Destroy the given heartbeat. */
112 done_heartbeat(struct heartbeat
*hb
)
114 if (!hb
) return; /* add_heartbeat returned NULL */
115 assert(hb
->interpreter
);
117 /* Stop the heartbeat timer if this heartbeat is the only one. */
118 if (list_is_singleton(*hb
)) {
119 heartbeat_timer
.it_value
.tv_sec
= 0;
120 setitimer(ITIMER_VIRTUAL
, &heartbeat_timer
, NULL
);
124 hb
->interpreter
->heartbeat
= NULL
;