fplayer
C99 Falcon Sequence (fseq) file player for Light-O-Rama hardware (like xLights)
Loading...
Searching...
No Matches
sleep.c File Reference

Adaptive sleep time controller implementation. More...

#include "sleep.h"
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "std2/errcode.h"
Include dependency graph for sleep.c:

Data Structures

struct  sleep_coll_s
 Sleep collector structure for providing historical sleep performance data to better estimate sleep times. More...
 

Macros

#define SLEEP_COLL_SAMPLE_COUNT   20
 Number of sleep time samples to store in the sleep collector.
 

Functions

int Sleep_init (struct sleep_coll_s **coll)
 Allocates and initializes a new sleep collector with default values.
 
static void Sleep_record (struct sleep_coll_s *coll, const int64_t ns)
 Appends the given sleep time to the sleep collector, updating the collector's internal state. This should be called after each sleep operation to provide historical data for better sleep time estimation. Once full, the collector will overwrite the oldest sleep time.
 
int64_t Sleep_average (const struct sleep_coll_s *coll)
 Returns the average sleep time in nanoseconds from the given sleep collector. This is calculated by summing all sleep times in the collector and dividing by the number of samples.
 
static void Sleep_oneMilli (void)
 Sleeps for one millisecond using the system's equivalent sleep function. This is used to provide a baseline for sleep time estimation.
 
static void Sleep_step (const int64_t ns)
 Sleeps for the given number of nanoseconds using incremental sleep calls that estimate the time remaining to sleep using historical sleep data.
 
static void Sleep_spinLock (const int64_t ns)
 Sleeps for the given number of nanoseconds using a spin lock. This is a blocking operation that will consume CPU cycles until the given time has elapsed.
 
void Sleep_do (struct sleep_coll_s *coll, const uint32_t ms)
 Sleeps for the given number of milliseconds using incremental sleep calls that estimate the time remaining to sleep using historical sleep data. The remaining sleep time is performed using a primitive spin lock to ensure the executed sleep time is accurate.
 

Detailed Description

Adaptive sleep time controller implementation.

Macro Definition Documentation

◆ SLEEP_COLL_SAMPLE_COUNT

#define SLEEP_COLL_SAMPLE_COUNT   20

Number of sleep time samples to store in the sleep collector.

Function Documentation

◆ Sleep_average()

int64_t Sleep_average ( const struct sleep_coll_s * coll)

Returns the average sleep time in nanoseconds from the given sleep collector. This is calculated by summing all sleep times in the collector and dividing by the number of samples.

Parameters
colltarget sleep collector
Returns
the average sleep time in nanoseconds
Here is the caller graph for this function:

◆ Sleep_do()

void Sleep_do ( struct sleep_coll_s * coll,
uint32_t ms )

Sleeps for the given number of milliseconds using incremental sleep calls that estimate the time remaining to sleep using historical sleep data. The remaining sleep time is performed using a primitive spin lock to ensure the executed sleep time is accurate.

Parameters
colltarget sleep collector for recording historical sleep data
msnumber of milliseconds to sleep
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Sleep_init()

int Sleep_init ( struct sleep_coll_s ** coll)

Allocates and initializes a new sleep collector with default values.

Parameters
colltarget sleep collector
Returns
0 on success, a negative error code on failure
Here is the caller graph for this function:

◆ Sleep_oneMilli()

static void Sleep_oneMilli ( void )
inlinestatic

Sleeps for one millisecond using the system's equivalent sleep function. This is used to provide a baseline for sleep time estimation.

Here is the caller graph for this function:

◆ Sleep_record()

static void Sleep_record ( struct sleep_coll_s * coll,
const int64_t ns )
static

Appends the given sleep time to the sleep collector, updating the collector's internal state. This should be called after each sleep operation to provide historical data for better sleep time estimation. Once full, the collector will overwrite the oldest sleep time.

Parameters
colltarget sleep collector
nssleep time in nanoseconds
Here is the caller graph for this function:

◆ Sleep_spinLock()

static void Sleep_spinLock ( const int64_t ns)
static

Sleeps for the given number of nanoseconds using a spin lock. This is a blocking operation that will consume CPU cycles until the given time has elapsed.

Parameters
nsnumber of nanoseconds to sleep
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Sleep_step()

static void Sleep_step ( const int64_t ns)
static

Sleeps for the given number of nanoseconds using incremental sleep calls that estimate the time remaining to sleep using historical sleep data.

Parameters
nsnumber of nanoseconds to sleep
Here is the call graph for this function:
Here is the caller graph for this function: