aboutsummaryrefslogtreecommitdiff
path: root/src/library.js
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-11-15 18:35:41 -0800
committerAlon Zakai <alonzakai@gmail.com>2013-11-15 18:35:41 -0800
commitb8ac3d2a175799678691bd123d7e1cb1d42b46f8 (patch)
treeea304e167568975da6ed9387090b5e1cc107b884 /src/library.js
parent6aff04516ecb627d7f8919b5d9dc082c3f9ceba8 (diff)
precise clock_gettime when not using CLOCK_REALTIME; fixes #17951.7.7
Diffstat (limited to 'src/library.js')
-rw-r--r--src/library.js29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/library.js b/src/library.js
index 48acf6ac..6d821bfc 100644
--- a/src/library.js
+++ b/src/library.js
@@ -6118,12 +6118,17 @@ LibraryManager.library = {
}
return _usleep((seconds * 1e6) + (nanoseconds / 1000));
},
- // TODO: Implement these for real.
+ clock_gettime__deps: ['emscripten_get_now'],
clock_gettime: function(clk_id, tp) {
// int clock_gettime(clockid_t clk_id, struct timespec *tp);
- var now = Date.now();
+ var now;
+ if (clk_id === {{{ cDefine('CLOCK_REALTIME') }}}) {
+ now = Date.now();
+ } else {
+ now = _emscripten_get_now();
+ }
{{{ makeSetValue('tp', C_STRUCTS.timespec.tv_sec, 'Math.floor(now/1000)', 'i32') }}}; // seconds
- {{{ makeSetValue('tp', C_STRUCTS.timespec.tv_nsec, '(now % 1000) * 1000 * 1000', 'i32') }}}; // nanoseconds (really milliseconds)
+ {{{ makeSetValue('tp', C_STRUCTS.timespec.tv_nsec, 'Math.floor((now % 1000)*1000*1000)', 'i32') }}}; // nanoseconds
return 0;
},
clock_settime: function(clk_id, tp) {
@@ -8672,6 +8677,24 @@ LibraryManager.library = {
return func();
},
+ emscripten_get_now: function() {
+ if (!_emscripten_get_now.actual) {
+ if (ENVIRONMENT_IS_NODE) {
+ _emscripten_get_now.actual = function _emscripten_get_now_actual() {
+ var t = process['hrtime']();
+ return t[0] * 1e3 + t[1] / 1e6;
+ }
+ } else if (typeof dateNow !== 'undefined') {
+ _emscripten_get_now.actual = dateNow;
+ } else if (ENVIRONMENT_IS_WEB && window['performance'] && window['performance']['now']) {
+ _emscripten_get_now.actual = function _emscripten_get_now_actual() { return window['performance']['now'](); };
+ } else {
+ _emscripten_get_now.actual = Date.now;
+ }
+ }
+ return _emscripten_get_now.actual();
+ },
+
//============================
// emscripten vector ops
//============================