From c6136ec3d2b6c19bbaca5c3a84af36c2c339dd38 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Mon, 28 Sep 2015 22:03:24 +0200 Subject: [PATCH] Fix globals ptr passing wrt. timer thread --- php_v8js_macros.h | 5 +++-- v8js_class.cc | 4 ++-- v8js_timer.cc | 35 ++++++++++++++--------------------- v8js_timer.h | 2 +- v8js_v8.cc | 2 +- 5 files changed, 21 insertions(+), 27 deletions(-) diff --git a/php_v8js_macros.h b/php_v8js_macros.h index 04d5ee5..ca5e1d0 100644 --- a/php_v8js_macros.h +++ b/php_v8js_macros.h @@ -42,7 +42,6 @@ extern "C" { #include "v8js_class.h" #include "v8js_v8.h" -#include "v8js_timer.h" #ifndef PATH_MAX /* Some platforms (Windows among others) don't have a PATH_MAX, for the moment @@ -99,7 +98,9 @@ void v8js_accessor_ctx_dtor(v8js_accessor_ctx * TSRMLS_DC); /* Register accessors into passed object */ void v8js_register_accessors(std::vector *accessor_list, v8::Local, zval *, v8::Isolate * TSRMLS_DC); -/* Resource declaration */ + +/* Forward declarations */ +struct v8js_timer_ctx; /* Module globals */ ZEND_BEGIN_MODULE_GLOBALS(v8js) diff --git a/v8js_class.cc b/v8js_class.cc index 01b7cc4..b40aaf0 100644 --- a/v8js_class.cc +++ b/v8js_class.cc @@ -701,7 +701,7 @@ static PHP_METHOD(V8Js, setTimeLimit) if (c->in_execution && time_limit && !V8JSG(timer_thread)) { /* If timer thread is not started already and we now impose a time limit * finally install the timer. */ - V8JSG(timer_thread) = new std::thread(v8js_timer_thread TSRMLS_CC); + V8JSG(timer_thread) = new std::thread(v8js_timer_thread, ZEND_MODULE_GLOBALS_BULK(v8js)); } } /* }}} */ @@ -732,7 +732,7 @@ static PHP_METHOD(V8Js, setMemoryLimit) if (c->in_execution && memory_limit && !V8JSG(timer_thread)) { /* If timer thread is not started already and we now impose a memory limit * finally install the timer. */ - V8JSG(timer_thread) = new std::thread(v8js_timer_thread TSRMLS_CC); + V8JSG(timer_thread) = new std::thread(v8js_timer_thread, ZEND_MODULE_GLOBALS_BULK(v8js)); } } /* }}} */ diff --git a/v8js_timer.cc b/v8js_timer.cc index 4ce5663..c83a51e 100644 --- a/v8js_timer.cc +++ b/v8js_timer.cc @@ -28,14 +28,12 @@ extern "C" { #include "php_v8js_macros.h" #include "v8js_v8.h" #include "v8js_exceptions.h" +#include "v8js_timer.h" static void v8js_timer_interrupt_handler(v8::Isolate *isolate, void *data) { /* {{{ */ -#ifdef ZTS - // @fixme - //TSRMLS_D = (void ***) data; -#endif + zend_v8js_globals *globals = static_cast(data); - if (!V8JSG(timer_stack).size()) { + if (!globals->timer_stack.size()) { return; } @@ -43,10 +41,10 @@ static void v8js_timer_interrupt_handler(v8::Isolate *isolate, void *data) { /* v8::HeapStatistics hs; isolate->GetHeapStatistics(&hs); - V8JSG(timer_mutex).lock(); + globals->timer_mutex.lock(); - for (std::deque< v8js_timer_ctx* >::iterator it = V8JSG(timer_stack).begin(); - it != V8JSG(timer_stack).end(); it ++) { + 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; @@ -61,17 +59,17 @@ static void v8js_timer_interrupt_handler(v8::Isolate *isolate, void *data) { /* } } - V8JSG(timer_mutex).unlock(); + globals->timer_mutex.unlock(); } /* }}} */ -void v8js_timer_thread(TSRMLS_D) /* {{{ */ +void v8js_timer_thread(zend_v8js_globals *globals) /* {{{ */ { - while (!V8JSG(timer_stop)) { + while (!globals->timer_stop) { - V8JSG(timer_mutex).lock(); - if (V8JSG(timer_stack).size()) { - v8js_timer_ctx *timer_ctx = V8JSG(timer_stack).front(); + 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 now = std::chrono::high_resolution_clock::now(); @@ -90,15 +88,10 @@ void v8js_timer_thread(TSRMLS_D) /* {{{ */ * 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 ... */ - void *data = NULL; -#ifdef ZTS - // @fixme - //data = (void *) TSRMLS_C; -#endif - c->isolate->RequestInterrupt(v8js_timer_interrupt_handler, data); + c->isolate->RequestInterrupt(v8js_timer_interrupt_handler, static_cast(globals)); } } - V8JSG(timer_mutex).unlock(); + globals->timer_mutex.unlock(); // Sleep for 10ms #ifdef _WIN32 diff --git a/v8js_timer.h b/v8js_timer.h index 61bbc83..b1b1d24 100644 --- a/v8js_timer.h +++ b/v8js_timer.h @@ -24,7 +24,7 @@ struct v8js_timer_ctx bool killed; }; -void v8js_timer_thread(TSRMLS_D); +void v8js_timer_thread(zend_v8js_globals *globals); void v8js_timer_push(long time_limit, long memory_limit, v8js_ctx *c TSRMLS_DC); #endif /* V8JS_TIMER_H */ diff --git a/v8js_v8.cc b/v8js_v8.cc index 8401060..cc046e3 100644 --- a/v8js_v8.cc +++ b/v8js_v8.cc @@ -98,7 +98,7 @@ void v8js_v8_call(v8js_ctx *c, zval **return_value, // If timer thread is not running then start it if (!V8JSG(timer_thread)) { // If not, start timer thread - V8JSG(timer_thread) = new std::thread(v8js_timer_thread TSRMLS_CC); + V8JSG(timer_thread) = new std::thread(v8js_timer_thread, ZEND_MODULE_GLOBALS_BULK(v8js)); } }