API Reference Manual  1.45.0
odp_timer_simple.c

Minimal example application demonstrating ODP timer API usage

/* SPDX-License-Identifier: BSD-3-Clause
* Copyright (c) 2016-2018 Linaro Limited
*/
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
/* ODP main header */
#include <odp_api.h>
#include <odp/helper/odph_api.h>
int main(int argc ODP_UNUSED, char *argv[] ODP_UNUSED)
{
odp_instance_t instance;
odp_pool_t timeout_pool;
odp_timer_pool_t timer_pool;
odp_queue_t queue;
uint64_t sched_tmo;
int i, rc;
uint64_t period;
uint64_t tick;
int ret = 0;
/*
* Init ODP app
*/
if (odp_init_global(&instance, NULL, NULL))
goto err_global;
goto err_local;
/*
* Create pool for timeouts
*/
params.tmo.num = 10;
timeout_pool = odp_pool_create("timeout_pool", &params);
if (timeout_pool == ODP_POOL_INVALID) {
ret += 1;
goto err_tp;
}
/*
* Create pool of timeouts
*/
ret += 1;
goto err_tp;
}
tparams.res_ns = ODPH_MAX(10 * ODP_TIME_MSEC_IN_NS,
timer_capa.highest_res_ns);
tparams.min_tmo = 10 * ODP_TIME_MSEC_IN_NS;
tparams.max_tmo = 1 * ODP_TIME_SEC_IN_NS;
tparams.num_timers = 1; /* One timer per worker */
tparams.priv = 0; /* Shared */
timer_pool = odp_timer_pool_create("timer_pool", &tparams);
if (timer_pool == ODP_TIMER_POOL_INVALID) {
ret += 1;
goto err;
}
if (odp_timer_pool_start_multi(&timer_pool, 1) != 1) {
ODPH_ERR("Timer pool start failed\n");
ret += 1;
goto err;
}
/* Configure scheduler */
/*
* Create a queue for timer test
*/
queue = odp_queue_create("timer_queue", &qparam);
if (queue == ODP_QUEUE_INVALID) {
ret += 1;
goto err;
}
tim = odp_timer_alloc(timer_pool, queue, NULL);
if (tim == ODP_TIMER_INVALID) {
ODPH_ERR("Failed to allocate timer\n");
ret += 1;
goto err;
}
tmo = odp_timeout_alloc(timeout_pool);
if (tmo == ODP_TIMEOUT_INVALID) {
ODPH_ERR("Failed to allocate timeout\n");
return -1;
}
/* Calculate timer period in ticks */
period = odp_timer_ns_to_tick(timer_pool, 100 * ODP_TIME_MSEC_IN_NS);
/* Wait time to return from odp_schedule() if there are no
* events
*/
for (i = 0; i < 5; i++) {
odp_time_t time;
odp_timer_start_t start_param;
/* Program timeout action on current tick + period */
tick = odp_timer_current_tick(timer_pool);
start_param.tick = tick + period;
start_param.tmo_ev = ev;
rc = odp_timer_start(tim, &start_param);
/* Too early or too late timeout requested */
ODPH_ABORT("odp_timer_start() failed: %d\n", rc);
/* Wait for 2 seconds for timeout action to be generated */
ev = odp_schedule(&queue, sched_tmo);
if (ev == ODP_EVENT_INVALID)
ODPH_ABORT("Invalid event\n");
if (odp_event_type(ev) != ODP_EVENT_TIMEOUT)
ODPH_ABORT("Unexpected event type (%u) received\n",
time = odp_time_global();
printf("timer tick %d, time ns %" PRIu64 "\n",
i, odp_time_to_ns(time));
/* Do not free current event, just go back to loop and program
* the next timeout.
*/
}
/* Destroy created resources */
if (odp_timer_free(tim))
ret++;
if (odp_queue_destroy(queue))
ret++;
err:
err_tp:
if (odp_pool_destroy(timeout_pool))
ret++;
ret++;
err_local:
if (odp_term_global(instance))
ret++;
err_global:
return ret;
}
#define odp_unlikely(x)
Branch unlikely taken.
Definition: spec/hints.h:64
#define ODP_UNUSED
Intentionally unused variables of functions.
Definition: spec/hints.h:54
void odp_event_free(odp_event_t event)
Free event.
odp_event_type_t odp_event_type(odp_event_t event)
Event type of an event.
#define ODP_EVENT_INVALID
Invalid event.
int odp_init_local(odp_instance_t instance, odp_thread_type_t thr_type)
Thread local ODP initialization.
int odp_init_global(odp_instance_t *instance, const odp_init_t *params, const odp_platform_init_t *platform_params)
Global ODP initialization.
int odp_term_local(void)
Thread local ODP termination.
int odp_term_global(odp_instance_t instance)
Global ODP termination.
uint64_t odp_instance_t
ODP instance ID.
odp_pool_t odp_pool_create(const char *name, const odp_pool_param_t *param)
Create a pool.
void odp_pool_param_init(odp_pool_param_t *param)
Initialize pool params.
int odp_pool_destroy(odp_pool_t pool)
Destroy a pool previously created by odp_pool_create()
#define ODP_POOL_INVALID
Invalid pool.
@ ODP_POOL_TIMEOUT
Timeout pool.
void odp_queue_param_init(odp_queue_param_t *param)
Initialize queue params.
#define ODP_QUEUE_INVALID
Invalid queue.
odp_queue_t odp_queue_create(const char *name, const odp_queue_param_t *param)
Queue create.
int odp_queue_destroy(odp_queue_t queue)
Destroy ODP queue.
@ ODP_QUEUE_TYPE_SCHED
Scheduled queue.
#define ODP_SCHED_SYNC_PARALLEL
Parallel scheduled queues.
int odp_schedule_default_prio(void)
Default scheduling priority level.
int odp_schedule_config(const odp_schedule_config_t *config)
Global schedule configuration.
uint64_t odp_schedule_wait_time(uint64_t ns)
Schedule wait time.
odp_event_t odp_schedule(odp_queue_t *from, uint64_t wait)
Schedule an event.
#define ODP_SCHED_GROUP_ALL
Group of all threads.
@ ODP_THREAD_CONTROL
Control thread.
uint64_t odp_time_to_ns(odp_time_t time)
Convert time to nanoseconds.
#define ODP_TIME_SEC_IN_NS
A second in nanoseconds.
odp_time_t odp_time_global(void)
Current global time.
#define ODP_TIME_MSEC_IN_NS
A millisecond in nanoseconds.
int odp_timer_pool_start_multi(odp_timer_pool_t timer_pool[], int num)
Start timer pools.
odp_timeout_t odp_timeout_alloc(odp_pool_t pool)
Timeout alloc.
int odp_timer_free(odp_timer_t timer)
Free a timer.
#define ODP_TIMER_POOL_INVALID
Invalid timer pool handle.
odp_timer_pool_t odp_timer_pool_create(const char *name, const odp_timer_pool_param_t *params)
Create a timer pool.
uint64_t odp_timer_current_tick(odp_timer_pool_t timer_pool)
Current tick value.
int odp_timer_capability(odp_timer_clk_src_t clk_src, odp_timer_capability_t *capa)
Query timer capabilities per clock source.
uint64_t odp_timer_ns_to_tick(odp_timer_pool_t timer_pool, uint64_t ns)
Convert nanoseconds to timer ticks.
int odp_timer_start(odp_timer_t timer, const odp_timer_start_t *start_param)
Start a timer.
odp_event_t odp_timeout_to_event(odp_timeout_t tmo)
Convert timeout handle to event handle.
#define ODP_TIMEOUT_INVALID
Invalid timeout handle.
odp_timer_t odp_timer_alloc(odp_timer_pool_t timer_pool, odp_queue_t queue, const void *user_ptr)
Allocate a timer.
#define ODP_CLOCK_DEFAULT
The default clock source.
#define ODP_TIMER_INVALID
Invalid timer handle.
void odp_timer_pool_param_init(odp_timer_pool_param_t *param)
Initialize timer pool parameters.
void odp_timer_pool_destroy(odp_timer_pool_t timer_pool)
Destroy a timer pool.
@ ODP_TIMER_SUCCESS
Timer operation succeeded.
@ ODP_TIMER_TICK_ABS
Absolute ticks.
The OpenDataPlane API.
Pool parameters.
uint32_t num
Number of buffers in the pool.
struct odp_pool_param_t::@124 tmo
Parameters for timeout pools.
odp_pool_type_t type
Pool type.
ODP Queue parameters.
odp_schedule_param_t sched
Scheduler parameters.
odp_queue_type_t type
Queue type.
odp_schedule_group_t group
Thread group.
odp_schedule_prio_t prio
Priority level.
odp_schedule_sync_t sync
Synchronization method.
uint64_t highest_res_ns
Highest timer resolution in nanoseconds.
Timer pool parameters.
uint64_t res_ns
Timeout resolution in nanoseconds.
int priv
Thread private timer pool.
uint64_t min_tmo
Minimum relative timeout in nanoseconds.
uint32_t num_timers
Number of timers in the pool.
odp_timer_clk_src_t clk_src
Clock source for timers.
uint64_t max_tmo
Maximum relative timeout in nanoseconds.
Timer start parameters.
uint64_t tick
Expiration time in ticks.
odp_event_t tmo_ev
Timeout event.
odp_timer_tick_type_t tick_type
Tick type.