48#ifdef LOSCFG_SECURITY_CAPABILITY
52#ifdef LOSCFG_KERNEL_VDSO
55#ifdef LOSCFG_SECURITY_VID
63#define CPUCLOCK_PERTHREAD_MASK 4
64#define CPUCLOCK_ID_OFFSET 3
73#define TIME_RETURN(err) do { \
88#define timeval64 timeval
89#define timespec64 timespec
100 if ((tv->tv_usec < 0) || (tv->tv_usec >= OS_SYS_US_PER_SECOND) || (tv->tv_sec < 0)) {
115 if ((tv->tv_usec < 0) || (tv->tv_usec >= OS_SYS_US_PER_SECOND) || (tv->tv_sec < 0)) {
125 if (swtmrID >= OS_SWTMR_MAX_TIMERID) {
142STATIC
const long long g_adjPacement = (((LOSCFG_BASE_CORE_ADJ_PER_SECOND * SCHED_CLOCK_INTETRVAL_TICKS) /
143 LOSCFG_BASE_CORE_TICK_PER_SECOND) * OS_SYS_NS_PER_US);
208int adjtime(
const struct timeval *delta,
struct timeval *oldDelta)
213 if (oldDelta != NULL) {
216 oldDelta->tv_usec = (
g_adjTimeLeft % OS_SYS_NS_PER_SECOND) / OS_SYS_NS_PER_US;
219 oldDelta->tv_usec = -((
g_adjTimeLeft % OS_SYS_NS_PER_SECOND) / OS_SYS_NS_PER_US);
223 if ((delta == NULL) || ((delta->tv_sec == 0) && (delta->tv_usec == 0))) {
228 if ((delta->tv_usec > OS_SYS_US_PER_SECOND) || (delta->tv_usec < -OS_SYS_US_PER_SECOND)) {
237 if ((delta->tv_sec < (INT_MIN / OS_SYS_US_PER_SECOND + 2)) ||
238 (delta->tv_sec > (INT_MAX / OS_SYS_US_PER_SECOND + 2))) {
243 g_adjTimeLeft = (
INT64)delta->tv_sec * OS_SYS_NS_PER_SECOND + delta->tv_usec * OS_SYS_NS_PER_US;
255STATIC INLINE
struct timespec64
OsTimeSpecAdd(const struct timespec64 t1, const struct timespec64 t2)
257 struct timespec64 ret = {0};
259 ret.tv_sec = t1.tv_sec + t2.tv_sec;
260 ret.tv_nsec = t1.tv_nsec + t2.tv_nsec;
262 if (ret.tv_nsec >= OS_SYS_NS_PER_SECOND) {
264 ret.tv_nsec -= OS_SYS_NS_PER_SECOND;
265 }
else if (ret.tv_nsec < 0L) {
267 ret.tv_nsec += OS_SYS_NS_PER_SECOND;
273STATIC INLINE
struct timespec64
OsTimeSpecSub(const struct timespec64 t1, const struct timespec64 t2)
275 struct timespec64 ret = {0};
277 ret.tv_sec = t1.tv_sec - t2.tv_sec;
278 ret.tv_nsec = t1.tv_nsec - t2.tv_nsec;
279 if (ret.tv_nsec < 0) {
281 ret.tv_nsec += OS_SYS_NS_PER_SECOND;
292 hwTime->tv_sec = nowNsec / OS_SYS_NS_PER_SECOND;
293 hwTime->tv_nsec = nowNsec - hwTime->tv_sec * OS_SYS_NS_PER_SECOND;
299 struct timespec64 setTime = {0};
300 struct timespec64 hwTime = {0};
301 struct timespec64 realTime = {0};
302 struct timespec64 tmp = {0};
304#ifdef LOSCFG_SECURITY_CAPABILITY
312 setTime.tv_sec = tv->tv_sec;
313 setTime.tv_nsec = tv->tv_usec * OS_SYS_NS_PER_US;
333 struct timeval64 stTimeVal64 = {0};
339 stTimeVal64.tv_sec = tv->tv_sec;
340 stTimeVal64.tv_usec = tv->tv_usec;
345#ifndef LOSCFG_AARCH64
358 struct timeval tv = {0};
371 struct timespec64 hwTime = {0};
372 struct timespec64 realTime = {0};
381 tv->tv_sec = realTime.tv_sec;
382 tv->tv_usec = realTime.tv_nsec / OS_SYS_NS_PER_US;
384 if (tv->tv_sec < 0) {
390#ifndef LOSCFG_AARCH64
419#ifdef LOSCFG_LIBC_NEWLIB
425 struct timeval64 stTimeVal64 = {0};
426#ifdef LOSCFG_LIBC_NEWLIB
427 struct timezone *
tz = (
struct timezone *)_tz;
439 tv->tv_sec = stTimeVal64.tv_sec;
440 tv->tv_usec = stTimeVal64.tv_usec;
442 if (stTimeVal64.tv_sec > (
long long)LONG_MAX) {
445 tv->tv_sec = (time_t)stTimeVal64.tv_sec;
446 tv->tv_usec = (suseconds_t)stTimeVal64.tv_usec;
454 struct timeval tv = {0};
460 case CLOCK_MONOTONIC_COARSE:
461 case CLOCK_REALTIME_COARSE:
462 case CLOCK_MONOTONIC_RAW:
463 case CLOCK_PROCESS_CPUTIME_ID:
465 case CLOCK_REALTIME_ALARM:
466 case CLOCK_BOOTTIME_ALARM:
468 case CLOCK_THREAD_CPUTIME_ID:
469 TIME_RETURN(ENOTSUP);
470 case CLOCK_MONOTONIC:
479#ifdef LOSCFG_SECURITY_CAPABILITY
485 tv.tv_sec = tp->tv_sec;
486 tv.tv_usec = tp->tv_nsec / OS_SYS_NS_PER_US;
490#ifdef LOSCFG_KERNEL_CPUP
494 UINT32 tid = -(clockID - 6) / 8 - 1;
501 const pid_t pid = -(clockID - 2) / 8 - 1;
511 if (OS_TID_CHECK_INVALID(tid)) {
521 SCHEDULER_LOCK(intSave);
523 SCHEDULER_UNLOCK(intSave);
525 ats->tv_sec = runtime / OS_SYS_NS_PER_SECOND;
526 ats->tv_nsec = runtime % OS_SYS_NS_PER_SECOND;
542 spcb = OS_PCB_FROM_PID(pid);
547 SCHEDULER_LOCK(intSave);
549 SCHEDULER_UNLOCK(intSave);
553 SCHEDULER_UNLOCK(intSave);
555 ats->tv_sec = runtime / OS_SYS_NS_PER_SECOND;
556 ats->tv_nsec = runtime % OS_SYS_NS_PER_SECOND;
569 if ((
UINT32)clockID & CPUCLOCK_PERTHREAD_MASK) {
583 if (!((
UINT32)clockID & CPUCLOCK_PERTHREAD_MASK)) {
588 spcb = OS_PCB_FROM_PID(pid);
617 struct timespec64 tmp = {0};
618 struct timespec64 hwTime = {0};
620 if (clockID > MAX_CLOCKS) {
631 case CLOCK_MONOTONIC_RAW:
632 tp->tv_sec = hwTime.tv_sec;
633 tp->tv_nsec = hwTime.tv_nsec;
635 case CLOCK_MONOTONIC:
639 tp->tv_sec = tmp.tv_sec;
640 tp->tv_nsec = tmp.tv_nsec;
647 tp->tv_sec = tmp.tv_sec;
648 tp->tv_nsec = tmp.tv_nsec;
650 case CLOCK_MONOTONIC_COARSE:
651 case CLOCK_REALTIME_COARSE:
652 case CLOCK_THREAD_CPUTIME_ID:
653 case CLOCK_PROCESS_CPUTIME_ID:
655 case CLOCK_REALTIME_ALARM:
656 case CLOCK_BOOTTIME_ALARM:
658 TIME_RETURN(ENOTSUP);
661#ifdef LOSCFG_KERNEL_CPUP
683 case CLOCK_MONOTONIC_RAW:
684 case CLOCK_MONOTONIC:
687 tp->tv_nsec = OS_SYS_NS_PER_US;
690 case CLOCK_MONOTONIC_COARSE:
691 case CLOCK_REALTIME_COARSE:
694 tp->tv_nsec = OS_SYS_NS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND;
697 case CLOCK_THREAD_CPUTIME_ID:
698 case CLOCK_PROCESS_CPUTIME_ID:
700 case CLOCK_REALTIME_ALARM:
701 case CLOCK_BOOTTIME_ALARM:
703 TIME_RETURN(ENOTSUP);
705#ifdef LOSCFG_KERNEL_CPUP
718int clock_nanosleep(clockid_t clk,
int flags,
const struct timespec *req,
struct timespec *rem)
727 case CLOCK_MONOTONIC_COARSE:
728 case CLOCK_REALTIME_COARSE:
729 case CLOCK_MONOTONIC_RAW:
730 case CLOCK_MONOTONIC:
731 case CLOCK_PROCESS_CPUTIME_ID:
733 case CLOCK_REALTIME_ALARM:
734 case CLOCK_BOOTTIME_ALARM:
736 if (flags == 0 || flags == TIMER_ABSTIME) {
737 TIME_RETURN(ENOTSUP);
740 case CLOCK_THREAD_CPUTIME_ID:
752 union sigval sigev_value;
757#ifdef LOSCFG_KERNEL_VM
765 OS_GOTO_EXIT_IF(arg == NULL, EINVAL);
773 info.si_code = SI_TIMER;
774 info.si_value.sival_ptr = arg->
sigev_value.sival_ptr;
779 OS_GOTO_EXIT_IF(OS_TID_CHECK_INVALID(arg->
tid), EINVAL);
782 OS_GOTO_EXIT_IF(ret != LOS_OK, -ret);
786 SCHEDULER_LOCK(intSave);
788 SCHEDULER_UNLOCK(intSave);
789 OS_GOTO_EXIT_IF(ret != LOS_OK, -ret);
792 OS_GOTO_EXIT_IF(pid <= 0 || OS_PID_CHECK_INVALID(pid), EINVAL);
794 SCHEDULER_LOCK(intSave);
795 OsDispatch(pid, &info, OS_USER_KILL_PERMISSION);
796 SCHEDULER_UNLOCK(intSave);
800 PRINT_ERR(
"Dispatch signals failed!, ret: %d\r\n", ret);
805int timer_create(clockid_t clockID,
struct sigevent *restrict evp, timer_t *restrict timerID)
809#ifdef LOSCFG_SECURITY_VID
813 if (!timerID || (clockID != CLOCK_REALTIME) || !evp) {
818 if ((evp->sigev_notify != SIGEV_THREAD) || evp->sigev_notify_attributes) {
824 &swtmrID, (
UINTPTR)evp->sigev_value.sival_ptr);
826 errno = (ret == LOS_ERRNO_SWTMR_MAXSIZE) ? EAGAIN : EINVAL;
830#ifdef LOSCFG_SECURITY_VID
832 if (vid == MAX_INVALID_TIMER_VID) {
838 *timerID = (timer_t)(
UINTPTR)swtmrID;
848#ifdef LOSCFG_SECURITY_VID
852 if ((clockID != CLOCK_REALTIME) || (timerID == NULL)) {
858 if (signo > SIGRTMAX || signo < 1) {
879 errno = (ret == LOS_ERRNO_SWTMR_MAXSIZE) ? EAGAIN : EINVAL;
884#ifdef LOSCFG_SECURITY_VID
886 if (vid == MAX_INVALID_TIMER_VID) {
893 *timerID = (timer_t)(
UINTPTR)swtmrID;
903#ifdef LOSCFG_SECURITY_VID
910 arg = (VOID *)OS_SWT_FROM_SID(swtmrID)->uwArg;
911 swtmrProc = (
UINTPTR)OS_SWT_FROM_SID(swtmrID)->pfnHandler;
919#ifdef LOSCFG_SECURITY_VID
930 const struct itimerspec *value,
931 struct itimerspec *oldValue)
935 UINT32 interval, expiry, ret;
944#ifdef LOSCFG_SECURITY_VID
947 if ((value == NULL) || OS_INT_ACTIVE || !
ValidTimerID(swtmrID)) {
961 swtmr = OS_SWT_FROM_SID(swtmrID);
963 if ((ret != LOS_OK) && (ret != LOS_ERRNO_SWTMR_NOT_STARTED)) {
973 swtmr->
uwExpiry = expiry + !!expiry;
978 if ((value->it_value.tv_sec == 0) && (value->it_value.tv_nsec == 0)) {
1002#ifdef LOSCFG_SECURITY_VID
1010 swtmr = OS_SWT_FROM_SID(swtmrID);
1014 if ((ret != LOS_OK) && (ret != LOS_ERRNO_SWTMR_NOT_STARTED)) {
1030#ifdef LOSCFG_SECURITY_VID
1038 swtmr = OS_SWT_FROM_SID(swtmrID);
1039 if (swtmr->
usTimerID >= OS_SWTMR_MAX_TIMERID) {
1045 return (overRun > DELAYTIMER_MAX) ? DELAYTIMER_MAX : overRun;
1053 if (ret == LOS_OK || ret == LOS_ERRNO_TSK_YIELD_NOT_ENOUGH_TASK) {
1059#ifdef LOSCFG_LIBC_NEWLIB
1062int usleep(
unsigned useconds)
1068int nanosleep(
const struct timespec *rqtp,
struct timespec *rmtp)
1081 nanoseconds = (
UINT64)rqtp->tv_sec * OS_SYS_NS_PER_SECOND + rqtp->tv_nsec;
1093 return (
double)(time2 - time1);
1102 clockMsec = (clock_t)(nowNsec / (OS_SYS_NS_PER_SECOND / CLOCKS_PER_SEC));
1109 clock_t clockTick = -1;
1117int setitimer(
int which,
const struct itimerval *value,
struct itimerval *ovalue)
1122 timer_t timerID = 0;
1123 struct itimerspec spec;
1124 struct itimerspec ospec;
1128 if (which != ITIMER_REAL || !value) {
1134 if (processCB->
timerID == (timer_t)(
UINTPTR)MAX_INVALID_TIMER_VID) {
1136 if (ret != LOS_OK) {
1144 SCHEDULER_LOCK(intSave);
1145 if (processCB->
timerID == (timer_t)(
UINTPTR)MAX_INVALID_TIMER_VID) {
1147 SCHEDULER_UNLOCK(intSave);
1149 SCHEDULER_UNLOCK(intSave);
1160 TIMEVAL_TO_TIMESPEC(&value->it_value, &spec.it_value);
1161 TIMEVAL_TO_TIMESPEC(&value->it_interval, &spec.it_interval);
1164 if (ret == LOS_OK && ovalue) {
1165 TIMESPEC_TO_TIMEVAL(&ovalue->it_value, &ospec.it_value);
1166 TIMESPEC_TO_TIMEVAL(&ovalue->it_interval, &ospec.it_interval);
1176 struct itimerspec spec = {};
1181 if (which != ITIMER_REAL || !value) {
1186 if (processCB->
timerID != (timer_t)(
UINTPTR)MAX_INVALID_TIMER_VID) {
1190 if (ret == LOS_OK) {
1191 TIMESPEC_TO_TIMEVAL(&value->it_value, &spec.it_value);
1192 TIMESPEC_TO_TIMEVAL(&value->it_interval, &spec.it_interval);
1198#ifdef LOSCFG_KERNEL_VDSO
1203 struct timespec64 tmp = {0};
1204 struct timespec64 hwTime = {0};
1206 if (vdsoDataPage == NULL) {
1231 if (ret == LOS_OK) {
1238 return (time_t)OS_ERROR;
BOOL IsCapPermit(UINT32 capIndex)
LITE_OS_SEC_TEXT UINT32 LOS_SwtmrStop(UINT16 swtmrID)
接口函数 停止定时器 参数定时任务ID
LITE_OS_SEC_TEXT UINT32 LOS_SwtmrStart(UINT16 swtmrID)
接口函数 启动定时器 参数定时任务ID
LITE_OS_SEC_TEXT UINT32 LOS_SwtmrTimeGet(UINT16 swtmrID, UINT32 *tick)
接口函数 获得软件定时器剩余Tick数 通过 *tick 带走
VOID(* SWTMR_PROC_FUNC)(UINTPTR arg)
Define the type of a callback function that handles software timer timeout.
LITE_OS_SEC_TEXT UINT32 LOS_SwtmrDelete(UINT16 swtmrID)
接口函数 删除定时器
LITE_OS_SEC_TEXT_INIT UINT32 LOS_SwtmrCreate(UINT32 interval, UINT8 mode, SWTMR_PROC_FUNC handler, UINT16 *swtmrID, UINTPTR arg)
创建定时器,设置定时器的定时时长、定时器模式、回调函数,并返回定时器ID
@ LOS_SWTMR_MODE_NO_SELFDELETE
LITE_OS_SEC_TEXT_MINOR UINT32 OsNS2Tick(UINT64 nanoseconds)
纳秒转化成 tick
LITE_OS_SEC_TEXT UINT32 LOS_CurTaskIDGet(VOID)
Obtain current running task ID.
LITE_OS_SEC_TEXT UINT32 LOS_TaskDelay(UINT32 tick)
任务延时等待,释放CPU,等待时间到期后该任务会重新进入ready状态
LITE_OS_SEC_TEXT_MINOR UINT64 LOS_CurrNanosec(VOID)
获取自系统启动以来的纳秒数
LITE_OS_SEC_TEXT UINT32 LOS_GetCurrProcessID(VOID)
获取当前进程的进程ID
STATIC INLINE BOOL OsProcessIsUnused(const LosProcessCB *processCB)
STATIC INLINE BOOL OsProcessIDUserCheckInvalid(UINT32 pid)
STATIC INLINE LosTaskCB * OsCurrTaskGet(VOID)
int OsDispatch(pid_t pid, siginfo_t *info, int permission)
信号分发,发送信号权限/进程组过滤.
static int GOOD_SIGNO(unsigned int sig)
信号ID是否有效
VOID LOS_SpinUnlockRestore(SPIN_LOCK_S *lock, UINT32 intSave)
VOID LOS_SpinLockSave(SPIN_LOCK_S *lock, UINT32 *intSave)
INT32 OsUserProcessOperatePermissionsCheck(const LosTaskCB *taskCB, UINT32 processID)
STATIC INLINE LosTaskCB * OsGetTaskCB(UINT32 taskID)
通过任务ID获取任务实体,task由任务池分配,本质是个数组,彼此都挨在一块
INT32 OsTcbDispatch(LosTaskCB *stcb, siginfo_t *info)
给任务(线程)发送一个信号
void * malloc(size_t size)
动态分配内存块大小
void free(void *ptr)
释放ptr所指向的内存空间
INT64 monoTimeSec
系统运行时间,从系统启动时开始计时,速度更快精度更低,系统休眠时不再计时
INT64 realTimeNsec
单位纳秒: 系统实时时间
INT64 realTimeSec
单位秒: 系统实时时间
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void
STATIC VOID OsGetHwTime(struct timespec64 *hwTime)
获取硬件时间
int timer_delete(timer_t timerID)
int setlocalseconds(int seconds)
设置本地秒数
STATIC long long g_adjTimeLeft
STATIC INT32 DoNanoSleep(UINT64 nanoseconds)
STATIC const long long g_adjPacement
STATIC struct timespec64 g_accDeltaFromAdj
int clock_nanosleep(clockid_t clk, int flags, const struct timespec *req, struct timespec *rem)
int settimeofday64(const struct timeval64 *tv, const struct timezone *tz)
int clock_settime(clockid_t clockID, const struct timespec *tp)
const pid_t GetPidFromClockID(clockid_t clockID)
static VOID SwtmrProc(UINTPTR tmrArg)
int getitimer(int which, struct itimerval *value)
STATIC INT32 g_adjDirection
STATIC INT32 OsSetTimeOfDay(const struct timeval64 *tv, const struct timezone *tz)
设置日历
int gettimeofday64(struct timeval64 *tv, struct timezone *tz)
STATIC INLINE BOOL ValidTimeval(const struct timeval *tv)
int OsTimerCreate(clockid_t clockID, struct ksigevent *evp, timer_t *timerID)
int timer_settime(timer_t timerID, int flags, const struct itimerspec *value, struct itimerspec *oldValue)
UINT32 GetTidFromClockID(clockid_t clockID)
double difftime(time_t time2, time_t time1)
int clock_getres(clockid_t clockID, struct timespec *tp)
unsigned int sleep(unsigned int seconds)
int settimeofday(const struct timeval *tv, const struct timezone *tz)
STATIC INT32 OsGetTimeOfDay(struct timeval64 *tv, struct timezone *tz)
获取日历时间
static int PthreadGetCputime(clockid_t clockID, struct timespec *ats)
clock_t times(struct tms *buf)
int gettimeofday(struct timeval *tv, void *_tz) int gettimeofday(struct timeval *tv
gettimeofday
static int ProcessGetCputime(clockid_t clockID, struct timespec *ats)
int timer_gettime(timer_t timerID, struct itimerspec *value)
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
VOID OsVdsoTimeGet(VdsoDataPage *vdsoDataPage)
将最新的时间刷进数据页
static int CheckClock(const clockid_t clockID)
STATIC INLINE BOOL ValidTimerID(UINT16 swtmrID)
static int GetCputime(clockid_t clockID, struct timespec *tp)
int timer_create(clockid_t clockID, struct sigevent *restrict evp, timer_t *restrict timerID)
int usleep(unsigned long useconds) int usleep(unsigned useconds)
int adjtime(const struct timeval *delta, struct timeval *oldDelta)
STATIC INLINE BOOL ValidTimeval64(const struct timeval64 *tv)
int timer_getoverrun(timer_t timerID)
int clock_gettime(clockid_t clockID, struct timespec *tp)
当用户程序进行特定系统调用时(例如clock_gettime(CLOCK_REALTIME_COARSE, &ts)),VDSO代码页会将其拦截;
STATIC INLINE struct timespec64 OsTimeSpecAdd(const struct timespec64 t1, const struct timespec64 t2)
增加指定时间
static int CpuClockGetres(const clockid_t clockID, struct timespec *tp)
STATIC struct timespec64 g_accDeltaFromSet
STATIC INLINE struct timespec64 OsTimeSpecSub(const struct timespec64 t1, const struct timespec64 t2)
减少指定时间
STATIC SPIN_LOCK_INIT(g_timeSpin)
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
STATIC INLINE BOOL ValidTimeSpec(const struct timespec *tp)
STATIC INLINE VOID OsTick2TimeSpec(struct timespec *tp, UINT32 tick)
STATIC INLINE UINT32 OsTimeSpec2Tick(const struct timespec *tp)
UINT16 GetRidByVid(UINT16 vid)
void RemoveNodeByVid(UINT16 vid)
UINT16 AddNodeByRid(UINT16 rid)