Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Created January 11, 2012 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnoordhuis/0000731c7df6b7876601 to your computer and use it in GitHub Desktop.
Save bnoordhuis/0000731c7df6b7876601 to your computer and use it in GitHub Desktop.
diff --git a/deps/uv/include/uv-private/uv-unix.h b/deps/uv/include/uv-private/uv-unix.h
index 24ef37c..fa40760 100644
--- a/deps/uv/include/uv-private/uv-unix.h
+++ b/deps/uv/include/uv-private/uv-unix.h
@@ -163,7 +163,8 @@ typedef void* uv_lib_t;
/* UV_TIMER */
#define UV_TIMER_PRIVATE_FIELDS \
ev_timer timer_watcher; \
- uv_timer_cb timer_cb;
+ uv_timer_cb timer_cb; \
+ int active;
#define UV_ARES_TASK_PRIVATE_FIELDS \
int sock; \
diff --git a/deps/uv/src/unix/core.c b/deps/uv/src/unix/core.c
index 210f8fe..3c1e595 100644
--- a/deps/uv/src/unix/core.c
+++ b/deps/uv/src/unix/core.c
@@ -130,10 +130,7 @@ void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
case UV_TIMER:
timer = (uv_timer_t*)handle;
- if (ev_is_active(&timer->timer_watcher)) {
- ev_ref(timer->loop->ev);
- }
- ev_timer_stop(timer->loop->ev, &timer->timer_watcher);
+ uv_timer_stop(timer);
break;
case UV_PROCESS:
@@ -552,7 +549,9 @@ int uv_async_send(uv_async_t* async) {
static void uv__timer_cb(EV_P_ ev_timer* w, int revents) {
uv_timer_t* timer = w->data;
- if (!ev_is_active(w)) {
+ assert(timer->active > 0);
+ if (timer->active == 1) {
+ timer->active = 0;
ev_ref(EV_A);
}
@@ -568,6 +567,7 @@ int uv_timer_init(uv_loop_t* loop, uv_timer_t* timer) {
ev_init(&timer->timer_watcher, uv__timer_cb);
timer->timer_watcher.data = timer;
+ timer->active = 0;
return 0;
}
@@ -575,10 +575,8 @@ int uv_timer_init(uv_loop_t* loop, uv_timer_t* timer) {
int uv_timer_start(uv_timer_t* timer, uv_timer_cb cb, int64_t timeout,
int64_t repeat) {
- if (ev_is_active(&timer->timer_watcher)) {
- return -1;
- }
-
+ if (timer->active) return -1;
+ timer->active = 1 + (repeat != 0);
timer->timer_cb = cb;
ev_timer_set(&timer->timer_watcher, timeout / 1000.0, repeat / 1000.0);
ev_timer_start(timer->loop->ev, &timer->timer_watcher);
@@ -588,21 +586,19 @@ int uv_timer_start(uv_timer_t* timer, uv_timer_cb cb, int64_t timeout,
int uv_timer_stop(uv_timer_t* timer) {
- if (ev_is_active(&timer->timer_watcher)) {
- ev_ref(timer->loop->ev);
- }
-
+ if (timer->active) ev_ref(timer->loop->ev);
ev_timer_stop(timer->loop->ev, &timer->timer_watcher);
return 0;
}
int uv_timer_again(uv_timer_t* timer) {
- if (!ev_is_active(&timer->timer_watcher)) {
+ if (!timer->active) {
uv__set_sys_error(timer->loop, EINVAL);
return -1;
}
+ assert(timer->active == 2);
ev_timer_again(timer->loop->ev, &timer->timer_watcher);
return 0;
}
@@ -614,6 +610,7 @@ void uv_timer_set_repeat(uv_timer_t* timer, int64_t repeat) {
int64_t uv_timer_get_repeat(uv_timer_t* timer) {
assert(timer->type == UV_TIMER);
+ assert(timer->active == 2);
return (int64_t)(1000 * timer->timer_watcher.repeat);
}
diff --git a/deps/uv/src/unix/ev/ev.c b/deps/uv/src/unix/ev/ev.c
index a3bec43..e2a4a0f 100644
--- a/deps/uv/src/unix/ev/ev.c
+++ b/deps/uv/src/unix/ev/ev.c
@@ -2548,6 +2548,7 @@ void
ev_unref (EV_P)
{
--activecnt;
+ if (activecnt < 0) abort();
}
void
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment