C Microkernel Realtime eXecutive
Realtime Operating System for Cortex-M based microcontrollers
 
Loading...
Searching...
No Matches

Kernel internals providing services of delayed and periodic execution. More...

Data Structures

struct  TimerEntry_t
 Description of one sleep request. More...
 
struct  TimerQueueEntry_t
 Entry for sorting sleep requests. More...
 

Macros

#define SLEEPER_KEY_TYPE_SHIFT   24
 
#define SLEEPER_KEY(_type, _thread)   (((_type) << SLEEPER_KEY_TYPE_SHIFT) | (_thread))
 
#define TIMER_INVALID_ID   0xFF
 

Enumerations

enum  eSleepType { TIMER_SLEEP = 0 , TIMER_TIMEOUT , TIMER_PERIODIC , TIMER_INTERVAL = TIMER_PERIODIC }
 

Functions

static bool is_periodic (struct TimerEntry_t *entry)
 Determine if interval in sleep entry is periodic or not.
 
int os_find_timer_slot (Thread_t owner, enum eSleepType type)
 
int os_find_timer_queue (int timer_slot_id)
 
void os_dequeue_timed_event (unsigned queue_offs)
 
void os_enqueue_timed_event (unsigned sleeper_id, uint32_t next_resume)
 
static int do_set_timed_event (Txn_t txn, const unsigned slot, const unsigned interval, const enum eSleepType type)
 Perform heavy lifting of setting timers This routine will store timed event into list of timed events.
 
int os_set_timed_event (unsigned microseconds, enum eSleepType type)
 Find a slot for timed event and store it.
 
int os_find_timer (Thread_t owner, enum eSleepType type)
 Find a timer slot matching owner thread and timer type.
 
int os_cancel_timed_event (Thread_t owner, enum eSleepType type)
 Cancels existing timed event.
 
int os_cancel_sleeper (unsigned sleeper_queue_id)
 Cancel existing sleeper.
 
static void delay_us (uint32_t period_us)
 Perform short busy wait.
 
int os_usleep (unsigned microseconds)
 Kernel implementation of usleep() syscall.
 
int os_setitimer (unsigned microseconds)
 Kernel implementation of setitimer() syscall.
 
void os_timer_init (void)
 This routine initializes kernel scheduling subsystem.
 
bool os_schedule_timer (unsigned *delay)
 Provide information on next scheduled event.
 
void os_sleeper_reschedule (unsigned sleeper_queue_id)
 
void os_run_timer (uint32_t start_microtime, uint32_t interval)
 Fire scheduled events.
 
uint32_t os_cpu_freq_get (void)
 Get current CPU frequency.
 

Variables

struct TimerEntry_t sleepers [SLEEPERS_MAX]
 List of all delays requested from kernel.
 
struct TimerQueueEntry_t sleeper_queue [SLEEPERS_MAX]
 
unsigned sleeper_queue_size
 
unsigned sleeper_queue_cursor
 

Detailed Description

Kernel internals providing services of delayed and periodic execution.

Macro Definition Documentation

◆ SLEEPER_KEY

#define SLEEPER_KEY (   _type,
  _thread 
)    (((_type) << SLEEPER_KEY_TYPE_SHIFT) | (_thread))

◆ SLEEPER_KEY_TYPE_SHIFT

#define SLEEPER_KEY_TYPE_SHIFT   24

◆ TIMER_INVALID_ID

#define TIMER_INVALID_ID   0xFF

Enumeration Type Documentation

◆ eSleepType

enum eSleepType
Enumerator
TIMER_SLEEP 
TIMER_TIMEOUT 
TIMER_PERIODIC 
TIMER_INTERVAL 

Function Documentation

◆ delay_us()

static void delay_us ( uint32_t  period_us)
static

Perform short busy wait.

This will perform busywait in the context of current thread. Useful to do short waits for I/O.

Parameters
period_ushow long the wait should take

◆ do_set_timed_event()

static int do_set_timed_event ( Txn_t  txn,
const unsigned  slot,
const unsigned  interval,
const enum eSleepType  type 
)
static

Perform heavy lifting of setting timers This routine will store timed event into list of timed events.

If event is not periodic, it will also stop thread (os_usleep semantics).

Parameters
txntransaction used to modify the table
slotnumber of slot in sleepers list
intervalamount of us for which thread should sleep
typetype of timer to set up
Returns
0 if timer was set up properly

◆ is_periodic()

static bool is_periodic ( struct TimerEntry_t entry)
static

Determine if interval in sleep entry is periodic or not.

Returns
1 if interval is periodic, 0 if interval is one-shot.

◆ os_cancel_sleeper()

int os_cancel_sleeper ( unsigned  sleeper_queue_id)

Cancel existing sleeper.

This function will cancel existing sleeper. It is useful for cases where you already know the identity of the sleeper in advance.

Parameters
sleeper_queue_idID of the item in sorted key array
Returns
0 is operation performed successfully

◆ os_cancel_timed_event()

int os_cancel_timed_event ( Thread_t  owner,
enum eSleepType  type 
)

Cancels existing timed event.

This function is only accessible for periodic timers externally. It allows cancelling of interval timers set previously.

Parameters
ownerthread which shall own the interval timer
typetype of the event to be cancelled
Returns
0 if operation performed successfully.

◆ os_cpu_freq_get()

uint32_t os_cpu_freq_get ( void  )

Get current CPU frequency.

Will return the current CPU frequency (if known and/or available).

Returns
CPU frequency in Hz, 0 if value is not known.

◆ os_dequeue_timed_event()

void os_dequeue_timed_event ( unsigned  queue_offs)

◆ os_enqueue_timed_event()

void os_enqueue_timed_event ( unsigned  sleeper_id,
uint32_t  next_resume 
)

◆ os_find_timer()

int os_find_timer ( Thread_t  owner,
enum eSleepType  type 
)

Find a timer slot matching owner thread and timer type.

This will find a slot which bears the timer of given type for given thread.

Parameters
[in]ownerthread ID which should own the timer
[in]typetype of the timer.
Returns
offset of entry in timer queue or TIMER_INVALID_ID if no such timer was found.

◆ os_find_timer_queue()

int os_find_timer_queue ( int  timer_slot_id)

◆ os_find_timer_slot()

int os_find_timer_slot ( Thread_t  owner,
enum eSleepType  type 
)

◆ os_run_timer()

void os_run_timer ( uint32_t  start_microtime,
uint32_t  interval 
)

Fire scheduled events.

Will find and run all scheduled events in given time interval.

Parameters
microtimestarting processor time in microseconds of interval to run
intervallenght of the interval events should be fired

◆ os_schedule_timer()

bool os_schedule_timer ( unsigned *  delay)

Provide information on next scheduled event.

This function informs caller about delay until next scheduled event. Next scheduled event may be either wake-up of sleeped thread, or interval timer.

Parameters
[out]delayaddress of buffer, where delay to next scheduled event will be written
Returns
true if there is any scheduled event known and at address pointed to by delay value was written. Returns false if there is no known scheduled event. In such case content of memory pointed to by delay is undefined.

◆ os_set_timed_event()

int os_set_timed_event ( unsigned  microseconds,
enum eSleepType  type 
)

Find a slot for timed event and store it.

This is actual execution core for both os_usleep and os_setitimer functions. It will find free slot or slot already occupied by calling thread and will set / update timeout values.

Parameters
microsecondstime for which thread should sleep / wait for event
typetype of timed event to set up
Returns
error status. 0 means no error.

◆ os_setitimer()

int os_setitimer ( unsigned  microseconds)

Kernel implementation of setitimer() syscall.

See setitimer for details on arguments.

◆ os_sleeper_reschedule()

void os_sleeper_reschedule ( unsigned  sleeper_queue_id)

◆ os_timer_init()

void os_timer_init ( void  )

This routine initializes kernel scheduling subsystem.

It is necessary to call this routine before first call to either timer syscalls, or os_run_timer otherwise things will go wrong.

◆ os_usleep()

int os_usleep ( unsigned  microseconds)

Kernel implementation of usleep() syscall.

See usleep for details on arguments.

Variable Documentation

◆ sleeper_queue

struct TimerQueueEntry_t sleeper_queue[SLEEPERS_MAX]

◆ sleeper_queue_cursor

unsigned sleeper_queue_cursor

◆ sleeper_queue_size

unsigned sleeper_queue_size

◆ sleepers

struct TimerEntry_t sleepers[SLEEPERS_MAX]

List of all delays requested from kernel.

This structure contains all scheduled sleeps requested by all threads. Every thread can technically request one periodic timer and one one-shot delay.