Check if hb is NULL.
[elinks.git] / src / ecmascript / spidermonkey / heartbeat.c
blobf71206a0a00e5c25d45a0d269439d065b7f3108e
1 /* The SpiderMonkey ECMAScript backend heartbeat fuctionality. */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <sys/time.h> /* setitimer(2) */
9 #include "elinks.h"
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. */
35 JSBool
36 heartbeat_callback(JSContext *ctx)
38 return JS_FALSE;
41 /* Callback for SIGVTALRM. Go through all heartbeats, decrease each
42 * one's TTL, and call JS_TriggerOperationCallback if a heartbeat's TTL
43 * goes to 0. */
44 static void
45 check_heartbeats(void *data)
47 struct heartbeat *hb;
49 foreach (hb, heartbeats) {
50 assert(hb->interpreter);
52 --hb->ttl;
54 if (hb->ttl <= 0) {
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. */
75 struct heartbeat *
76 add_heartbeat(struct ecmascript_interpreter *interpreter)
78 struct session *ses;
79 struct heartbeat *hb;
81 assert(interpreter);
83 if (!interpreter->vs || !interpreter->vs->doc_view)
84 ses = NULL;
85 else
86 ses = interpreter->vs->doc_view->session;
88 hb = mem_alloc(sizeof(struct heartbeat));
89 if (!hb) return NULL;
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
104 * the signal. */
105 install_signal_handler(SIGVTALRM, check_heartbeats, NULL, 1);
107 return hb;
110 /* Destroy the given heartbeat. */
111 void
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);
123 del_from_list(hb);
124 hb->interpreter->heartbeat = NULL;
125 mem_free(hb);