SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
time.h File Reference

POSIX <sys/time.h> extension for STK: gettimeofday(). Implemented by stk_c_pthread.cpp, together with time() of <time.h>. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define _SYS__PTHREADTYPES_H_
#define STK_C_PTHREADTYPES_GUARD

Functions

int gettimeofday (struct timeval *tv, void *tz)
 Get the wall time (microsecond fields, tick resolution).
int settimeofday (const struct timeval *tv, const struct timezone *tz)
 Set the wall time (microsecond precision): the offset of time() / gettimeofday() is changed, the kernel timeline of clock_gettime() and of all deadlines is not (see the file description).

Detailed Description

POSIX <sys/time.h> extension for STK: gettimeofday(). Implemented by stk_c_pthread.cpp, together with time() of <time.h>.

This header EXTENDS the toolchain's <sys/time.h> (included first via #include_next on GCC/Clang, or the header named by STK_C_SYSTEM_SYS_TIME_H on other compilers).

Wall time: STK has no real-time clock. gettimeofday() and time() report the kernel time since start plus an offset which is 0 until the application sets the current Unix time with stk_posix_set_wall_time() (declared in time.h; e.g. after reading an RTC or a network time). The offset applies ONLY to gettimeofday() and time(): clock_gettime(), all absolute deadlines (pthread_cond_timedwait(), sem_timedwait(), mq_timedreceive(), ...) and TIMER_ABSTIME stay on the kernel timeline, so do not derive such a deadline from gettimeofday()/time().

These functions replace the ones of the C library (newlib's gettimeofday()/time() end in a system call stub), the object file of this shim must be linked before libc.

Definition in file sys/time.h.

Macro Definition Documentation

◆ _SYS__PTHREADTYPES_H_

#define _SYS__PTHREADTYPES_H_

Definition at line 32 of file sys/time.h.

◆ STK_C_PTHREADTYPES_GUARD

#define STK_C_PTHREADTYPES_GUARD

Definition at line 33 of file sys/time.h.

Function Documentation

◆ gettimeofday()

int gettimeofday ( struct timeval * tv,
void * tz )

Get the wall time (microsecond fields, tick resolution).

Parameters
[out]tvReceives the time since the Unix epoch (see the file description for what that means on STK).
[in]tzObsolete, ignored.
Returns
0, or -1 with errno = EINVAL if tv is NULL.
Warning
ISR-unsafe.

Definition at line 3384 of file stk_c_pthread.cpp.

3385{
3386 int result = -1;
3387
3388 STK_UNUSED(tz);
3389
3390 if (tv == nullptr)
3391 {
3392 errno = EINVAL;
3393 }
3394 else
3395 {
3396 const int64_t us = WallTimeUs();
3397
3398 tv->tv_sec = static_cast<time_t>(us / 1000000LL);
3399 tv->tv_usec = static_cast<suseconds_t>(us % 1000000LL);
3400 result = 0;
3401 }
3402
3403 return result;
3404}
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:715
static int64_t WallTimeUs()

References STK_UNUSED, and WallTimeUs().

Here is the call graph for this function:

◆ settimeofday()

int settimeofday ( const struct timeval * tv,
const struct timezone * tz )

Set the wall time (microsecond precision): the offset of time() / gettimeofday() is changed, the kernel timeline of clock_gettime() and of all deadlines is not (see the file description).

Parameters
[in]tvNew time since the Unix epoch.
[in]tzObsolete, ignored.
Returns
0, or -1 with errno = EINVAL for a NULL tv or a tv_usec outside 0..999999.
Warning
ISR-unsafe.

Definition at line 3365 of file stk_c_pthread.cpp.

3366{
3367 int result = -1;
3368
3369 STK_UNUSED(tz);
3370
3371 if ((tv == nullptr) || (tv->tv_usec < 0) || (tv->tv_usec >= 1000000))
3372 {
3373 errno = EINVAL;
3374 }
3375 else
3376 {
3377 SetWallTimeUs((static_cast<int64_t>(tv->tv_sec) * 1000000LL) + static_cast<int64_t>(tv->tv_usec));
3378 result = 0;
3379 }
3380
3381 return result;
3382}
static void SetWallTimeUs(int64_t unix_us)

References SetWallTimeUs(), and STK_UNUSED.

Here is the call graph for this function: