Category Archives: timing

sleep & delay function (2)

To let the thread to suspend the exact amount of time, without being affected by thread scheduling, we can use nanospin().

int nanospin( const struct timespec *when );

 

The nanospin() function occupies the CPU for the amount of time specified by the argument when without blocking the calling thread. (The thread isn’t taken off the ready list.) The function is essentially a do…while loop.

The first time you call nanospin(), the C library invokes nanospin_calibrate() with an argument of 0 (interrupts enabled), if you haven’t already called it.

int nanospin_ns( unsigned long nsec );

The nanospin_ns() function busy-waits for the number of nanoseconds specified in nsec, without blocking the calling thread.

void nanospin_count( unsigned long count );

The nanospin_count() function busy-waits for the number of iterations specified in count. Use nanospin_ns_to_count() to turn a number of nanoseconds into an iteration count suitable for nanospin_count().

sleep & delay functions (1)

Quoted from QNX document.

— delay(unsigned int duration) suspends the calling thread for duration milliseconds.

— sleep(unsigned int seconds) function suspends the calling thread until the number of realtime seconds specified by the seconds argument have elapsed, or the thread receives a signal whose action is either to terminate the process or to call a signal handler.

both delay() and sleep() returns either 0 or the number of unslept time if interrupt by a signal.

— usleep(useconds_t useconds) function suspends the calling thread until useconds microseconds of realtime have elapsed, or until a signal that isn’t ignored is received.

— nanosleep( const struct timespec* rqtp, struct timespec* rmtp )  function causes the calling thread to be suspended from execution until either:

  • The time interval specified by the rqtp argument has elapsed

    Or

  • A signal is delivered to the thread, and the signal’s action is to invoke a signal-catching function or terminate the process.

usleep() and nanosleep()  returns either 0 (success) or -1 (an error occured)

 

Note:

With all the functions above, the suspension time may be greater than the requested amount, due to the nature of time measurement (see the Tick, Tock: Understanding the Neutrino Microkernel’s Concept of Time chapter of the QNX Neutrino Programmer’s Guide), or due to the scheduling of other, higher priority threads by the system.

Sleep functions

These functions are used to suspend a calling thread for a given length of time, until a signal is received (question: does delay() cares about signals?)
include
sleep(unsigned int seconds);
usleep(useconds_t useconds);
delay(unsigned int mseconds);
Include
nanosleep(const struct timespec* rqtp, struct timespec* rmtp); // rqtp: requested time; rmtp: remaining time, can be NULL.
Note: the suspension time may be greater than the requested amount, due to the nature of time measurement, or the thread scheduling. Below is an example of usleep(500): 500us sleep turns to be 1ms 920us, or 1ms 853us.

if(delay_needed) {

int sleep_us = 500;

trace_logf( _NTO_TRACE_USERFIRST + 801, “delay %d us”, sleep_us);

usleep(sleep_us);

delay_needed = 0;

}

trace_logf( _NTO_TRACE_USERFIRST + 800, “set up a new transfer”);

Screen shot 2015-05-07 at 4.54.22 PM Screen shot 2015-05-07 at 4.52.31 PM