timer: add osmo_timer_setup()

Add a new function timer function to set up the timer, similar to what
we have in the Linux kernel. This patch also converts existing opencoded
timer setup in the libosmocore tree as initial client of this new
function.

This patch implicitly removes function callback passed by reference that
defeat compile time type validation.

Compile-tested only, but I ran make check that reports success when
testing timer infrastructure.

Change-Id: I2fa49972ecaab3748b25168b26d92034e9145666
This commit is contained in:
Pablo Neira Ayuso 2017-05-08 18:00:28 +02:00
parent c65c5b4ea0
commit 44f423f117
11 changed files with 26 additions and 22 deletions

View File

@ -65,6 +65,8 @@ struct osmo_timer_list {
* timer management
*/
void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data), void *data);
void osmo_timer_add(struct osmo_timer_list *timer);
void osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds);

View File

@ -206,8 +206,7 @@ struct osmo_fsm_inst *osmo_fsm_inst_alloc(struct osmo_fsm *fsm, void *ctx, void
fi->fsm = fsm;
fi->priv = priv;
fi->log_level = log_level;
fi->timer.data = fi;
fi->timer.cb = fsm_tmr_cb;
osmo_timer_setup(&fi->timer, fsm_tmr_cb, fi);
if (id)
fi->id = talloc_strdup(fi, id);

View File

@ -640,8 +640,7 @@ static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
/* FIXME: add that time to fc->time_last_pdu and subtract it from
* current time */
fc->timer.data = fc;
fc->timer.cb = &fc_timer_cb;
osmo_timer_setup(&fc->timer, fc_timer_cb, fc);
osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
} else {
/* If the PCU is telling us to not send any more data at all,

View File

@ -238,8 +238,7 @@ struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
/* before RESET procedure: BLOCKED and DEAD */
nsvc->state = NSE_S_BLOCKED;
nsvc->nsi = nsi;
nsvc->timer.cb = gprs_ns_timer_cb;
nsvc->timer.data = nsvc;
osmo_timer_setup(&nsvc->timer, gprs_ns_timer_cb, nsvc);
nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, nsvci);

View File

@ -184,8 +184,7 @@ static int gsm411_mmsms_send_msg(struct gsm411_smc_inst *inst)
/* 5.2.3.1.2: enter MO-wait for CP-ACK */
/* 5.2.3.2.3: enter MT-wait for CP-ACK */
new_cp_state(inst, GSM411_CPS_WAIT_CP_ACK);
inst->cp_timer.data = inst;
inst->cp_timer.cb = cp_timer_expired;
osmo_timer_setup(&inst->cp_timer, cp_timer_expired, inst);
/* 5.3.2.1: Set Timer TC1A */
osmo_timer_schedule(&inst->cp_timer, inst->cp_tc1, 0);
/* clone cp_msg */

View File

@ -77,8 +77,7 @@ void gsm411_smr_init(struct gsm411_smr_inst *inst, uint64_t id, int network,
inst->rp_state = GSM411_RPS_IDLE;
inst->rl_recv = rl_recv;
inst->mn_send = mn_send;
inst->rp_timer.data = inst;
inst->rp_timer.cb = rp_timer_expired;
osmo_timer_setup(&inst->rp_timer, rp_timer_expired, inst);
LOGP(DLSMS, LOGL_INFO,
SMR_LOG_STR "instance created for %s.\n",

View File

@ -267,12 +267,10 @@ void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range,
dl->n200 = 3;
dl->t200_sec = 1;
dl->t200_usec = 0;
dl->t200.data = dl;
dl->t200.cb = &lapd_t200_cb;
osmo_timer_setup(&dl->t200, lapd_t200_cb, dl);
dl->t203_sec = 10;
dl->t203_usec = 0;
dl->t203.data = dl;
dl->t203.cb = &lapd_t203_cb;
osmo_timer_setup(&dl->t203, lapd_t203_cb, dl);
dl->maxf = maxf;
if (k > v_range - 1)
k = v_range - 1;

View File

@ -147,7 +147,7 @@ static void rate_ctr_timer_cb(void *data)
int rate_ctr_init(void *tall_ctx)
{
tall_rate_ctr_ctx = tall_ctx;
rate_ctr_timer.cb = rate_ctr_timer_cb;
osmo_timer_setup(&rate_ctr_timer, rate_ctr_timer_cb, NULL);
osmo_timer_schedule(&rate_ctr_timer, 1, 0);
return 0;

View File

@ -107,7 +107,7 @@ static int start_timer()
if (!is_initialised)
return -ESRCH;
osmo_stats_timer.cb = osmo_stats_timer_cb;
osmo_timer_setup(&osmo_stats_timer, osmo_stats_timer_cb, NULL);
osmo_timer_schedule(&osmo_stats_timer, 0, 1);
return 0;

View File

@ -65,6 +65,18 @@ static void __add_timer(struct osmo_timer_list *timer)
rb_insert_color(&timer->node, &timer_root);
}
/*! \brief set up timer callback and data
* \param[in] timer the timer that should be added
* \param[in] callback function to be called when timer expires
* \param[in] pointer to data that passed to the callback function
*/
void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data),
void *data)
{
timer->cb = cb;
timer->data = data;
}
/*! \brief add a new timer to the timer management
* \param[in] timer the timer that should be added
*/

View File

@ -39,10 +39,7 @@ static void main_timer_fired(void *data);
static void secondary_timer_fired(void *data);
static unsigned int main_timer_step = 0;
static struct osmo_timer_list main_timer = {
.cb = main_timer_fired,
.data = &main_timer_step,
};
static struct osmo_timer_list main_timer;
static LLIST_HEAD(timer_test_list);
@ -92,8 +89,7 @@ static void main_timer_fired(void *data)
return;
}
osmo_gettimeofday(&v->start, NULL);
v->timer.cb = secondary_timer_fired;
v->timer.data = v;
osmo_timer_setup(&v->timer, secondary_timer_fired, v);
unsigned int seconds = (i & 0x7) + 1;
v->stop.tv_sec = v->start.tv_sec + seconds;
v->stop.tv_usec = v->start.tv_usec;
@ -195,6 +191,7 @@ int main(int argc, char *argv[])
" %d steps of %d msecs each\n",
timer_nsteps, steps, TIME_BETWEEN_TIMER_CHECKS / 1000);
osmo_timer_setup(&main_timer, main_timer_fired, &main_timer_step);
osmo_timer_schedule(&main_timer, 1, 0);
#ifdef HAVE_SYS_SELECT_H