Introduce time_offset() helper and replace open-coded versions in timer.c
This function takes a struct timeval and offsets it by a (potentially fractional) number of seconds, given as a 'double'.
This commit is contained in:
@@ -74,6 +74,7 @@ double BX_time_diff (struct timeval, struct timeval);
|
|||||||
double time_since(const struct timeval *tv_from);
|
double time_since(const struct timeval *tv_from);
|
||||||
double time_until(const struct timeval *tv_to);
|
double time_until(const struct timeval *tv_to);
|
||||||
int time_cmp(const struct timeval *a, const struct timeval *b);
|
int time_cmp(const struct timeval *a, const struct timeval *b);
|
||||||
|
struct timeval *time_offset(struct timeval *tv, double offset);
|
||||||
char * BX_plural (int);
|
char * BX_plural (int);
|
||||||
int BX_time_to_next_minute (void);
|
int BX_time_to_next_minute (void);
|
||||||
char * BX_remove_trailing_spaces (char *);
|
char * BX_remove_trailing_spaces (char *);
|
||||||
|
|||||||
@@ -1747,6 +1747,26 @@ int time_cmp(const struct timeval *a, const struct timeval *b)
|
|||||||
return (a->tv_sec > b->tv_sec) - (a->tv_sec < b->tv_sec);
|
return (a->tv_sec > b->tv_sec) - (a->tv_sec < b->tv_sec);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Add an offset, in seconds, to a timeval */
|
||||||
|
struct timeval *time_offset(struct timeval *tv, double offset)
|
||||||
|
{
|
||||||
|
time_t seconds = offset; /* Discards fractional part */
|
||||||
|
|
||||||
|
offset -= seconds;
|
||||||
|
if (offset < 0.0)
|
||||||
|
{
|
||||||
|
/* This ensures we never make tv_usec go negative, since it might be unsigned. */
|
||||||
|
offset += 1.0;
|
||||||
|
seconds -= 1;
|
||||||
|
}
|
||||||
|
tv->tv_usec += offset * 1000000.0;
|
||||||
|
seconds += tv->tv_usec / 1000000;
|
||||||
|
tv->tv_usec %= 1000000;
|
||||||
|
tv->tv_sec += seconds;
|
||||||
|
|
||||||
|
return tv;
|
||||||
|
}
|
||||||
|
|
||||||
int BX_time_to_next_minute (void)
|
int BX_time_to_next_minute (void)
|
||||||
{
|
{
|
||||||
time_t now = time(NULL);
|
time_t now = time(NULL);
|
||||||
|
|||||||
@@ -164,18 +164,18 @@ static char *current_exec_timer = empty_string;
|
|||||||
extern void ExecuteTimers (void)
|
extern void ExecuteTimers (void)
|
||||||
{
|
{
|
||||||
TimerList *current;
|
TimerList *current;
|
||||||
static int parsingtimer = 0;
|
static int parsingtimer = 0;
|
||||||
int old_from_server = from_server;
|
int old_from_server = from_server;
|
||||||
struct timeval now1;
|
struct timeval now1;
|
||||||
|
|
||||||
/* We do NOT want to parse timers while waiting
|
/* We do NOT want to parse timers while waiting
|
||||||
* cause it gets icky recursive
|
* cause it gets icky recursive
|
||||||
*/
|
*/
|
||||||
if (!PendingTimers || parsingtimer)
|
if (!PendingTimers || parsingtimer)
|
||||||
return;
|
return;
|
||||||
get_time(&now1);
|
get_time(&now1);
|
||||||
|
|
||||||
parsingtimer = 1;
|
parsingtimer = 1;
|
||||||
while (PendingTimers && time_cmp(&now1, &PendingTimers->time) >= 0)
|
while (PendingTimers && time_cmp(&now1, &PendingTimers->time) >= 0)
|
||||||
{
|
{
|
||||||
int old_refnum = current_window->refnum;
|
int old_refnum = current_window->refnum;
|
||||||
@@ -218,37 +218,26 @@ extern void ExecuteTimers (void)
|
|||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
case 1:
|
case 1:
|
||||||
{
|
{
|
||||||
/* callback cleans up command */
|
/* callback cleans up command */
|
||||||
if (!current->callback)
|
if (!current->callback)
|
||||||
new_free(¤t->command);
|
new_free(¤t->command);
|
||||||
new_free(¤t->subargs);
|
new_free(¤t->subargs);
|
||||||
new_free(¤t->whom);
|
new_free(¤t->whom);
|
||||||
new_free(¤t);
|
new_free(¤t);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
current->events--;
|
current->events--;
|
||||||
case -1:
|
case -1:
|
||||||
{
|
{
|
||||||
#if 1
|
time_offset(¤t->time, current->interval);
|
||||||
double milli, seconds;
|
schedule_timer(current);
|
||||||
milli = current->interval * 1000 * 1000 + current->time.tv_usec;
|
break;
|
||||||
seconds = current->time.tv_sec + (milli / 1000000);
|
}
|
||||||
milli = ((unsigned long)current) % 1000000;
|
|
||||||
current->time.tv_sec = seconds;
|
|
||||||
current->time.tv_usec = milli;
|
|
||||||
#else
|
|
||||||
current->time.tv_usec += (current->interval * 1000);
|
|
||||||
current->time.tv_sec += (current->time.tv_usec / 1000000);
|
|
||||||
current->time.tv_usec %= 1000000;
|
|
||||||
#endif
|
|
||||||
schedule_timer(current);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
parsingtimer = 0;
|
parsingtimer = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -464,24 +453,11 @@ char *BX_add_timer(int update, char *refnum_want, double when, long events, int
|
|||||||
{
|
{
|
||||||
TimerList *ntimer, *otimer = NULL;
|
TimerList *ntimer, *otimer = NULL;
|
||||||
char refnum_got[REFNUM_MAX+1] = "";
|
char refnum_got[REFNUM_MAX+1] = "";
|
||||||
double seconds = 0.0, milli = 0.0;
|
|
||||||
extern double fmod(double, double);
|
|
||||||
|
|
||||||
ntimer = (TimerList *) new_malloc(sizeof(TimerList));
|
ntimer = (TimerList *) new_malloc(sizeof(TimerList));
|
||||||
|
|
||||||
get_time(&ntimer->time);
|
get_time(&ntimer->time);
|
||||||
#if 1
|
time_offset(&ntimer->time, when / 1000.0);
|
||||||
milli = when * 1000 + ntimer->time.tv_usec;
|
|
||||||
seconds = ntimer->time.tv_sec + (milli / 1000000);
|
|
||||||
|
|
||||||
milli = ((unsigned long)milli) % 1000000;
|
|
||||||
ntimer->time.tv_sec = seconds;
|
|
||||||
ntimer->time.tv_usec = milli;
|
|
||||||
#else
|
|
||||||
ntimer->time.tv_usec += (unsigned long)when;
|
|
||||||
ntimer->time.tv_sec += ((when + ntimer->time.tv_usec) / 1000);
|
|
||||||
ntimer->time.tv_usec %= 1000;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ntimer->interval = when / 1000;
|
ntimer->interval = when / 1000;
|
||||||
ntimer->events = events;
|
ntimer->events = events;
|
||||||
|
|||||||
Reference in New Issue
Block a user