Permalink
Browse files

timer, feat: support beyond 2^31 delay (#357)

  • Loading branch information...
ngot authored and xicilion committed Sep 30, 2017
1 parent bab3531 commit 875654e4f08ea4af00df73f923713b149ef1dbbf
@@ -39,8 +39,8 @@ class global_base : public object_base {
static result_t clearInterval(Timer_base* t);
static result_t clearTimeout(Timer_base* t);
static result_t clearImmediate(Timer_base* t);
static result_t setInterval(v8::Local<v8::Function> callback, int32_t timeout, obj_ptr<Timer_base>& retVal);
static result_t setTimeout(v8::Local<v8::Function> callback, int32_t timeout, obj_ptr<Timer_base>& retVal);
static result_t setInterval(v8::Local<v8::Function> callback, double timeout, obj_ptr<Timer_base>& retVal);
static result_t setTimeout(v8::Local<v8::Function> callback, double timeout, obj_ptr<Timer_base>& retVal);
static result_t setImmediate(v8::Local<v8::Function> callback, obj_ptr<Timer_base>& retVal);
static result_t GC();
static result_t repl(v8::Local<v8::Array> cmds);
@@ -257,7 +257,7 @@ inline void global_base::s_setInterval(const v8::FunctionCallbackInfo<v8::Value>
METHOD_OVER(2, 2);
ARG(v8::Local<v8::Function>, 0);
ARG(int32_t, 1);
ARG(double, 1);
hr = setInterval(v0, v1, vr);
@@ -273,7 +273,7 @@ inline void global_base::s_setTimeout(const v8::FunctionCallbackInfo<v8::Value>&
METHOD_OVER(2, 2);
ARG(v8::Local<v8::Function>, 0);
ARG(int32_t, 1);
ARG(double, 1);
hr = setTimeout(v0, v1, vr);
@@ -26,8 +26,8 @@ class timers_base : public object_base {
static result_t clearInterval(Timer_base* t);
static result_t clearTimeout(Timer_base* t);
static result_t clearImmediate(Timer_base* t);
static result_t setInterval(v8::Local<v8::Function> callback, int32_t timeout, obj_ptr<Timer_base>& retVal);
static result_t setTimeout(v8::Local<v8::Function> callback, int32_t timeout, obj_ptr<Timer_base>& retVal);
static result_t setInterval(v8::Local<v8::Function> callback, double timeout, obj_ptr<Timer_base>& retVal);
static result_t setTimeout(v8::Local<v8::Function> callback, double timeout, obj_ptr<Timer_base>& retVal);
static result_t setImmediate(v8::Local<v8::Function> callback, obj_ptr<Timer_base>& retVal);
public:
@@ -123,7 +123,7 @@ inline void timers_base::s_setInterval(const v8::FunctionCallbackInfo<v8::Value>
METHOD_OVER(2, 2);
ARG(v8::Local<v8::Function>, 0);
ARG(int32_t, 1);
ARG(double, 1);
hr = setInterval(v0, v1, vr);
@@ -139,7 +139,7 @@ inline void timers_base::s_setTimeout(const v8::FunctionCallbackInfo<v8::Value>&
METHOD_OVER(2, 2);
ARG(v8::Local<v8::Function>, 0);
ARG(int32_t, 1);
ARG(double, 1);
hr = setTimeout(v0, v1, vr);
View
@@ -12,6 +12,8 @@
namespace fibjs {
#define TIMEOUT_MAX 2147483647 // 2^31-1
class JSTimer : public Timer {
public:
JSTimer(v8::Local<v8::Function> callback, int32_t timeout = 0, bool repeat = false)
@@ -69,9 +71,9 @@ result_t timers_base::clearImmediate(Timer_base* t)
return t->clear();
}
result_t timers_base::setInterval(v8::Local<v8::Function> callback, int32_t timeout, obj_ptr<Timer_base>& retVal)
result_t timers_base::setInterval(v8::Local<v8::Function> callback, double timeout, obj_ptr<Timer_base>& retVal)
{
if (timeout < 1)
if (timeout < 1 || timeout > TIMEOUT_MAX)
timeout = 1;
obj_ptr<Timer> timer = new JSTimer(callback, timeout, true);
@@ -81,9 +83,10 @@ result_t timers_base::setInterval(v8::Local<v8::Function> callback, int32_t time
return 0;
}
result_t timers_base::setTimeout(v8::Local<v8::Function> callback, int32_t timeout, obj_ptr<Timer_base>& retVal)
result_t timers_base::setTimeout(v8::Local<v8::Function> callback,
double timeout, obj_ptr<Timer_base>& retVal)
{
if (timeout < 1)
if (timeout < 1 || timeout > TIMEOUT_MAX)
timeout = 1;
obj_ptr<Timer> timer = new JSTimer(callback, timeout);
@@ -117,12 +120,12 @@ result_t global_base::clearImmediate(Timer_base* t)
return timers_base::clearImmediate(t);
}
result_t global_base::setInterval(v8::Local<v8::Function> callback, int32_t timeout, obj_ptr<Timer_base>& retVal)
result_t global_base::setInterval(v8::Local<v8::Function> callback, double timeout, obj_ptr<Timer_base>& retVal)
{
return timers_base::setInterval(callback, timeout, retVal);
}
result_t global_base::setTimeout(v8::Local<v8::Function> callback, int32_t timeout, obj_ptr<Timer_base>& retVal)
result_t global_base::setTimeout(v8::Local<v8::Function> callback, double timeout, obj_ptr<Timer_base>& retVal)
{
return timers_base::setTimeout(callback, timeout, retVal);
}
View
@@ -11,6 +11,15 @@ module global
/*! @brief Console Access Objects */
static console;
/*! @brief Process Objects*/
static process;
/*! @brief Worker Object,only available at Worker entry script*/
static readonly Worker Master;
/*! @brief global Object*/
static readonly Object global;
/*! @brief Run a script
@param fname Specifies the running path for script
@param argv Specify the run parameters, which can be caught in script by argv.
@@ -40,6 +49,47 @@ module global
/*! @brief Mandatory for garbage collection*/
static GC();
/*! @brief current script filename*/
static readonly String __filename;
/*! @brief current script directory*/
static readonly String __dirname;
/*! @brief cancels a timed, repeating action which was previously established by a call to setInterval()
@param t The identifier of the repeated action you want to cancel. This ID was returned by the corresponding call to setInterval().
*/
static clearInterval(Timer t);
/*! @brief cancels a timeout previously established by calling setTimeout().
@param t The identifier of the timeout you want to cancel. This ID was returned by the corresponding call to setTimeout().
*/
static clearTimeout(Timer t);
/*! @brief clears the action specified by setImmediate.
@param t immediateID is a ID returned by setImmediate.
*/
static clearImmediate(Timer t);
/*! @brief repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.
@param callback A function to be executed every delay milliseconds. The function is not passed any parameters, and no return value is expected.
@param timeout The time, in milliseconds (thousandths of a second), the timer should delay in between executions of the specified function or code.When greater than 2^31 , execute immediately
@return The returned timeoutID is a numeric, non-zero value which identifies the timer created by the call to setInterval(); this value can be passed to clearInterval() to cancel the timeout.
*/
static Timer setInterval(Function callback, Number timeout);
/*! @brief sets a timer which executes a function or specified piece of code once after the timer expires.
@param callback A function to be executed after the timer expires.
@param timeout The time, in milliseconds (thousandths of a second), the timer should wait before the specified function or code is executed.When greater than 2^31 , execute immediately
@return The returned timeoutID is a positive integer value which identifies the timer created by the call to setTimeout(); this value can be passed to clearTimeout() to cancel the timeout.
*/
static Timer setTimeout(Function callback, Number timeout);
/*! @brief run a callback function immediately after the browser has completed other operations such as events and display updates.
@param callback the function you wish to call.
@return where immediateID is the ID of the immediate which can be used later with clearImmediate.
*/
static Timer setImmediate(Function callback);
/*! @brief Enter the interactive mode, you can interactively execute the internal commands and code which can only be referenced when js start.
@param cmds Add commands in the following format:
@code
View
@@ -0,0 +1,38 @@
/*! @brief timers module */
module timers
{
/*! @brief cancels a timed, repeating action which was previously established by a call to setInterval()
@param t The identifier of the repeated action you want to cancel. This ID was returned by the corresponding call to setInterval().
*/
static clearInterval(Timer t);
/*! @brief cancels a timeout previously established by calling setTimeout().
@param t The identifier of the timeout you want to cancel. This ID was returned by the corresponding call to setTimeout().
*/
static clearTimeout(Timer t);
/*! @brief clears the action specified by setImmediate.
@param t immediateID is a ID returned by setImmediate.
*/
static clearImmediate(Timer t);
/*! @brief repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.
@param callback A function to be executed every delay milliseconds. The function is not passed any parameters, and no return value is expected.
@param timeout The time, in milliseconds (thousandths of a second), the timer should delay in between executions of the specified function or code.When greater than 2^31 , execute immediately
@return The returned timeoutID is a numeric, non-zero value which identifies the timer created by the call to setInterval(); this value can be passed to clearInterval() to cancel the timeout.
*/
static Timer setInterval(Function callback, Number timeout);
/*! @brief sets a timer which executes a function or specified piece of code once after the timer expires.
@param callback A function to be executed after the timer expires.
@param timeout The time, in milliseconds (thousandths of a second), the timer should wait before the specified function or code is executed.When greater than 2^31 , execute immediately
@return The returned timeoutID is a positive integer value which identifies the timer created by the call to setTimeout(); this value can be passed to clearTimeout() to cancel the timeout.
*/
static Timer setTimeout(Function callback, Number timeout);
/*! @brief run a callback function immediately after the browser has completed other operations such as events and display updates.
@param callback the function you wish to call.
@return where immediateID is the ID of the immediate which can be used later with clearImmediate.
*/
static Timer setImmediate(Function callback);
};
View
@@ -11,7 +11,7 @@ module global
/*! @brief 控制台访问对象 */
static console;
/*! @brief 控制台访问对象 */
/*! @brief 进程对象 */
static process;
/*! @brief Worker 宿主对象,仅在 Worker 入口脚本有效 */
@@ -114,17 +114,17 @@ module global
/*! @brief 每间隔指定的时间后调用函数
@param callback 指定回调函数
@param timeout 指定间隔的时间,以毫秒为单位
@param timeout 指定间隔的时间,以毫秒为单位。超过 2^31 的话,立即执行。
@return 返回定时器对象
*/
static Timer setInterval(Function callback, Integer timeout);
static Timer setInterval(Function callback, Number timeout);
/*! @brief 在指定的时间后调用函数
@param callback 指定回调函数
@param timeout 指定延时的时间,以毫秒为单位
@param timeout 指定延时的时间,以毫秒为单位。超过 2^31 的话,立即执行。
@return 返回定时器对象
*/
static Timer setTimeout(Function callback, Integer timeout);
static Timer setTimeout(Function callback, Number timeout);
/*! @brief 下一个空闲时间立即执行回调函数
@param callback 指定回调函数
View
@@ -18,17 +18,17 @@ module timers
/*! @brief 每间隔指定的时间后调用函数
@param callback 指定回调函数
@param timeout 指定间隔的时间,以毫秒为单位
@param timeout 指定间隔的时间,以毫秒为单位。超过 2^31 的话,立即执行。
@return 返回定时器对象
*/
static Timer setInterval(Function callback, Integer timeout);
static Timer setInterval(Function callback, Number timeout);
/*! @brief 在指定的时间后调用函数
@param callback 指定回调函数
@param timeout 指定延时的时间,以毫秒为单位
@return 返回定时器对象
@param callback 指定回调函数
@param timeout 指定延时的时间,以毫秒为单位。超过 2^31 的话,立即执行。
@return 返回定时器对象
*/
static Timer setTimeout(Function callback, Integer timeout);
static Timer setTimeout(Function callback, Number timeout);
/*! @brief 下一个空闲时间立即执行回调函数
@param callback 指定回调函数
View
@@ -358,7 +358,7 @@ describe("net", () => {
var no = test_util.countObject('Timer');
c1.connect('127.0.0.1', 8085 + base_port);
for (var i = 0; i < 1000 && no !== test_util.countObject('Timer'); i++)
coroutine.sleep(1);
coroutine.sleep(50);
assert.equal(no, test_util.countObject('Timer'));
var t1 = new Date();
Oops, something went wrong.

0 comments on commit 875654e

Please sign in to comment.