summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrea Guzzo <andrea.guzzo@booking.com>2013-07-12 14:42:53 (GMT)
committer xant <xant@xant.net>2013-09-22 21:56:01 (GMT)
commit0027f1ab54f177ccf69c1e37569a9472123cdbe3 (patch)
tree10679bfaa2f369cef3ba81c8c05b6e2dd1e22f20
parent0da9b1e9a5f876b30b8d0eb2e48e3d58033bcef9 (diff)
use internal timers instead of node.js ones
performances are still low ... more work is needed here
-rw-r--r--core/JMXScript.mm3
-rw-r--r--core/JMXThreadedEntity.mm67
-rw-r--r--node.js/src/node.js2
3 files changed, 44 insertions, 28 deletions
diff --git a/core/JMXScript.mm b/core/JMXScript.mm
index 821a047..106913e 100644
--- a/core/JMXScript.mm
+++ b/core/JMXScript.mm
@@ -502,6 +502,7 @@ static v8::Handle<Value> SetInterval(const Arguments& args)
v8::String::Utf8Value statements(args[0]->ToString());
foo.statements = [NSString stringWithUTF8String:*statements];
} else {
+ v8::String::Utf8Value statements(args[0]->ToString());
foo.function = Persistent<Function>::New(Handle<Function>::Cast(args[0]));
foo.function->SetHiddenValue(String::New("lastUpdate"), v8::Number::New([[NSDate date] timeIntervalSince1970]));
foo.function->SetHiddenValue(String::New("interval"), args[1]);
@@ -753,7 +754,7 @@ static char *argv[2] = { (char *)"JMX", NULL };
// ctxTemplate->Set(String::New("quit"), FunctionTemplate::New(Quit));
ctxTemplate->Set(String::New("addToRunLoop"), FunctionTemplate::New(AddToRunLoop));
ctxTemplate->Set(String::New("removeFromRunLoop"), FunctionTemplate::New(RemoveFromRunLoop));
-#if 0
+#if 1
ctxTemplate->Set(String::New("setTimeout"), FunctionTemplate::New(SetTimeout));
ctxTemplate->Set(String::New("clearTimeout"), FunctionTemplate::New(ClearTimeout));
diff --git a/core/JMXThreadedEntity.mm b/core/JMXThreadedEntity.mm
index 71ffaf6..f74b361 100644
--- a/core/JMXThreadedEntity.mm
+++ b/core/JMXThreadedEntity.mm
@@ -236,42 +236,55 @@
[pool release];
}
+- (void)_do_tick
+{
+// NSThread *currentThread = [NSThread currentThread];
+ uint64_t timeStamp = CVGetCurrentHostTime();
+ [self tick:timeStamp];
+ previousTimeStamp = timeStamp;
+// uint64_t now = CVGetCurrentHostTime();
+// // Check if tick() has returned earlier and we still have time before next tick.
+// // If the current delta is smaller than our frequency, we will wait the difference
+// // between maxDelta and delta to honor the configured frequency.
+// // Otherwise, since we would be already late, we just skip the sleep time and
+// // go for the next frame.
+// uint64_t delta = now - timeStamp;
+// uint64_t sleepTime = (delta && delta < maxDelta) ? maxDelta - delta : 0;
+ //return sleepTime;
+}
+
- (void)run
{
NSThread *currentThread = [NSThread currentThread];
realEntity.active = YES;
+ [[NSOperationQueue mainQueue] addOperation:[NSO]]
while (![currentThread isCancelled]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+ //uint64_t sleepTime = [self _do_tick];
uint64_t maxDelta = 1e9 / [self.frequency doubleValue];
uint64_t timeStamp = CVGetCurrentHostTime();
- [self tick:timeStamp];
- previousTimeStamp = timeStamp;
- uint64_t now = CVGetCurrentHostTime();
- // Check if tick() has returned earlier and we still have time before next tick.
- // If the current delta is smaller than our frequency, we will wait the difference
- // between maxDelta and delta to honor the configured frequency.
- // Otherwise, since we would be already late, we just skip the sleep time and
- // go for the next frame.
- uint64_t delta = now - timeStamp;
+ uint64_t delta = timeStamp - previousTimeStamp;
uint64_t sleepTime = (delta && delta < maxDelta) ? maxDelta - delta : 0;
-
- if (sleepTime) {
- // using nanosleep is a good portable way, but since we are running
- // on OSX only, we should try relying on the NSThread API.
- // We will switch back to nanosleep if we notice that 'sleepForTimeInterval'
- // is not precise enough.
- struct timespec time = { 0, 0 };
- struct timespec remainder = { 0, static_cast<long>(sleepTime) };
- do {
- time.tv_sec = remainder.tv_sec;
- time.tv_nsec = remainder.tv_nsec;
- remainder.tv_nsec = 0;
- nanosleep(&time, &remainder);
- } while (remainder.tv_sec || remainder.tv_nsec);
- } else {
- // mmm ... no sleep time ... perhaps we are out of resources and slowing down mixing
- // TODO - produce a warning in this case
- }
+ [[NSRunLoop currentRunLoop] addTimer:[NSTimer timerWithTimeInterval:(double)(sleepTime/1e9) target:self selector:@selector(_do_tick) userInfo:nil repeats:NO] forMode:NSRunLoopCommonModes];
+ [[NSRunLoop currentRunLoop] run];
+
+// if (sleepTime) {
+// // using nanosleep is a good portable way, but since we are running
+// // on OSX only, we should try relying on the NSThread API.
+// // We will switch back to nanosleep if we notice that 'sleepForTimeInterval'
+// // is not precise enough.
+// struct timespec time = { 0, 0 };
+// struct timespec remainder = { 0, static_cast<long>(sleepTime) };
+// do {
+// time.tv_sec = remainder.tv_sec;
+// time.tv_nsec = remainder.tv_nsec;
+// remainder.tv_nsec = 0;
+// nanosleep(&time, &remainder);
+// } while (remainder.tv_sec || remainder.tv_nsec);
+// } else {
+// // mmm ... no sleep time ... perhaps we are out of resources and slowing down mixing
+// // TODO - produce a warning in this case
+// }
[pool drain];
}
realEntity.active = NO;
diff --git a/node.js/src/node.js b/node.js/src/node.js
index 88b68b0..270965f 100644
--- a/node.js/src/node.js
+++ b/node.js/src/node.js
@@ -172,6 +172,7 @@
_timeouts = new Array();
_intervals = new Array();
+/*
global.setTimeout = function() {
var t = NativeModule.require('timers');
timer = t.setTimeout.apply(this, arguments);
@@ -203,6 +204,7 @@
}
return t.clearInterval.apply(this, arguments);
};
+*/
global.clearAllTimers = function() {
for (i in _intervals)