-
Notifications
You must be signed in to change notification settings - Fork 200
/
v8js_timer.cc
158 lines (133 loc) · 4.54 KB
/
v8js_timer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| http://www.opensource.org/licenses/mit-license.php MIT License |
+----------------------------------------------------------------------+
| Author: Jani Taskinen <[email protected]> |
| Author: Patrick Reilly <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef _WIN32
#include <concrt.h>
#endif
#include "php_v8js_macros.h"
#include "v8js_v8.h"
#include "v8js_exceptions.h"
#include "v8js_timer.h"
extern "C" {
#include "ext/date/php_date.h"
#include "ext/standard/php_string.h"
#include "zend_interfaces.h"
#include "zend_closures.h"
#include "ext/spl/spl_exceptions.h"
#include "zend_exceptions.h"
}
static void v8js_timer_interrupt_handler(v8::Isolate *isolate, void *data) { /* {{{ */
zend_v8js_globals *globals = static_cast<zend_v8js_globals *>(data);
if (!globals->timer_stack.size()) {
return;
}
v8::Locker locker(isolate);
v8::HeapStatistics hs;
bool send_notification = false;
bool has_sent_notification = false;
do {
if (send_notification) {
isolate->LowMemoryNotification();
has_sent_notification = true;
}
isolate->GetHeapStatistics(&hs);
globals->timer_mutex.lock();
for (std::deque< v8js_timer_ctx* >::iterator it = globals->timer_stack.begin();
it != globals->timer_stack.end(); it ++) {
v8js_timer_ctx *timer_ctx = *it;
v8js_ctx *c = timer_ctx->ctx;
if(c->isolate != isolate || timer_ctx->killed) {
continue;
}
if (timer_ctx->memory_limit > 0 && hs.used_heap_size() > timer_ctx->memory_limit) {
if (has_sent_notification) {
timer_ctx->killed = true;
c->isolate->TerminateExecution();
c->memory_limit_hit = true;
} else {
// force garbage collection, then check again
send_notification = true;
break;
}
}
}
globals->timer_mutex.unlock();
} while(send_notification != has_sent_notification);
}
/* }}} */
void v8js_timer_thread(zend_v8js_globals *globals) /* {{{ */
{
while (!globals->timer_stop) {
globals->timer_mutex.lock();
if (globals->timer_stack.size()) {
v8js_timer_ctx *timer_ctx = globals->timer_stack.front();
v8js_ctx *c = timer_ctx->ctx;
std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
if(timer_ctx->killed) {
/* execution already terminated, nothing to check anymore,
* but wait for caller to pop this timer context. */
}
else if(timer_ctx->time_limit > 0 && now > timer_ctx->time_point) {
timer_ctx->killed = true;
c->isolate->TerminateExecution();
c->time_limit_hit = true;
}
else if (timer_ctx->memory_limit > 0) {
/* If a memory_limit is set, we need to interrupt execution
* and check heap size within the callback. We must *not*
* directly call GetHeapStatistics here, since we don't have
* a v8::Locker on the isolate, but are expected to hold one,
* and cannot aquire it as v8 is executing the script ... */
c->isolate->RequestInterrupt(v8js_timer_interrupt_handler, static_cast<void *>(globals));
}
}
globals->timer_mutex.unlock();
// Sleep for 10ms
#ifdef _WIN32
concurrency::wait(10);
#else
std::chrono::milliseconds duration(10);
std::this_thread::sleep_for(duration);
#endif
}
}
/* }}} */
void v8js_timer_push(long time_limit, size_t memory_limit, v8js_ctx *c) /* {{{ */
{
V8JSG(timer_mutex).lock();
// Create context for this timer
v8js_timer_ctx *timer_ctx = (v8js_timer_ctx *)emalloc(sizeof(v8js_timer_ctx));
// Calculate the time point when the time limit is exceeded
std::chrono::milliseconds duration(time_limit);
std::chrono::time_point<std::chrono::high_resolution_clock> from = std::chrono::high_resolution_clock::now();
// Push the timer context
timer_ctx->time_limit = time_limit;
timer_ctx->memory_limit = memory_limit;
timer_ctx->time_point = from + duration;
timer_ctx->ctx = c;
timer_ctx->killed = false;
V8JSG(timer_stack).push_front(timer_ctx);
V8JSG(timer_mutex).unlock();
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/