Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

WIP Common timer functionality #709

Closed
wants to merge 1 commit into from

2 participants

@txdv

Do not pull yet, wnidows version not here.
I am going to squash the commits later on, just comment on them for now.

@txdv

I have a problem with uv_timer_set_repeat and uv_timer_get.
Both use asserts instead of returning error codes.
Now I can change uv_timer_set_repeat easily to return an int (0 for success, other for error).

But what should I do with uv_timer_get_repeat?

@saghul
Collaborator

Since you'd prevent the timer from getting anything < 0 on set, maybe get can return -1 on error or the actual value otherwise?

@txdv

Yeah, but we loose the errorcode, which is in this is always invalid argument.
Maybe

int uv_timer_get_repeat(uv_handle_t *handle, int64_t* value)

seems like an overkill to me.

@saghul
Collaborator

Hum, no you wouldn't. If there is an error you store it in the loop so it can be retrieved with uv_last_error, and return -1, thus telling the user that he should check it on the loop. This is how uv_fs_read works for example.

@txdv

Seems legit.

@txdv txdv closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 15, 2013
  1. @txdv
This page is out of date. Refresh to see the latest.
View
18 include/uv.h
@@ -1194,9 +1194,25 @@ UV_EXTERN int uv_timer_again(uv_timer_t* timer);
* from a timer callback it does not immediately take effect. If the timer was
* non-repeating before, it will have been stopped. If it was repeating, then
* the old repeat value will have been used to schedule the next timeout.
+ *
+ * Arguments:
+ * timer Timer handle. Should have been initialized with `uv_timer_init`.
+ * repeat Repeat time in milliseconds.
+ *
+ * Returns:
+ * 0 on success, -1 on error.
*/
-UV_EXTERN void uv_timer_set_repeat(uv_timer_t* timer, int64_t repeat);
+UV_EXTERN int uv_timer_set_repeat(uv_timer_t* timer, int64_t repeat);
+/*
+ * Gets the set repeat value in milliseconds of a handle.
+
+ * Arguments:
+ * timer Timer handle.
+ *
+ * Returns:
+ * greater or equal 0 on success, -1 on error.
+ */
UV_EXTERN int64_t uv_timer_get_repeat(uv_timer_t* timer);
View
23 src/unix/timer.c
@@ -50,13 +50,10 @@ int uv_timer_init(uv_loop_t* loop, uv_timer_t* handle) {
}
-int uv_timer_start(uv_timer_t* handle,
- uv_timer_cb cb,
- int64_t timeout,
- int64_t repeat) {
- assert(timeout >= 0);
- assert(repeat >= 0);
-
+int uv__timer_start(uv_timer_t* handle,
+ uv_timer_cb cb,
+ int64_t timeout,
+ int64_t repeat) {
if (uv__is_active(handle))
uv_timer_stop(handle);
@@ -73,7 +70,7 @@ int uv_timer_start(uv_timer_t* handle,
}
-int uv_timer_stop(uv_timer_t* handle) {
+int uv__timer_stop(uv_timer_t* handle) {
if (!uv__is_active(handle))
return 0;
@@ -84,10 +81,7 @@ int uv_timer_stop(uv_timer_t* handle) {
}
-int uv_timer_again(uv_timer_t* handle) {
- if (handle->timer_cb == NULL)
- return uv__set_artificial_error(handle->loop, UV_EINVAL);
-
+int uv__timer_again(uv_timer_t* handle) {
if (handle->repeat) {
uv_timer_stop(handle);
uv_timer_start(handle, handle->timer_cb, handle->repeat, handle->repeat);
@@ -97,13 +91,12 @@ int uv_timer_again(uv_timer_t* handle) {
}
-void uv_timer_set_repeat(uv_timer_t* handle, int64_t repeat) {
- assert(repeat >= 0);
+void uv__timer_set_repeat(uv_timer_t* handle, int64_t repeat) {
handle->repeat = repeat;
}
-int64_t uv_timer_get_repeat(uv_timer_t* handle) {
+int64_t uv__timer_get_repeat(uv_timer_t* handle) {
return handle->repeat;
}
View
50 src/uv-common.c
@@ -264,6 +264,56 @@ int uv_tcp_connect6(uv_connect_t* req,
}
+int uv_timer_start(uv_timer_t* handle,
+ uv_timer_cb cb,
+ int64_t timeout,
+ int64_t repeat) {
+ if (handle->type != UV_TIMER || timeout < 0 || repeat < 0) {
+ return uv__set_artificial_error(handle->loop, UV_EINVAL);
+ }
+
+ return uv__timer_start(handle, cb, timeout, repeat);
+}
+
+
+int uv_timer_stop(uv_timer_t* handle) {
+ if (handle->type != UV_TIMER) {
+ return uv__set_artificial_error(handle->loop, UV_EINVAL);
+ }
+
+ return uv__timer_stop(handle);
+}
+
+
+int uv_timer_again(uv_timer_t *handle) {
+ /* timer_cb is NULL means that the timer was never started. */
+ if (handle->type != UV_TIMER || handle->timer_cb == NULL) {
+ return uv__set_artificial_error(handle->loop, UV_EINVAL);
+ }
+
+ return uv__timer_again(handle);
+}
+
+
+int uv_timer_set_repeat(uv_timer_t* handle, int64_t repeat) {
+ if (handle->type != UV_TIMER || repeat < 0) {
+ return uv__set_artificial_error(handle->loop, UV_EINVAL);
+ }
+
+ uv__timer_set_repeat(handle, repeat);
+ return 0;
+}
+
+
+int64_t uv_timer_get_repeat(uv_timer_t* handle) {
+ if (handle->type != UV_TIMER) {
+ return uv__set_artificial_error(handle->loop, UV_EINVAL);
+ }
+
+ return uv__timer_get_repeat(handle);
+}
+
+
#ifdef _WIN32
static UINT __stdcall uv__thread_start(void *ctx_v)
#else
View
13 src/uv-common.h
@@ -93,6 +93,19 @@ int uv__tcp_connect6(uv_connect_t* req,
struct sockaddr_in6 address,
uv_connect_cb cb);
+int uv__timer_start(uv_timer_t* handle,
+ uv_timer_cb cb,
+ int64_t timeout,
+ int64_t repeat);
+
+int uv__timer_stop(uv_timer_t* handle);
+
+int uv__timer_again(uv_timer_t* handle);
+
+void uv__timer_set_repeat(uv_timer_t* handle, int64_t repeat);
+
+int64_t uv__timer_get_repeat(uv_timer_t* handle);
+
void uv__fs_poll_close(uv_fs_poll_t* handle);
View
18 src/win/timer.c
@@ -87,7 +87,7 @@ void uv_timer_endgame(uv_loop_t* loop, uv_timer_t* handle) {
}
-int uv_timer_start(uv_timer_t* handle, uv_timer_cb timer_cb, int64_t timeout,
+int uv__timer_start(uv_timer_t* handle, uv_timer_cb timer_cb, int64_t timeout,
int64_t repeat) {
uv_loop_t* loop = handle->loop;
uv_timer_t* old;
@@ -112,7 +112,7 @@ int uv_timer_start(uv_timer_t* handle, uv_timer_cb timer_cb, int64_t timeout,
}
-int uv_timer_stop(uv_timer_t* handle) {
+int uv__timer_stop(uv_timer_t* handle) {
uv_loop_t* loop = handle->loop;
if (!(handle->flags & UV_HANDLE_ACTIVE))
@@ -127,15 +127,9 @@ int uv_timer_stop(uv_timer_t* handle) {
}
-int uv_timer_again(uv_timer_t* handle) {
+int uv__timer_again(uv_timer_t* handle) {
uv_loop_t* loop = handle->loop;
- /* If timer_cb is NULL that means that the timer was never started. */
- if (!handle->timer_cb) {
- uv__set_sys_error(loop, ERROR_INVALID_DATA);
- return -1;
- }
-
if (handle->flags & UV_HANDLE_ACTIVE) {
RB_REMOVE(uv_timer_tree_s, &loop->timers, handle);
handle->flags &= ~UV_HANDLE_ACTIVE;
@@ -157,14 +151,12 @@ int uv_timer_again(uv_timer_t* handle) {
}
-void uv_timer_set_repeat(uv_timer_t* handle, int64_t repeat) {
- assert(handle->type == UV_TIMER);
+void uv__timer_set_repeat(uv_timer_t* handle, int64_t repeat) {
handle->repeat = repeat;
}
-int64_t uv_timer_get_repeat(uv_timer_t* handle) {
- assert(handle->type == UV_TIMER);
+int64_t uv__timer_get_repeat(uv_timer_t* handle) {
return handle->repeat;
}
Something went wrong with that request. Please try again.