diff options
author | Geoff Gustafson <geoff@linux.intel.com> | 2017-03-01 17:40:47 -0800 |
---|---|---|
committer | Jimmy Huang <jimmy.huang@linux.intel.com> | 2017-03-02 16:43:18 -0800 |
commit | 92629e916c7634b6d868839176bc49c54859256c (patch) | |
tree | 6266d16441ea39ca6813579c371ade6f61466bfe /src | |
parent | b541510b0c59cdccde256a6a287eb1e7ccf5a7bf (diff) |
[timers] Ensure coordinated timers execute in the order created
This changes the output of samples/Timers.js to match what happens in
Chrome and appears to be required by specification. We were previously
inserting the timers in reverse and this was visible to the users if they
created multiple timers with the same timeout.
Also found we weren't checking malloc for success when allocating the
timer callback argument storage, fixed that.
Signed-off-by: Geoff Gustafson <geoff@linux.intel.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/zjs_timers.c | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/src/zjs_timers.c b/src/zjs_timers.c index 34a63ec..31d0257 100644 --- a/src/zjs_timers.c +++ b/src/zjs_timers.c @@ -53,10 +53,7 @@ static zjs_timer_t *add_timer(uint32_t interval, uint32_t argc, const jerry_value_t argv[]) { - int i; - zjs_timer_t *tm; - - tm = zjs_malloc(sizeof(zjs_timer_t)); + zjs_timer_t *tm = zjs_malloc(sizeof(zjs_timer_t)); if (!tm) { ERR_PRINT("out of memory allocating timer struct\n"); return NULL; @@ -66,25 +63,35 @@ static zjs_timer_t *add_timer(uint32_t interval, tm->interval = interval; tm->repeat = repeat; tm->completed = false; - tm->next = zjs_timers; - if (tm->repeat) { - tm->callback_id = zjs_add_callback(callback, this, tm, NULL); - } else { - tm->callback_id = zjs_add_callback_once(callback, this, tm, NULL); - } + tm->next = NULL; tm->argc = argc; if (tm->argc) { tm->argv = zjs_malloc(sizeof(jerry_value_t) * argc); - for (i = 0; i < argc; ++i) { + if (!tm->argv) { + zjs_free(tm); + ERR_PRINT("out of memory allocating timer args\n"); + return NULL; + } + for (int i = 0; i < argc; ++i) { tm->argv[i] = jerry_acquire_value(argv[i + 2]); } } else { tm->argv = NULL; } - zjs_timers = tm; + if (tm->repeat) { + tm->callback_id = zjs_add_callback(callback, this, tm, NULL); + } else { + tm->callback_id = zjs_add_callback_once(callback, this, tm, NULL); + } + + zjs_timer_t **pnext = &zjs_timers; + while (*pnext != NULL) { + pnext = &(*pnext)->next; + } + *pnext = tm; - DBG_PRINT("adding timer. id=%d, interval=%lu, repeat=%u, argv=%p, argc=%lu\n", + DBG_PRINT("add timer, id=%d, interval=%lu, repeat=%u, argv=%p, argc=%lu\n", tm->callback_id, interval, repeat, argv, argc); zjs_port_timer_start(&tm->timer, interval); return tm; |