Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cy_can/cy_can.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
#error "Invalid CY_CAN_NONBLOCKING_SPIN_LIMIT"
#endif

/// Default extent for incoming unicast messages; will grow as needed.
#define UNICAST_EXTENT_INITIAL (HEADER_BYTES + 1024U)
/// Default extent for incoming unicast messages; will grow as needed. Chosen rather arbitrarily.
#define UNICAST_EXTENT_INITIAL (CANARD_MTU_CAN_FD - 1U)

typedef struct
{
Expand Down
51 changes: 30 additions & 21 deletions lib/olga_scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@

#pragma once

#ifndef OLGA_ASSERT
#include <assert.h>
#define OLGA_ASSERT(x) assert(x)
#endif

#ifndef CAVL2_ASSERT
#define CAVL2_ASSERT(x) OLGA_ASSERT(x)
#endif

#include <cavl2.h> // Add to your include paths: https://github.com/pavel-kirienko/cavl

#include <assert.h>
#include <stdint.h>

#ifdef __cplusplus
Expand All @@ -33,6 +41,7 @@ extern "C"

typedef struct olga_t olga_t;
typedef struct olga_event_t olga_event_t;
typedef void (*olga_handler_t)(olga_t*, olga_event_t*, int64_t now);

/// Represents a user-handled future event.
/// When the handler is invoked, the event is already removed from the scheduler.
Expand All @@ -41,11 +50,11 @@ typedef struct olga_event_t olga_event_t;
/// The time units can be arbitrary.
struct olga_event_t
{
CAVL2_T base;
int64_t deadline;
uint64_t seqno;
void (*handler)(olga_t*, olga_event_t*, int64_t now);
void* user;
CAVL2_T base;
int64_t deadline;
uint64_t seqno;
olga_handler_t handler;
void* user;
};

// Convenience initializer for a fresh event (all fields zeroed, base pointers NULL).
Expand Down Expand Up @@ -78,8 +87,8 @@ typedef struct olga_spin_result_t
/// The time units can be arbitrary.
static inline void olga_init(olga_t* const self, void* const user, int64_t (*const now)(olga_t* sched))
{
assert(self != NULL);
assert(now != NULL);
OLGA_ASSERT(self != NULL);
OLGA_ASSERT(now != NULL);
self->events = NULL;
self->next_seqno = 0U;
self->now = now;
Expand Down Expand Up @@ -108,15 +117,15 @@ static inline CAVL2_RELATION olga_private_compare(const void* user, const CAVL2_
/// The event must be either zero-initialized using OLGA_EVENT_INIT or have been used at least once.
/// Events are already canceled prior to handler invocation, so it is safe to re-register immediately from the handler.
/// The complexity is logarithmic in the number of pending events.
static inline void olga_defer(olga_t* const self,
const int64_t deadline,
void* const user,
void (*const handler)(olga_t*, olga_event_t*, int64_t now),
olga_event_t* const out_event)
static inline void olga_defer(olga_t* const self,
const int64_t deadline,
void* const user,
const olga_handler_t handler,
olga_event_t* const out_event)
{
assert(self != NULL);
assert(handler != NULL);
assert(out_event != NULL);
OLGA_ASSERT(self != NULL);
OLGA_ASSERT(handler != NULL);
OLGA_ASSERT(out_event != NULL);
(void)cavl2_remove_if(&self->events, &out_event->base);
out_event->deadline = deadline;
out_event->seqno = self->next_seqno++;
Expand All @@ -130,17 +139,17 @@ static inline void olga_defer(olga_t* const self,
/// The complexity is logarithmic in the number of pending events.
static inline void olga_cancel(olga_t* const self, olga_event_t* const event)
{
assert(self != NULL);
assert(event != NULL);
OLGA_ASSERT(self != NULL);
OLGA_ASSERT(event != NULL);
(void)cavl2_remove_if(&self->events, &event->base);
event->deadline = INT64_MIN;
}

/// True if the event is currently pending in the scheduler.
static inline bool olga_is_pending(const olga_t* const self, const olga_event_t* const event)
{
assert(self != NULL);
assert(event != NULL);
OLGA_ASSERT(self != NULL);
OLGA_ASSERT(event != NULL);
return cavl2_is_inserted(self->events, &event->base);
}

Expand All @@ -150,7 +159,7 @@ static inline bool olga_is_pending(const olga_t* const self, const olga_event_t*
/// This method should be invoked regularly to pump the event loop.
static inline olga_spin_result_t olga_spin(olga_t* const self)
{
assert(self != NULL);
OLGA_ASSERT(self != NULL);
olga_spin_result_t out = { .next_deadline = INT64_MAX, .worst_lateness = 0, .now = INT64_MIN };
for (;;) { // GCOVR_EXCL_LINE
olga_event_t* const event = (olga_event_t*)cavl2_min(self->events);
Expand Down