schedule_job uses seconds to support time values larger than 49 days

added schedule_job_ms for ms resolution events
This commit is contained in:
Martin Willi
2009-04-21 15:16:56 +00:00
parent 59a4e87db6
commit 6554b5e412
11 changed files with 125 additions and 74 deletions
@@ -1530,7 +1530,7 @@ static void schedule_expire(private_kernel_klips_ipsec_t *this,
expire->reqid = reqid;
expire->type = type;
job = callback_job_create((callback_job_cb_t)sa_expires, expire, free, NULL);
charon->scheduler->schedule_job(charon->scheduler, (job_t*)job, time * 1000);
charon->scheduler->schedule_job(charon->scheduler, (job_t*)job, time);
}
/**
@@ -219,7 +219,7 @@ static void fire_roam_job(private_kernel_netlink_net_t *this, bool address)
now.tv_usec -= 1000000;
}
this->last_roam = now;
charon->scheduler->schedule_job(charon->scheduler,
charon->scheduler->schedule_job_ms(charon->scheduler,
(job_t*)roam_job_create(address), ROAM_DELAY);
}
}
+78 -37
View File
@@ -19,7 +19,6 @@
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include "scheduler.h"
@@ -41,7 +40,7 @@ struct event_t {
* Time to fire the event.
*/
timeval_t time;
/**
* Every event has its assigned job.
*/
@@ -63,16 +62,17 @@ typedef struct private_scheduler_t private_scheduler_t;
* Private data of a scheduler_t object.
*/
struct private_scheduler_t {
/**
* Public part of a scheduler_t object.
*/
scheduler_t public;
/**
* Job which queues scheduled jobs to the processor.
*/
callback_job_t *job;
/**
* The heap in which the events are stored.
*/
@@ -87,12 +87,12 @@ struct private_scheduler_t {
* The number of scheduled events.
*/
u_int event_count;
/**
* Exclusive access to list
*/
mutex_t *mutex;
/**
* Condvar to wait for next job.
*/
@@ -100,16 +100,27 @@ struct private_scheduler_t {
};
/**
* Returns the difference of two timeval structs in milliseconds
* Comparse two timevals, return >0 if a > b, <0 if a < b and =0 if equal
*/
static long time_difference(timeval_t *end, timeval_t *start)
static int timeval_cmp(timeval_t *a, timeval_t *b)
{
time_t s;
suseconds_t us;
s = end->tv_sec - start->tv_sec;
us = end->tv_usec - start->tv_usec;
return (s * 1000 + us/1000);
if (a->tv_sec > b->tv_sec)
{
return 1;
}
if (a->tv_sec < b->tv_sec)
{
return -1;
}
if (a->tv_usec > b->tv_usec)
{
return 1;
}
if (a->tv_usec < b->tv_usec)
{
return -1;
}
return 0;
}
/**
@@ -146,14 +157,14 @@ static event_t *remove_event(private_scheduler_t *this)
u_int child = position << 1;
if ((child + 1) <= this->event_count &&
time_difference(&this->heap[child + 1]->time,
&this->heap[child]->time) < 0)
timeval_cmp(&this->heap[child + 1]->time,
&this->heap[child]->time) < 0)
{
/* the "right" child is smaller */
child++;
}
if (time_difference(&top->time, &this->heap[child]->time) <= 0)
if (timeval_cmp(&top->time, &this->heap[child]->time) <= 0)
{
/* the top event fires before the smaller of the two children, stop */
break;
@@ -175,7 +186,6 @@ static job_requeue_t schedule(private_scheduler_t * this)
{
timeval_t now;
event_t *event;
long difference;
int oldstate;
bool timed = FALSE;
@@ -185,8 +195,7 @@ static job_requeue_t schedule(private_scheduler_t * this)
if ((event = peek_event(this)) != NULL)
{
difference = time_difference(&now, &event->time);
if (difference >= 0)
if (timeval_cmp(&now, &event->time) >= 0)
{
remove_event(this);
this->mutex->unlock(this->mutex);
@@ -195,7 +204,16 @@ static job_requeue_t schedule(private_scheduler_t * this)
free(event);
return JOB_REQUEUE_DIRECT;
}
DBG2(DBG_JOB, "next event in %ldms, waiting", -difference);
timersub(&event->time, &now, &now);
if (now.tv_sec)
{
DBG2(DBG_JOB, "next event in %ds %dms, waiting",
now.tv_sec, now.tv_usec/1000);
}
else
{
DBG2(DBG_JOB, "next event in %dms, waiting", now.tv_usec/1000);
}
timed = TRUE;
}
pthread_cleanup_push((void*)this->mutex->unlock, this->mutex);
@@ -228,25 +246,16 @@ static u_int get_job_load(private_scheduler_t *this)
}
/**
* Implements scheduler_t.schedule_job.
* Implements scheduler_t.schedule_job_tv.
*/
static void schedule_job(private_scheduler_t *this, job_t *job, u_int32_t time)
static void schedule_job_tv(private_scheduler_t *this, job_t *job, timeval_t tv)
{
timeval_t now;
event_t *event;
u_int position;
time_t s;
suseconds_t us;
event = malloc_thing(event_t);
event->job = job;
/* calculate absolute time */
s = time / 1000;
us = (time - s * 1000) * 1000;
gettimeofday(&now, NULL);
event->time.tv_usec = (now.tv_usec + us) % 1000000;
event->time.tv_sec = now.tv_sec + (now.tv_usec + us)/1000000 + s;
event->time = tv;
this->mutex->lock(this->mutex);
@@ -255,14 +264,15 @@ static void schedule_job(private_scheduler_t *this, job_t *job, u_int32_t time)
{
/* double the size of the heap */
this->heap_size <<= 1;
this->heap = (event_t**)realloc(this->heap, (this->heap_size + 1) * sizeof(event_t*));
this->heap = (event_t**)realloc(this->heap,
(this->heap_size + 1) * sizeof(event_t*));
}
/* "put" the event to the bottom */
position = this->event_count;
/* then bubble it up */
while (position > 1 && time_difference(&this->heap[position >> 1]->time,
&event->time) > 0)
while (position > 1 && timeval_cmp(&this->heap[position >> 1]->time,
&event->time) > 0)
{
/* parent has to be fired after the new event, move up */
this->heap[position] = this->heap[position >> 1];
@@ -274,6 +284,35 @@ static void schedule_job(private_scheduler_t *this, job_t *job, u_int32_t time)
this->mutex->unlock(this->mutex);
}
/**
* Implements scheduler_t.schedule_job.
*/
static void schedule_job(private_scheduler_t *this, job_t *job, u_int32_t s)
{
timeval_t tv;
gettimeofday(&tv, NULL);
tv.tv_sec += s;
schedule_job_tv(this, job, tv);
}
/**
* Implements scheduler_t.schedule_job_ms.
*/
static void schedule_job_ms(private_scheduler_t *this, job_t *job, u_int32_t ms)
{
timeval_t tv, add;
gettimeofday(&tv, NULL);
add.tv_sec = ms / 1000;
add.tv_usec = (ms % 1000) * 1000;
timeradd(&tv, &add, &tv);
schedule_job_tv(this, job, tv);
}
/**
* Implementation of scheduler_t.destroy.
*/
@@ -299,7 +338,9 @@ scheduler_t * scheduler_create()
private_scheduler_t *this = malloc_thing(private_scheduler_t);
this->public.get_job_load = (u_int (*) (scheduler_t *this)) get_job_load;
this->public.schedule_job = (void (*) (scheduler_t *this, job_t *job, u_int32_t ms)) schedule_job;
this->public.schedule_job = (void (*) (scheduler_t *this, job_t *job, u_int32_t s)) schedule_job;
this->public.schedule_job_ms = (void (*) (scheduler_t *this, job_t *job, u_int32_t ms)) schedule_job_ms;
this->public.schedule_job_tv = (void (*) (scheduler_t *this, job_t *job, timeval_t tv)) schedule_job_tv;
this->public.destroy = (void(*)(scheduler_t*)) destroy;
/* Note: the root of the heap is at index 1 */
+24 -8
View File
@@ -26,6 +26,8 @@
typedef struct scheduler_t scheduler_t;
#include <sys/time.h>
#include <library.h>
#include <processing/jobs/job.h>
@@ -34,17 +36,31 @@ typedef struct scheduler_t scheduler_t;
*
* The scheduler stores timed events and passes them to the processor.
*/
struct scheduler_t {
struct scheduler_t {
/**
* Adds a event to the queue, using a relative time offset.
* Adds a event to the queue, using a relative time offset in s.
*
* Schedules a job for execution using a relative time offset.
*
* @param job job to schedule
* @param time relative to to schedule job (in ms)
* @param job job to schedule
* @param time relative time to schedule job, in s
*/
void (*schedule_job) (scheduler_t *this, job_t *job, u_int32_t time);
void (*schedule_job) (scheduler_t *this, job_t *job, u_int32_t s);
/**
* Adds a event to the queue, using a relative time offset in ms.
*
* @param job job to schedule
* @param time relative time to schedule job, in ms
*/
void (*schedule_job_ms) (scheduler_t *this, job_t *job, u_int32_t ms);
/**
* Adds a event to the queue, using an absolut time.
*
* @param job job to schedule
* @param time absolut time to schedule job
*/
void (*schedule_job_tv) (scheduler_t *this, job_t *job, timeval_t tv);
/**
* Returns number of jobs scheduled.
+3 -3
View File
@@ -904,7 +904,7 @@ static void update_checklist_state(private_connect_manager_t *this, check_list_t
callback_data_t *data = callback_data_create(this, checklist->connect_id);
job_t *job = (job_t*)callback_job_create((callback_job_cb_t)initiator_finish, data, (callback_job_cleanup_t)callback_data_destroy, NULL);
charon->scheduler->schedule_job(charon->scheduler, job, ME_WAIT_TO_FINISH);
charon->scheduler->schedule_job_ms(charon->scheduler, job, ME_WAIT_TO_FINISH);
checklist->is_finishing = TRUE;
}
@@ -1002,7 +1002,7 @@ static void queue_retransmission(private_connect_manager_t *this, check_list_t *
}
DBG2(DBG_IKE, "scheduling retransmission %d of pair '%d' in %dms", retransmission, pair->id, rto);
charon->scheduler->schedule_job(charon->scheduler, (job_t*)job, rto);
charon->scheduler->schedule_job_ms(charon->scheduler, (job_t*)job, rto);
}
/**
@@ -1139,7 +1139,7 @@ static void schedule_checks(private_connect_manager_t *this, check_list_t *check
{
callback_data_t *data = callback_data_create(this, checklist->connect_id);
checklist->sender = (job_t*)callback_job_create((callback_job_cb_t)sender, data, (callback_job_cleanup_t)callback_data_destroy, NULL);
charon->scheduler->schedule_job(charon->scheduler, checklist->sender, time);
charon->scheduler->schedule_job_ms(charon->scheduler, checklist->sender, time);
}
/**
+11 -17
View File
@@ -443,7 +443,7 @@ static void send_keepalive(private_ike_sa_t *this)
}
job = send_keepalive_job_create(this->ike_sa_id);
charon->scheduler->schedule_job(charon->scheduler, (job_t*)job,
(this->keepalive_interval - diff) * 1000);
this->keepalive_interval - diff);
}
/**
@@ -542,7 +542,7 @@ static void set_condition(private_ike_sa_t *this, ike_condition_t condition,
*/
static status_t send_dpd(private_ike_sa_t *this)
{
send_dpd_job_t *job;
job_t *job;
time_t diff, delay;
delay = this->peer_cfg->get_dpd(this->peer_cfg);
@@ -591,9 +591,8 @@ static status_t send_dpd(private_ike_sa_t *this)
}
}
/* recheck in "interval" seconds */
job = send_dpd_job_create(this->ike_sa_id);
charon->scheduler->schedule_job(charon->scheduler, (job_t*)job,
(delay - diff) * 1000);
job = (job_t*)send_dpd_job_create(this->ike_sa_id);
charon->scheduler->schedule_job(charon->scheduler, job, delay - diff);
return SUCCESS;
}
@@ -636,8 +635,7 @@ static void set_state(private_ike_sa_t *this, ike_sa_state_t state)
{
this->stats[STAT_REKEY] = t + this->stats[STAT_ESTABLISHED];
job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, FALSE);
charon->scheduler->schedule_job(charon->scheduler,
job, t * 1000);
charon->scheduler->schedule_job(charon->scheduler, job, t);
DBG1(DBG_IKE, "scheduling rekeying in %ds", t);
}
t = this->peer_cfg->get_reauth_time(this->peer_cfg);
@@ -646,8 +644,7 @@ static void set_state(private_ike_sa_t *this, ike_sa_state_t state)
{
this->stats[STAT_REAUTH] = t + this->stats[STAT_ESTABLISHED];
job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE);
charon->scheduler->schedule_job(charon->scheduler,
job, t * 1000);
charon->scheduler->schedule_job(charon->scheduler, job, t);
DBG1(DBG_IKE, "scheduling reauthentication in %ds", t);
}
t = this->peer_cfg->get_over_time(this->peer_cfg);
@@ -669,8 +666,7 @@ static void set_state(private_ike_sa_t *this, ike_sa_state_t state)
this->stats[STAT_DELETE] += t;
t = this->stats[STAT_DELETE] - this->stats[STAT_ESTABLISHED];
job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
charon->scheduler->schedule_job(charon->scheduler, job,
t * 1000);
charon->scheduler->schedule_job(charon->scheduler, job, t);
DBG1(DBG_IKE, "maximum IKE_SA lifetime %ds", t);
}
@@ -1930,8 +1926,8 @@ static void set_auth_lifetime(private_ike_sa_t *this, u_int32_t lifetime)
DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling reauthentication"
" in %ds", lifetime, lifetime - reduction);
charon->scheduler->schedule_job(charon->scheduler,
(job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
(lifetime - reduction) * 1000);
(job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
lifetime - reduction);
}
else
{
@@ -2077,11 +2073,9 @@ static status_t inherit(private_ike_sa_t *this, private_ike_sa_t *other)
DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
"lifetime reduced to %ds", reauth, delete);
charon->scheduler->schedule_job(charon->scheduler,
(job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
reauth * 1000);
(job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE), reauth);
charon->scheduler->schedule_job(charon->scheduler,
(job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE),
delete * 1000);
(job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE), delete);
}
/* we have to initate here, there may be new tasks to handle */
return this->task_manager->initiate(this->task_manager);
+2 -2
View File
@@ -44,9 +44,9 @@ typedef struct ike_sa_t ike_sa_t;
#include <config/auth_cfg.h>
/**
* Timeout in milliseconds after that a half open IKE_SA gets deleted.
* Timeout in seconds after that a half open IKE_SA gets deleted.
*/
#define HALF_OPEN_IKE_SA_TIMEOUT 30000
#define HALF_OPEN_IKE_SA_TIMEOUT 30
/**
* Interval to send keepalives when NATed, in seconds.
+1 -1
View File
@@ -259,7 +259,7 @@ static status_t retransmit(private_task_manager_t *this, u_int32_t message_id)
this->initiating.retransmitted++;
job = (job_t*)retransmit_job_create(this->initiating.mid,
this->ike_sa->get_id(this->ike_sa));
charon->scheduler->schedule_job(charon->scheduler, job, timeout);
charon->scheduler->schedule_job_ms(charon->scheduler, job, timeout);
}
return SUCCESS;
}
+1 -1
View File
@@ -772,7 +772,7 @@ static void handle_child_sa_failure(private_child_create_t *this,
/* we delay the delete for 100ms, as the IKE_AUTH response must arrive
* first */
DBG1(DBG_IKE, "closing IKE_SA due CHILD_SA setup failure");
charon->scheduler->schedule_job(charon->scheduler, (job_t*)
charon->scheduler->schedule_job_ms(charon->scheduler, (job_t*)
delete_ike_sa_job_create(this->ike_sa->get_id(this->ike_sa), TRUE),
100);
}
+1 -1
View File
@@ -269,7 +269,7 @@ static status_t process_i(private_child_rekey_t *this, message_t *message)
DBG1(DBG_IKE, "CHILD_SA rekeying failed, "
"trying again in %d seconds", retry);
this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
charon->scheduler->schedule_job(charon->scheduler, job, retry * 1000);
charon->scheduler->schedule_job(charon->scheduler, job, retry);
}
return SUCCESS;
}
+2 -2
View File
@@ -234,7 +234,7 @@ static status_t process_i(private_ike_rekey_t *this, message_t *message)
DBG1(DBG_IKE, "IKE_SA rekeying failed, "
"trying again in %d seconds", retry);
this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
charon->scheduler->schedule_job(charon->scheduler, job, retry * 1000);
charon->scheduler->schedule_job(charon->scheduler, job, retry);
}
return SUCCESS;
case NEED_MORE:
@@ -273,7 +273,7 @@ static status_t process_i(private_ike_rekey_t *this, message_t *message)
/* peer should delete this SA. Add a timeout just in case. */
job_t *job = (job_t*)delete_ike_sa_job_create(
other->new_sa->get_id(other->new_sa), TRUE);
charon->scheduler->schedule_job(charon->scheduler, job, 10000);
charon->scheduler->schedule_job(charon->scheduler, job, 10);
DBG1(DBG_IKE, "IKE_SA rekey collision won, deleting rekeyed IKE_SA");
charon->ike_sa_manager->checkin(charon->ike_sa_manager, other->new_sa);
other->new_sa = NULL;