From b8ac3d2a175799678691bd123d7e1cb1d42b46f8 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 15 Nov 2013 18:35:41 -0800 Subject: precise clock_gettime when not using CLOCK_REALTIME; fixes #1795 --- src/library.js | 29 ++++++++++++++++++++++++++--- src/library_browser.js | 18 ------------------ src/struct_info.json | 3 ++- tools/shared.py | 2 +- 4 files changed, 29 insertions(+), 23 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 //============================ diff --git a/src/library_browser.js b/src/library_browser.js index 39a1c55d..1883c3be 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -871,24 +871,6 @@ mergeInto(LibraryManager.library, { {{{ makeSetValue('isFullscreen', '0', 'Browser.isFullScreen ? 1 : 0', 'i32') }}}; }, - 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_create_worker: function(url) { url = Pointer_stringify(url); var id = Browser.workers.length; diff --git a/src/struct_info.json b/src/struct_info.json index b91d077e..c136cc8b 100644 --- a/src/struct_info.json +++ b/src/struct_info.json @@ -169,7 +169,8 @@ { "file": "libc/time.h", "defines": [ - ["li", "CLOCKS_PER_SEC"] + ["li", "CLOCKS_PER_SEC"], + "CLOCK_REALTIME" ], "structs": { "timezone": [ diff --git a/tools/shared.py b/tools/shared.py index 5b165b8b..0a6740b9 100644 --- a/tools/shared.py +++ b/tools/shared.py @@ -307,7 +307,7 @@ def find_temp_directory(): # we re-check sanity when the settings are changed) # We also re-check sanity and clear the cache when the version changes -EMSCRIPTEN_VERSION = '1.7.6' +EMSCRIPTEN_VERSION = '1.7.7' def generate_sanity(): return EMSCRIPTEN_VERSION + '|' + get_llvm_target() + '|' + LLVM_ROOT -- cgit v1.2.3-18-g5258