Skip to content

Commit

Permalink
renamed libtock_alarm_repeating_t
Browse files Browse the repository at this point in the history
Renamed libtock_alarm_repeating_t to libtock_alarm_t since the
previous name suggested it was only used by libtock_alarm_repeating_ms
when it was also used by libtock_alarm_in_ms. This new name is more
general and can apply to either function. This must be changed to
support the new fields for overflow.

Co-authored-by: Samir Rashid <Samir-Rashid@godsped.com>
  • Loading branch information
atar13 and Samir-Rashid committed Jun 20, 2024
1 parent 6e6b9f7 commit b9cdf9e
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 55 deletions.
4 changes: 2 additions & 2 deletions examples/tests/alarm_in_overflow/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static uint32_t get_time_ms(void) {
// callback of type subscribe_upcall which will get called when alarm fires
static void alarm_fire_callback(__attribute__ ((unused)) uint32_t unused0,
__attribute__ ((unused)) uint32_t unused1,
void* user_data) {
void* user_data) {
fired = true;

uint32_t expected_alarm_end_ms = *((uint32_t*)user_data);
Expand All @@ -56,7 +56,7 @@ static void alarm_fire_callback(__attribute__ ((unused)) uint32_t unused0,

int main(void) {
// need to allocate memory for a alarm before giving a pointer to `libtock_alarm_in_ms`
libtock_alarm_repeating_t* alarm = malloc(sizeof(libtock_alarm_repeating_t));
libtock_alarm_t* alarm = malloc(sizeof(libtock_alarm_t));

uint32_t alarm_start_ms = get_time_ms();

Expand Down
8 changes: 4 additions & 4 deletions examples/tests/gpio/gpio_original/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ static void gpio_output(void) {
libtock_gpio_enable_output(0);
// Start repeating alarm
data.fired = false;
libtock_alarm_repeating_t alarm_repeating;
libtock_alarm_repeating_every(1000, alarm_cb, NULL, &alarm_repeating);
libtock_alarm_t alarm_repeating;
libtock_alarm_repeating_every_ms(1000, alarm_cb, NULL, &alarm_repeating);

while (1) {
yield_for(&data.fired);
Expand All @@ -64,9 +64,9 @@ static void gpio_input(void) {
// set userspace pin 0 as input and start repeating alarm
// pin is configured with a pull-down resistor, so it should read 0 as default
libtock_gpio_enable_input(0, libtock_pull_down);
libtock_alarm_repeating_t alarm_repeating;
libtock_alarm_t alarm_repeating;
data.fired = false;
libtock_alarm_repeating_every(500, alarm_cb, NULL, &alarm_repeating);
libtock_alarm_repeating_every_ms(500, alarm_cb, NULL, &alarm_repeating);

while (1) {
// print pin value
Expand Down
4 changes: 2 additions & 2 deletions examples/tests/multi_alarm_test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ static int interval;
typedef struct {
int led;
libtock_alarm_t timer;
libtock_alarm_repeating_t repeating;
libtock_alarm_t repeating;
} timer_data;

static void toggle(int led_num) {
Expand All @@ -28,7 +28,7 @@ static void start_cb(__attribute__ ((unused)) uint32_t now,
__attribute__ ((unused)) uint32_t expiration,
void* ud) {
timer_data* td = (timer_data*)ud;
libtock_alarm_repeating_every(interval, event_cb, ud, &td->repeating);
libtock_alarm_repeating_every_ms(interval, event_cb, ud, &td->repeating);
toggle(td->led);
}

Expand Down
4 changes: 2 additions & 2 deletions libnrfserialization/serialization_tock.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,8 @@ uint32_t app_timer_start (app_timer_id_t timer_id,
p_node->p_context = p_context;
// timer_repeating_subscribe(p_node->p_timeout_handler, &timer_id);
// Use 0 for the prescaler
libtock_alarm_repeating_t* timer = (libtock_alarm_repeating_t*)malloc(sizeof(libtock_alarm_repeating_t));
libtock_alarm_repeating_every(APP_TIMER_MS(timeout_ticks, 0), serialization_timer_cb, timer_id, timer);
libtock_alarm_t* timer = (libtock_alarm_t*)malloc(sizeof(libtock_alarm_t));
libtock_alarm_repeating_every_ms(APP_TIMER_MS(timeout_ticks, 0), serialization_timer_cb, timer_id, timer);
} else {
// timer_oneshot_subscribe(p_node->p_timeout_handler, &timer_id);
}
Expand Down
6 changes: 3 additions & 3 deletions libopenthread/platform/alarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <assert.h>


static libtock_alarm_t alarm;
static libtock_alarm_repeating_t timer_wrap;
static libtock_alarm_ticks_t alarm;
static libtock_alarm_t timer_wrap;
static uint32_t frequency = 0;

// maintain global variables to be used for timer wrapping logic
Expand Down Expand Up @@ -80,7 +80,7 @@ void init_otPlatAlarm(void) {
// account for leftover ticks
uint64_t left_over_ticks = ((UINT32_MAX % frequency) * 1000) / frequency;
wrap_point += (uint32_t) left_over_ticks;
libtock_alarm_repeating_every(wrap_point >> 1, wrap_time_upcall, NULL, &timer_wrap);
libtock_alarm_repeating_every_ms(wrap_point >> 1, wrap_time_upcall, NULL, &timer_wrap);
prev_time_value = otPlatAlarmMilliGetNow();
}

Expand Down
6 changes: 3 additions & 3 deletions libtock-sync/services/alarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static void delay_cb(__attribute__ ((unused)) uint32_t now,

int libtocksync_alarm_delay_ms(uint32_t ms) {
delay_data.fired = false;
libtock_alarm_repeating_t alarm;
libtock_alarm_t alarm;
int rc;

if ((rc = libtock_alarm_in_ms(ms, delay_cb, NULL, &alarm)) != RETURNCODE_SUCCESS) {
Expand All @@ -35,7 +35,7 @@ static void yf_timeout_cb(__attribute__ ((unused)) uint32_t now,

int libtocksync_alarm_yield_for_with_timeout(bool* cond, uint32_t ms) {
yf_timeout_data.fired = false;
libtock_alarm_repeating_t alarm;
libtock_alarm_t alarm;
libtock_alarm_in_ms(ms, yf_timeout_cb, NULL, &alarm);

while (!*cond) {
Expand All @@ -46,6 +46,6 @@ int libtocksync_alarm_yield_for_with_timeout(bool* cond, uint32_t ms) {
yield();
}

libtock_alarm_repeating_cancel(&alarm);
libtock_alarm_ms_cancel(&alarm);
return RETURNCODE_SUCCESS;
}
4 changes: 2 additions & 2 deletions libtock-sync/services/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ struct unit_test_t {
int pid;

// alarm structure used for triggering test timeout conditions.
libtock_alarm_repeating_t alarm;
libtock_alarm_t alarm;

// Result of the most recently completed test.
unit_test_result_t result;
Expand Down Expand Up @@ -386,7 +386,7 @@ static void unit_test_service_callback(int pid,
case TestEnd:
// Cancel the timeout alarm since the test is now complete.
// Record the test result for the test summary statistics.
libtock_alarm_repeating_cancel(&test->alarm);
libtock_alarm_ms_cancel(&test->alarm);

// If the test timed out, the summary results will already have been
// printed. In this case, we no longer want the tests to continue,
Expand Down
42 changes: 21 additions & 21 deletions libtock/services/alarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,24 @@ static uint32_t ticks_to_ms(uint32_t ticks) {
return milliseconds;
}

static libtock_alarm_t* root = NULL;
static libtock_alarm_ticks_t* root = NULL;

static void root_insert(libtock_alarm_t* alarm) {
static void root_insert(libtock_alarm_ticks_t* alarm) {
if (root == NULL) {
root = alarm;
root->next = NULL;
root->prev = NULL;
return;
}

libtock_alarm_t** cur = &root;
libtock_alarm_t* prev = NULL;
libtock_alarm_ticks_t** cur = &root;
libtock_alarm_ticks_t* prev = NULL;
while (*cur != NULL) {
uint32_t cur_expiration = (*cur)->reference + (*cur)->dt;
uint32_t new_expiration = alarm->reference + alarm->dt;
if (!within_range(alarm->reference, cur_expiration, new_expiration)) {
// insert before
libtock_alarm_t* tmp = *cur;
libtock_alarm_ticks_t* tmp = *cur;
*cur = alarm;
alarm->next = tmp;
alarm->prev = prev;
Expand All @@ -106,11 +106,11 @@ static void root_insert(libtock_alarm_t* alarm) {

}

static libtock_alarm_t* root_pop(void) {
static libtock_alarm_ticks_t* root_pop(void) {
if (root == NULL) {
return NULL;
} else {
libtock_alarm_t* res = root;
libtock_alarm_ticks_t* res = root;
root = root->next;
if (root != NULL) {
root->prev = NULL;
Expand All @@ -120,15 +120,15 @@ static libtock_alarm_t* root_pop(void) {
}
}

static libtock_alarm_t* root_peek(void) {
static libtock_alarm_ticks_t* root_peek(void) {
return root;
}

static void alarm_upcall(__attribute__ ((unused)) int kernel_now,
__attribute__ ((unused)) int scheduled,
__attribute__ ((unused)) int unused2,
__attribute__ ((unused)) void* opaque) {
for (libtock_alarm_t* alarm = root_peek(); alarm != NULL; alarm = root_peek()) {
for (libtock_alarm_ticks_t* alarm = root_peek(); alarm != NULL; alarm = root_peek()) {
uint32_t now;
libtock_alarm_command_read(&now);
// has the alarm not expired yet? (distance from `now` has to be larger or
Expand All @@ -148,7 +148,7 @@ static void alarm_upcall(__attribute__ ((unused)) int kernel_now,
}

static int libtock_alarm_at_internal(uint32_t reference, uint32_t dt, libtock_alarm_callback cb, void* ud,
libtock_alarm_t* alarm) {
libtock_alarm_ticks_t* alarm) {
alarm->reference = reference;
alarm->dt = dt;
alarm->callback = cb;
Expand All @@ -158,7 +158,7 @@ static int libtock_alarm_at_internal(uint32_t reference, uint32_t dt, libtock_al

root_insert(alarm);
int i = 0;
for (libtock_alarm_t* cur = root_peek(); cur != NULL; cur = cur->next) {
for (libtock_alarm_ticks_t* cur = root_peek(); cur != NULL; cur = cur->next) {
i++;
}

Expand All @@ -170,11 +170,12 @@ static int libtock_alarm_at_internal(uint32_t reference, uint32_t dt, libtock_al
return RETURNCODE_SUCCESS;
}

int libtock_alarm_at(uint32_t reference, uint32_t dt, libtock_alarm_callback cb, void* opaque, libtock_alarm_t* alarm) {
int libtock_alarm_at(uint32_t reference, uint32_t dt, libtock_alarm_callback cb, void* opaque,
libtock_alarm_ticks_t* alarm) {
return libtock_alarm_at_internal(reference, dt, cb, opaque, alarm);
}

void libtock_alarm_cancel(libtock_alarm_t* alarm) {
void libtock_alarm_cancel(libtock_alarm_ticks_t* alarm) {
if (alarm->prev != NULL) {
alarm->prev->next = alarm->next;
}
Expand All @@ -200,7 +201,7 @@ void libtock_alarm_cancel(libtock_alarm_t* alarm) {
static void overflow_callback(__attribute__ ((unused)) uint32_t now,
uint32_t last_timer_fire_time,
void* overflow_ud) {
libtock_alarm_repeating_t* tock_timer = (libtock_alarm_repeating_t*)overflow_ud;
libtock_alarm_t* tock_timer = (libtock_alarm_t*)overflow_ud;

if (tock_timer->overflows_left == 0) {
// no overflows left, schedule last alarm with original callback
Expand All @@ -221,7 +222,7 @@ static void overflow_callback(__attribute__ ((unused)) uint32_t now,
}
}

int libtock_alarm_in_ms(uint32_t ms, libtock_alarm_callback cb, void* opaque, libtock_alarm_repeating_t* alarm) {
int libtock_alarm_in_ms(uint32_t ms, libtock_alarm_callback cb, void* opaque, libtock_alarm_t* alarm) {
uint32_t now;
int ret = libtock_alarm_command_read(&now);
if (ret != RETURNCODE_SUCCESS) return ret;
Expand Down Expand Up @@ -250,8 +251,8 @@ int libtock_alarm_in_ms(uint32_t ms, libtock_alarm_callback cb, void* opaque, li
}

static void alarm_repeating_cb(uint32_t now, __attribute__ ((unused)) uint32_t scheduled, void* opaque) {
libtock_alarm_repeating_t* repeating = (libtock_alarm_repeating_t*) opaque;
uint32_t interval_ms = repeating->interval_ms;
libtock_alarm_t* repeating = (libtock_alarm_t*) opaque;
uint32_t interval_ms = repeating->interval_ms;
// It's possible for the call to ms_to_ticks to overflow if interval_ms is greater
// than 2^32 ticks, but the wraparound gives use the expiration time we want.
uint32_t cur_exp = repeating->alarm.reference + ms_to_ticks(interval_ms);
Expand All @@ -262,17 +263,16 @@ static void alarm_repeating_cb(uint32_t now, __attribute__ ((unused)) uint32_t s


void libtock_alarm_repeating_every_ms(uint32_t ms, libtock_alarm_callback cb, void* opaque,
libtock_alarm_repeating_t* repeating) {
libtock_alarm_t* repeating) {
repeating->interval_ms = ms;
repeating->callback = cb;
repeating->user_data = opaque;

libtock_alarm_in_ms(ms, (libtock_alarm_callback)alarm_repeating_cb, (void*)repeating, repeating);
}

// TODO: should there also be a cancel called `libtock_alarm_in_ms_cancel`
void libtock_alarm_repeating_cancel(libtock_alarm_repeating_t* repeating) {
libtock_alarm_cancel(&repeating->alarm);
void libtock_alarm_ms_cancel(libtock_alarm_t* alarm) {
libtock_alarm_cancel(&alarm->alarm);
}

int libtock_alarm_gettimeasticks(struct timeval* tv, __attribute__ ((unused)) void* tzvp) {
Expand Down
33 changes: 17 additions & 16 deletions libtock/services/alarm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This module allows the client to initiate alarms and receive
* callbacks when those alarms have expired. Clients can set one-shot alarms to
* fire at particular clock values (`libtock_alarm_at`) or periodic alarms
* (`libtock_alarm_repeating_every`)
* (`libtock_alarm_repeating_every_ms`)
*
* The client should not assume anything about the underlying clock used by an
* implementation other than that it is running at sufficient frequency to
Expand Down Expand Up @@ -43,13 +43,14 @@ typedef struct alarm {
void* ud;
struct alarm* next;
struct alarm* prev;
} libtock_alarm_t;
} libtock_alarm_ticks_t;

/** \brief Opaque handle to a repeating alarm.
*
* An opaque handle to a repeating alarm created by `libtock_alarm_repeating_every`.
* An opaque handle to an alarm created by `libtock_alarm_repeating_every_ms`
* and `libtock_alarm_in_ms`.
*/
typedef struct alarm_repeating {
typedef struct alarm_data {
// Length of timer in milliseconds.
uint32_t interval_ms;
// Number of times the underlying counter will overflow
Expand All @@ -60,8 +61,8 @@ typedef struct alarm_repeating {
uint32_t remaining_ticks;
libtock_alarm_callback callback;
void* user_data;
libtock_alarm_t alarm;
} libtock_alarm_repeating_t;
libtock_alarm_ticks_t alarm;
} libtock_alarm_t;


/** \brief Create a new alarm to fire at a particular clock value.
Expand All @@ -70,7 +71,7 @@ typedef struct alarm_repeating {
* the alarm is outstanding. `reference` and `dt` are in terms of the tick
* time.
*
* Alarms longer than 2^32 ticks should use `timer_in`.
* Alarms longer than 2^32 ticks should use `libtock_alarm_in_ms`.
*
* \param reference the reference time from which the alarm is being set in ticks.
* \param dt the time after reference that the alarm should fire in ticks.
Expand All @@ -81,15 +82,15 @@ typedef struct alarm_repeating {
* \return An error code. Either RETURNCODE_SUCCESS or RETURNCODE_FAIL.
*/
int libtock_alarm_at(uint32_t reference, uint32_t dt, libtock_alarm_callback callback, void* opaque,
libtock_alarm_t* alarm);
libtock_alarm_ticks_t* alarm);

/** \brief Cancels an existing alarm.
*
* The caller is responsible for freeing the `alarm_t`.
*
* \param alarm
*/
void libtock_alarm_cancel(libtock_alarm_t*);
void libtock_alarm_cancel(libtock_alarm_ticks_t* alarm);

// Use this to implement _gettimeofday yourself as libtock-c doesn't provide
// an implementation.
Expand Down Expand Up @@ -118,27 +119,27 @@ int libtock_alarm_gettimeasticks(struct timeval* tv, void* tzvp);
* \param alarm handle to the alarm that was created.
* \return An error code. Either RETURNCODE_SUCCESS or RETURNCODE_FAIL.
*/
int libtock_alarm_in_ms(uint32_t ms, libtock_alarm_callback cb, void* opaque, libtock_alarm_repeating_t* alarm);
int libtock_alarm_in_ms(uint32_t ms, libtock_alarm_callback cb, void* opaque, libtock_alarm_t* alarm);

/** \brief Create a new repeating alarm to fire every `ms` milliseconds.
*
* The `alarm_repeating` parameter is allocated by the caller and must live as long as
* The `alarm` parameter is allocated by the caller and must live as long as
* the repeating alarm is outstanding.
*
* \param ms the interval to fire the alarm at in milliseconds.
* \param cb a callback to be invoked when the alarm expires.
* \param opaque pointer passed to the callback.
* \param alarm_repeating pointer to a new alarm_repeating_t to be used by the implementation to
* \param alarm pointer to a new libtock_alarm_t to be used by the implementation to
* keep track of the alarm.
*/
void libtock_alarm_repeating_every_ms(uint32_t ms, libtock_alarm_callback cb, void* opaque,
libtock_alarm_repeating_t* alarm_repeating);
libtock_alarm_t* alarm);

/** \brief Cancels an existing repeating alarm.
/** \brief Cancels an existing alarm set in milliseconds.
*
* \param alarm_repeating
* \param alarm to cancel.
*/
void libtock_alarm_repeating_cancel(libtock_alarm_repeating_t* alarm_repeating);
void libtock_alarm_ms_cancel(libtock_alarm_t* alarm);

#ifdef __cplusplus
}
Expand Down

0 comments on commit b9cdf9e

Please sign in to comment.