diff --git a/equeue.c b/equeue.c index 8655ec2..612ba57 100644 --- a/equeue.c +++ b/equeue.c @@ -64,7 +64,6 @@ int equeue_create_inplace(equeue_t *q, size_t size, void *buffer) { q->slab.data = q->buffer; q->queue = 0; - equeue_tick_init(); q->tick = equeue_tick(); q->generation = 0; q->break_requested = false; diff --git a/equeue_mbed.cpp b/equeue_mbed.cpp index 222b401..9b47fe8 100644 --- a/equeue_mbed.cpp +++ b/equeue_mbed.cpp @@ -27,13 +27,21 @@ using namespace mbed; #include "rtos/Kernel.h" #include "platform/mbed_os_timer.h" -void equeue_tick_init() { +static bool equeue_tick_inited = false; + +static void equeue_tick_init() { #if defined MBED_TICKLESS || !MBED_CONF_RTOS_PRESENT mbed::internal::init_os_timer(); #endif + + equeue_tick_inited = true; } unsigned equeue_tick() { + if (!equeue_tick_inited) { + equeue_tick_init(); + } + #if defined MBED_TICKLESS || !MBED_CONF_RTOS_PRESENT // It is not safe to call get_ms_count from ISRs, both // because documentation says so, and because it will give @@ -72,6 +80,7 @@ unsigned equeue_tick() { #define ALIAS_TIMEOUT Timeout #endif +static bool equeue_tick_inited = false; static volatile unsigned equeue_minutes = 0; static unsigned equeue_timer[ (sizeof(ALIAS_TIMER)+sizeof(unsigned)-1)/sizeof(unsigned)]; @@ -83,7 +92,7 @@ static void equeue_tick_update() { reinterpret_cast(equeue_timer)->reset(); } -void equeue_tick_init() { +static void equeue_tick_init() { MBED_STATIC_ASSERT(sizeof(equeue_timer) >= sizeof(ALIAS_TIMER), "The equeue_timer buffer must fit the class Timer"); MBED_STATIC_ASSERT(sizeof(equeue_ticker) >= sizeof(ALIAS_TICKER), @@ -94,9 +103,15 @@ void equeue_tick_init() { equeue_minutes = 0; timer->start(); ticker->attach_us(equeue_tick_update, 1000 << 16); + + equeue_tick_inited = true; } unsigned equeue_tick() { + if (!equeue_tick_inited) { + equeue_tick_init(); + } + unsigned minutes; unsigned ms; diff --git a/equeue_platform.h b/equeue_platform.h index 346e06d..a472cf6 100644 --- a/equeue_platform.h +++ b/equeue_platform.h @@ -60,7 +60,6 @@ extern "C" { // limited by the accuracy of this tick. // // Must intentionally overflow to 0 after 2^32-1 -void equeue_tick_init(void); unsigned equeue_tick(void); diff --git a/equeue_posix.c b/equeue_posix.c index 0d4062f..28bf5ae 100644 --- a/equeue_posix.c +++ b/equeue_posix.c @@ -14,9 +14,6 @@ // Tick operations -void equeue_tick_init(void) { -} - unsigned equeue_tick(void) { struct timeval tv; gettimeofday(&tv, 0);