Skip to content

Commit

Permalink
Merge pull request #431 from lf-lang/zephyr
Browse files Browse the repository at this point in the history
Zephyr: Reset sleeping semaphore and correctly check its return values
  • Loading branch information
lhstrh authored May 20, 2024
2 parents 0a42722 + 961903f commit 40042db
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions low_level_platform/impl/src/lf_zephyr_clock_kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <zephyr/kernel.h>
#include <errno.h>

#include "platform/lf_zephyr_support.h"
#include "low_level_platform.h"
Expand Down Expand Up @@ -86,6 +87,10 @@ int _lf_interruptable_sleep_until_locked(environment_t* env, instant_t wakeup) {
return 0;
}

// Reset the semaphore. This is safe to do before we leave the critical
// section.
k_sem_reset(&sleeping_sem);

if (lf_critical_section_exit(env)) {
lf_print_error_and_exit("Failed to exit critical section.");
}
Expand All @@ -96,11 +101,18 @@ int _lf_interruptable_sleep_until_locked(environment_t* env, instant_t wakeup) {
lf_print_error_and_exit("Failed to exit critical section.");
}

if (res < 0 || async_event == true) {
if (res == 0) {
// We got the semaphore, this means there should be a new event
if (!async_event) {
lf_print_warning("Sleep was interrupted, but no new event");
}
async_event = false;
return -1;
} else {
} else if (res == -EAGAIN) {
// This means we timed out and have reached our wakeup instant.
return 0;
} else {
lf_print_error_and_exit("k_sem_take returned %d", res);
}
}

Expand Down

0 comments on commit 40042db

Please sign in to comment.