Post · 3 min read· pp. 446–448

Sleep

Sleep

NuttX provides three different types of sleep interfaces.

Common Sleep Interfaces

Scheduled Sleep Interfaces (tick-based)

Suspend the calling thread for a specified amount of time until the time expires or the thread is explicitly woken up through scheduler operations.

void nxsched_abstick_sleep(clock_t ticks) : Suspends the calling thread from execution until the specified absolute time in clock ticks.

code
Parameters:
:   - **ticks** – Absolute time in clock ticks.

void nxsched_ticksleep(unsigned int ticks) : Suspends the calling thread from execution for the specified number of system ticks.

code
Parameters:
:   - **ticks** – The number of system ticks to sleep.

void nxsched_usleep(useconds_t usec) : Suspends the calling thread from execution for the specified number of microseconds.

code
Parameters:
:   - **usec** – The number of microseconds to sleep.

void nxsched_msleep(unsigned int msec) : Suspends the calling thread from execution for the specified number of milliseconds.

code
Parameters:
:   - **msec** – The number of milliseconds to sleep.

void nxsched_sleep(unsigned int sec) : Suspends the calling thread from execution for the specified number of seconds.

code
Parameters:
:   - **sec** – The number of seconds to sleep.

int nxsched_nanosleep(const struct timespec *rqtp, struct timespec *rmtp) : Suspends the calling thread from execution for the specified rqtp argument. This function will return the remaining time via updating rmtp if the sleep is interrupted by a signal.

code
Parameters:
:   - **rqtp** – The amount of time to be suspended from execution.
    - **rmtp** – If the rmtp argument is non-NULL, the timespec structure referenced
      by it is updated to contain the amount of time remaining.

Returns:
:   0 (OK), or negated errno if unsuccessful.

void nxsched_wakeup(struct tcb_s *tcb) : This function is used to wake up a thread that is currently in sleeping state before its timeout expires.

code
Parameters:
:   - **tcb** – Pointer to the TCB of the task to be awakened.

Signal-based Sleep Interfaces (timespec-based)

Suspend the calling thread for a specified amount of time until the time expires or a signal is delivered to the calling thread.

Note

Implementations are dependent on the signal framework and based on standard timespec conversion.

void nxsig_usleep(useconds_t usec) : Suspends the calling thread from execution for the specified number of microseconds.

code
Parameters:
:   - **usec** – The number of microseconds to sleep.

void nxsig_sleep(unsigned int sec) : Suspends the calling thread from execution for the specified number of seconds.

code
Parameters:
:   - **sec** – The number of seconds to sleep.

int nxsig_nanosleep(const struct timespec *rqtp, struct timespec *rmtp) : Suspends the calling thread from execution for the specified rqtp argument. This function will return the remaining time via updating rmtp if the sleep is interrupted by a signal.

code
Parameters:
:   - **rqtp** – The amount of time to be suspended from execution.
    - **rmtp** – If the rmtp argument is non-NULL, the timespec structure referenced
      by it is updated to contain the amount of time remaining.

Returns:
:   0 (OK), or negated errno if unsuccessful.

Busy Sleep Interfaces

Spin in a loop for the requested duration and never yield the CPU. The delay accuracy depends on CONFIG_BOARD_LOOPSPERMSEC (unless the architecture/board code replaces these function(s) with an implementation that does not use the value.)

void up_mdelay(unsigned int milliseconds) : Delay inline for the requested number of milliseconds.

code
Parameters:
:   - **milliseconds** – The number of milliseconds to delay.

void up_udelay(useconds_t microseconds) : Delay inline for the requested number of microseconds.

code
Parameters:
:   - **microseconds** – The number of microseconds to delay.

void up_ndelay(unsigned long nanoseconds) : Delay inline for the requested number of nanoseconds.

code
Parameters:
:   - **nanoseconds** – The number of nanoseconds to delay.