Skip to content

Commit 3763057

Browse files
alevyppannuto
authored andcommitted
libtock-sync: alarm sleep_for: use yield-waitfor
1 parent 1285a0e commit 3763057

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

libtock-sync/services/alarm.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
#include "alarm.h"
22

3+
// Declare non-public export of helper from libtock.
4+
uint32_t _ms_to_ticks(uint32_t ms);
5+
36
struct alarm_cb_data {
47
bool fired;
58
};
69

7-
static struct alarm_cb_data delay_data = { .fired = false };
8-
9-
static void delay_cb(__attribute__ ((unused)) uint32_t now,
10-
__attribute__ ((unused)) uint32_t scheduled,
11-
__attribute__ ((unused)) void* opaque) {
12-
delay_data.fired = true;
13-
}
14-
1510
int libtocksync_alarm_delay_ms(uint32_t ms) {
16-
delay_data.fired = false;
17-
libtock_alarm_t alarm;
1811
int rc;
19-
20-
if ((rc = libtock_alarm_in_ms(ms, delay_cb, NULL, &alarm)) != RETURNCODE_SUCCESS) {
12+
uint32_t ticks = _ms_to_ticks(ms);
13+
if ((rc = libtock_alarm_command_set_relative_blind(ticks)) != RETURNCODE_SUCCESS) {
2114
return rc;
2215
}
2316

24-
yield_for(&delay_data.fired);
17+
yield_waitfor_return_t yval = yield_wait_for(DRIVER_NUM_ALARM, 1);
18+
if (yval.data0 != RETURNCODE_SUCCESS) return yval.data0;
19+
2520
return rc;
2621
}
2722

libtock-sync/services/alarm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <libtock/peripherals/syscalls/alarm_syscalls.h>
34
#include <libtock/services/alarm.h>
45
#include <libtock/tock.h>
56

libtock/peripherals/syscalls/alarm_syscalls.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ int libtock_alarm_command_set_relative(uint32_t dt, uint32_t* actual) {
2929
return tock_command_return_u32_to_returncode(rval, actual);
3030
}
3131

32+
int libtock_alarm_command_set_relative_blind(uint32_t dt) {
33+
syscall_return_t rval = command(DRIVER_NUM_ALARM, 5, dt, 0);
34+
return tock_command_return_novalue_to_returncode(rval);
35+
}
36+
3237
int libtock_alarm_command_set_absolute(uint32_t reference, uint32_t dt) {
3338
syscall_return_t rval = command(DRIVER_NUM_ALARM, 6, reference, dt);
3439
uint32_t unused;

libtock/peripherals/syscalls/alarm_syscalls.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ int libtock_alarm_command_stop(void);
4646
*/
4747
int libtock_alarm_command_set_relative(uint32_t dt, uint32_t* actual);
4848

49+
/*
50+
* Starts a oneshot alarm
51+
*
52+
* expiration - relative expiration value from when kernel handles syscall.
53+
*
54+
* Side-effects: cancels any existing/outstanding alarms
55+
*/
56+
int libtock_alarm_command_set_relative_blind(uint32_t dt);
57+
4958
/*
5059
* Starts a oneshot alarm
5160
*

libtock/services/alarm.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ static uint32_t ms_to_ticks(uint32_t ms) {
6464
return ticks;
6565
}
6666

67+
68+
// Declare non-public export of helper for libtock-sync.
69+
uint32_t _ms_to_ticks(uint32_t ms);
70+
/** \brief Private export of ms->ticks helper to libtock-sync
71+
*
72+
* This is a non-stable, non-public interface for a helper
73+
* function which is also useful to libtock-sync.
74+
*/
75+
uint32_t _ms_to_ticks(uint32_t ms) {
76+
return ms_to_ticks(ms);
77+
}
78+
6779
uint32_t libtock_alarm_ticks_to_ms(uint32_t ticks) {
6880
// `ticks_to_ms`'s conversion will be accurate to within the range
6981
// 0 to 1 milliseconds less than the exact conversion

0 commit comments

Comments
 (0)