Commit 4ff8b08b authored by Pekka Pessi's avatar Pekka Pessi

su_timer.c: refactored using su_timer_tree() validation function.

darcs-hash:20070208182605-65a35-679bdf6cb85f39e4114ec7d5b932776f59eb1652.gz
parent 29ba6a7d
......@@ -193,7 +193,10 @@ RBTREE_BODIES(static inline, timers, su_timer_t,
IS_RED, SET_RED, IS_BLACK, SET_BLACK, COPY_COLOR,
CMP, INSERT, REMOVE);
/** Set the timer. */
/**@internal Set the timer.
*
* @retval 0 when successful (always)
*/
static inline int
su_timer_set0(su_timer_t **timers,
su_timer_t *t,
......@@ -212,7 +215,10 @@ su_timer_set0(su_timer_t **timers,
return timers_append(timers, t);
}
/** Reset the timer. */
/**@internal Reset the timer.
*
* @retval 0 when successful (always)
*/
static inline int
su_timer_reset0(su_timer_t **timers,
su_timer_t *t)
......@@ -229,6 +235,40 @@ su_timer_reset0(su_timer_t **timers,
return 0;
}
/**@internal Validate timer @a t and return pointer to per-port timer tree.
*
* @retval pointer to pointer to timer tree when successful
* @retval NULL upon an error
*/
static
su_timer_t **su_timer_tree(su_timer_t const *t,
int use_sut_duration,
char const *caller)
{
su_timer_t **timers;
if (t == NULL) {
SU_DEBUG_1(("%s(%p): %s\n", caller, (void *)t,
"NULL argument"));
return NULL;
}
timers = su_task_timers(t->sut_task);
if (timers == NULL)
SU_DEBUG_1(("%s(%p): %s\n", caller, (void *)t,
"invalid timer"));
if (use_sut_duration && t->sut_duration == 0) {
assert(t->sut_duration > 0);
SU_DEBUG_1(("%s(%p): %s\n", caller, (void *)t,
"timer without default duration"));
return NULL;
}
return timers;
}
/**Create a timer.
*
......@@ -291,23 +331,12 @@ int su_timer_set_interval(su_timer_t *t,
su_timer_arg_t *arg,
su_duration_t interval)
{
char const *func = "su_timer_set_interval";
su_timer_t **timers;
if (t == NULL) {
SU_DEBUG_1(("%s(%p): %s\n", func, (void *)t, "NULL argument"));
return -1;
}
su_timer_t **timers = su_timer_tree(t, 0, "su_timer_set_interval");
timers = su_task_timers(t->sut_task);
if (timers == NULL) {
SU_DEBUG_1(("%s(%p): %s\n", func, (void *)t, "invalid timer"));
if (t == NULL)
return -1;
}
su_timer_set0(timers, t, wakeup, arg, su_now(), interval);
return 0;
return su_timer_set0(timers, t, wakeup, arg, su_now(), interval);
}
/** Set the timer for the default interval.
......@@ -326,19 +355,12 @@ int su_timer_set(su_timer_t *t,
su_timer_f wakeup,
su_timer_arg_t *arg)
{
char const *func = "su_timer_set";
su_timer_t **timers = su_timer_tree(t, 1, "su_timer_set");
if (t == NULL)
if (timers == NULL)
return -1;
assert(t->sut_duration > 0);
if (t->sut_duration == 0) {
SU_DEBUG_0(("%s(%p): %s\n", func, (void *)t,
"timer without default duration"));
return -1;
}
return su_timer_set_interval(t, wakeup, arg, t->sut_duration);
return su_timer_set0(timers, t, wakeup, arg, su_now(), t->sut_duration);
}
/** Set timer at known time.
......@@ -357,23 +379,12 @@ int su_timer_set_at(su_timer_t *t,
su_wakeup_arg_t *arg,
su_time_t when)
{
char const *func = "su_timer_set_at";
su_timer_t **timers;
if (t == NULL) {
SU_DEBUG_1(("%s(%p): %s\n", func, (void *)t, "NULL argument"));
return -1;
}
su_timer_t **timers = su_timer_tree(t, 0, "su_timer_set_at");
timers = su_task_timers(t->sut_task);
if (timers == NULL) {
SU_DEBUG_1(("%s(%p): %s\n", func, (void *)t, "invalid timer"));
if (timers == NULL)
return -1;
}
su_timer_set0(timers, t, wakeup, arg, when, 0);
return 0;
return su_timer_set0(timers, t, wakeup, arg, when, 0);
}
/** Set the timer for regular intervals.
......@@ -398,35 +409,17 @@ int su_timer_run(su_timer_t *t,
su_timer_f wakeup,
su_timer_arg_t *arg)
{
char const *func = "su_timer_run";
su_timer_t **timers;
su_time_t now = su_now();
su_timer_t **timers = su_timer_tree(t, 1, "su_timer_run");
su_time_t now;
if (t == NULL) {
SU_DEBUG_1(("%s(%p): %s\n", func, (void *)t, "NULL argument"));
if (timers == NULL)
return -1;
}
assert(t->sut_duration > 0);
if (t->sut_duration == 0) {
SU_DEBUG_1(("%s(%p): %s\n", func, (void *)t,
"timer without default duration"));
return -1;
}
timers = su_task_timers(t->sut_task);
if (timers == NULL) {
SU_DEBUG_1(("%s(%p): %s\n", func, (void *)t, "invalid timer"));
return -1;
}
t->sut_running = run_at_intervals;
t->sut_run = now;
t->sut_run = now = su_now();
t->sut_woken = 0;
su_timer_set0(timers, t, wakeup, arg, now, t->sut_duration);
return 0;
return su_timer_set0(timers, t, wakeup, arg, now, t->sut_duration);
}
/**Set the timer for regular intervals.
......@@ -449,35 +442,17 @@ int su_timer_set_for_ever(su_timer_t *t,
su_timer_f wakeup,
su_timer_arg_t *arg)
{
char const *func = "su_timer_set_for_ever";
su_timer_t **timers;
su_time_t now = su_now();
su_timer_t **timers = su_timer_tree(t, 1, "su_timer_set_for_ever");
su_time_t now;
if (t == NULL) {
SU_DEBUG_1(("%s(%p): %s\n", func, (void *)t, "NULL argument"));
return -1;
}
assert(t->sut_duration > 0);
if (t->sut_duration == 0) {
SU_DEBUG_1(("%s(%p): %s\n", func, (void *)t,
"timer without default duration"));
if (timers == NULL)
return -1;
}
timers = su_task_timers(t->sut_task);
if (timers == NULL) {
SU_DEBUG_1(("%s(%p): %s\n", func, (void *)t, "invalid timer"));
return -1;
}
t->sut_running = run_for_ever;
t->sut_run = now;
t->sut_run = now = su_now();
t->sut_woken = 0;
su_timer_set0(timers, t, wakeup, arg, now, t->sut_duration);
return 0;
return su_timer_set0(timers, t, wakeup, arg, now, t->sut_duration);
}
/**Reset the timer.
......@@ -490,19 +465,12 @@ int su_timer_set_for_ever(su_timer_t *t,
*/
int su_timer_reset(su_timer_t *t)
{
char const *func = "su_timer_reset";
su_timer_t **timers;
su_timer_t **timers = su_timer_tree(t, 0, "su_timer_reset");
if (t == NULL) {
SU_DEBUG_1(("%s(%p): %s\n", func, (void *)t, "NULL argument"));
if (timers == NULL)
return -1;
}
timers = su_task_timers(t->sut_task);
su_timer_reset0(timers, t);
return 0;
return su_timer_reset0(timers, t);
}
/** @internal Check for expired timers in queue.
......@@ -576,6 +544,7 @@ int su_timer_expire(su_timer_t ** const timers,
}
/** Calculate duration in milliseconds until next timer expires. */
su_duration_t su_timer_next_expires(su_timer_t const * t, su_time_t now)
{
su_duration_t tout;
......@@ -625,14 +594,13 @@ int su_timer_reset_all(su_timer_t **timers, su_task_r task)
/** Get the root object owning the timer.
*
* The function su_timer_root() return pointer to the root object owning the
* timer.
* Return pointer to the root object owning the timer.
*
* @param t pointer to the timer
*
* @return Pointer to the root object owning the timer.
* @return Pointer to the root object.
*/
su_root_t *su_timer_root(su_timer_t const *t)
{
return su_task_root(t->sut_task);
return t ? su_task_root(t->sut_task) : NULL;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment