更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
time.c
浏览该文件的文档.
1/*!
2 * @file time.c
3 * @brief
4 * @link
5 @verbatim
6 @endverbatim
7 * @version
8 * @author weharmonyos.com | 鸿蒙研究站 | 每天死磕一点点
9 * @date 2021-11-25
10 */
11/*
12 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
13 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without modification,
16 * are permitted provided that the following conditions are met:
17 *
18 * 1. Redistributions of source code must retain the above copyright notice, this list of
19 * conditions and the following disclaimer.
20 *
21 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
22 * of conditions and the following disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
26 * to endorse or promote products derived from this software without specific prior written
27 * permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
31 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
33 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
36 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
37 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
38 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
39 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42#include "time.h"
43#include "stdint.h"
44#include "stdio.h"
45#include "sys/times.h"
46#include "time_posix.h"
47#include "unistd.h"
48#ifdef LOSCFG_SECURITY_CAPABILITY
49#include "capability_api.h"
50#endif
51#include "los_signal.h"
52#ifdef LOSCFG_KERNEL_VDSO
53#include "los_vdso.h"
54#endif
55#ifdef LOSCFG_SECURITY_VID
56#include "vid_api.h"
57#endif
58#include "user_copy.h"
59#include "los_process_pri.h"
60#include "los_swtmr_pri.h"
61#include "los_sys_pri.h"
62
63#define CPUCLOCK_PERTHREAD_MASK 4
64#define CPUCLOCK_ID_OFFSET 3
65
66/*
67 * Do a time package defined return. This requires the error code
68 * to be placed in errno, and if it is non-zero, -1 returned as the
69 * result of the function. This also gives us a place to put any
70 * generic tidyup handling needed for things like signal delivery and
71 * cancellation.
72 */
73#define TIME_RETURN(err) do { \
74 INT32 retVal = 0; \
75 if ((err) != 0) { \
76 retVal = -1; \
77 errno = (err); \
78 } \
79 return retVal; \
80} while (0)
81
82#ifdef LOSCFG_AARCH64
83/*
84 * This two structures originally did't exit,
85 * they added by liteos to support 64bit interfaces on 32bit platform,
86 * in 64bit platform, timeval64 define to timeval which is platform adaptive.
87 */
88#define timeval64 timeval
89#define timespec64 timespec
90#endif
91
92STATIC INLINE BOOL ValidTimeval(const struct timeval *tv)
93{
94 /* Fail a NULL pointer */
95 if (tv == NULL) {
96 return FALSE;
97 }
98
99 /* Fail illegal microseconds values */
100 if ((tv->tv_usec < 0) || (tv->tv_usec >= OS_SYS_US_PER_SECOND) || (tv->tv_sec < 0)) {
101 return FALSE;
102 }
103
104 return TRUE;
105}
106
107STATIC INLINE BOOL ValidTimeval64(const struct timeval64 *tv)
108{
109 /* Fail a NULL pointer */
110 if (tv == NULL) {
111 return FALSE;
112 }
113
114 /* Fail illegal microseconds values */
115 if ((tv->tv_usec < 0) || (tv->tv_usec >= OS_SYS_US_PER_SECOND) || (tv->tv_sec < 0)) {
116 return FALSE;
117 }
118
119 return TRUE;
120}
121
122STATIC INLINE BOOL ValidTimerID(UINT16 swtmrID)
123{
124 /* check timer id */
125 if (swtmrID >= OS_SWTMR_MAX_TIMERID) {
126 return FALSE;
127 }
128
129 /* check owner of this timer */
130 if (OS_SWT_FROM_SID(swtmrID)->uwOwnerPid != LOS_GetCurrProcessID()) {
131 return FALSE;
132 }
133
134 return TRUE;
135}
136
137STATIC SPIN_LOCK_INIT(g_timeSpin);
138STATIC long long g_adjTimeLeft; /* absolute value of adjtime */
139STATIC INT32 g_adjDirection; /* 1, speed up; 0, slow down; | 调整方向(加速,减速)*/
140
141/* Adjust pacement, nanoseconds per SCHED_CLOCK_INTETRVAL_TICKS ticks */
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);
144
145/* accumulative time delta from continuous modify, such as adjtime | 运行时间,来自连续修改的累积时间增量,例如 adjtime */
146STATIC struct timespec64 g_accDeltaFromAdj;
147/* accumulative time delta from discontinuous modify, such as settimeofday | 实时时间,来自不连续修改的累积时间增量,例如 日历时间 settimeofday*/
148STATIC struct timespec64 g_accDeltaFromSet;
149
150VOID OsAdjTime(VOID)
151{
152 UINT32 intSave;
153
154 LOS_SpinLockSave(&g_timeSpin, &intSave);
155 if (!g_adjTimeLeft) {
156 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
157 return;
158 }
159
161 if (g_adjDirection) {
162 if ((g_accDeltaFromAdj.tv_nsec + g_adjPacement) >= OS_SYS_NS_PER_SECOND) {
163 g_accDeltaFromAdj.tv_sec++;
164 g_accDeltaFromAdj.tv_nsec = (g_accDeltaFromAdj.tv_nsec + g_adjPacement) % OS_SYS_NS_PER_SECOND;
165 } else {
167 }
168 } else {
169 if ((g_accDeltaFromAdj.tv_nsec - g_adjPacement) < 0) {
170 g_accDeltaFromAdj.tv_sec--;
171 g_accDeltaFromAdj.tv_nsec = g_accDeltaFromAdj.tv_nsec - g_adjPacement + OS_SYS_NS_PER_SECOND;
172 } else {
174 }
175 }
176
178 } else {
179 if (g_adjDirection) {
180 if ((g_accDeltaFromAdj.tv_nsec + g_adjTimeLeft) >= OS_SYS_NS_PER_SECOND) {
181 g_accDeltaFromAdj.tv_sec++;
182 g_accDeltaFromAdj.tv_nsec = (g_accDeltaFromAdj.tv_nsec + g_adjTimeLeft) % OS_SYS_NS_PER_SECOND;
183 } else {
185 }
186 } else {
187 if ((g_accDeltaFromAdj.tv_nsec - g_adjTimeLeft) < 0) {
188 g_accDeltaFromAdj.tv_sec--;
189 g_accDeltaFromAdj.tv_nsec = g_accDeltaFromAdj.tv_nsec - g_adjTimeLeft + OS_SYS_NS_PER_SECOND;
190 } else {
192 }
193 }
194
195 g_adjTimeLeft = 0;
196 }
197 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
198 return;
199}
200
201/*
202 * Function: adjtime
203 * Description: correct the time to synchronize the system clock.
204 * Input: delta - The amount of time by which the clock is to be adjusted.
205 * Output: oldDelta - the amount of time remaining from any previous adjustment that has not yet been completed.
206 * Return: On success, returns 0. On failure, -1 is returned, and errno is set to indicate the error.
207 */
208int adjtime(const struct timeval *delta, struct timeval *oldDelta)
209{
210 UINT32 intSave;
211 LOS_SpinLockSave(&g_timeSpin, &intSave);
212 /* return the amount of time remaining from any previous adjustment that has not yet been completed. */
213 if (oldDelta != NULL) {
214 if (g_adjDirection == 1) {
215 oldDelta->tv_sec = g_adjTimeLeft / OS_SYS_NS_PER_SECOND;
216 oldDelta->tv_usec = (g_adjTimeLeft % OS_SYS_NS_PER_SECOND) / OS_SYS_NS_PER_US;
217 } else {
218 oldDelta->tv_sec = -(g_adjTimeLeft / OS_SYS_NS_PER_SECOND);
219 oldDelta->tv_usec = -((g_adjTimeLeft % OS_SYS_NS_PER_SECOND) / OS_SYS_NS_PER_US);
220 }
221 }
222
223 if ((delta == NULL) || ((delta->tv_sec == 0) && (delta->tv_usec == 0))) {
224 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
225 return 0;
226 }
227
228 if ((delta->tv_usec > OS_SYS_US_PER_SECOND) || (delta->tv_usec < -OS_SYS_US_PER_SECOND)) {
229 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
230 TIME_RETURN(EINVAL);
231 }
232
233 /*
234 * 2: in the glibc implementation, delta must be less than or equal to (INT_MAX / 1000000 - 2) and
235 * greater than or equal to (INT_MIN / 1000000 + 2)
236 */
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))) {
239 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
240 TIME_RETURN(EINVAL);
241 }
242
243 g_adjTimeLeft = (INT64)delta->tv_sec * OS_SYS_NS_PER_SECOND + delta->tv_usec * OS_SYS_NS_PER_US;
244 if (g_adjTimeLeft > 0) {
245 g_adjDirection = 1;
246 } else {
247 g_adjDirection = 0;
249 }
250
251 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
252 return 0;
253}
254/// 增加指定时间
255STATIC INLINE struct timespec64 OsTimeSpecAdd(const struct timespec64 t1, const struct timespec64 t2)
256{
257 struct timespec64 ret = {0};
258
259 ret.tv_sec = t1.tv_sec + t2.tv_sec;
260 ret.tv_nsec = t1.tv_nsec + t2.tv_nsec;
261 //用于校正精度
262 if (ret.tv_nsec >= OS_SYS_NS_PER_SECOND) {//超过1秒时的计算
263 ret.tv_sec += 1; //秒数加一
264 ret.tv_nsec -= OS_SYS_NS_PER_SECOND;//剩余纳秒数
265 } else if (ret.tv_nsec < 0L) {//介于 0 - 1秒之间的纳秒数
266 ret.tv_sec -= 1; //秒数减一
267 ret.tv_nsec += OS_SYS_NS_PER_SECOND;//纳秒数增加
268 }
269
270 return ret;
271}
272/// 减少指定时间
273STATIC INLINE struct timespec64 OsTimeSpecSub(const struct timespec64 t1, const struct timespec64 t2)
274{
275 struct timespec64 ret = {0};
276
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) {
280 ret.tv_sec -= 1;
281 ret.tv_nsec += OS_SYS_NS_PER_SECOND;
282 }
283
284 return ret;
285}
286/// 获取硬件时间
287STATIC VOID OsGetHwTime(struct timespec64 *hwTime)
288{
289 UINT64 nowNsec;
290
291 nowNsec = LOS_CurrNanosec();//获取当前纳秒
292 hwTime->tv_sec = nowNsec / OS_SYS_NS_PER_SECOND;//当前秒数
293 hwTime->tv_nsec = nowNsec - hwTime->tv_sec * OS_SYS_NS_PER_SECOND;// @note_thinking 为啥要这样做? 不应该是直接 nowNsec ?
294}
295/// 设置日历
296STATIC INT32 OsSetTimeOfDay(const struct timeval64 *tv, const struct timezone *tz)
297{
298 UINT32 intSave;
299 struct timespec64 setTime = {0};
300 struct timespec64 hwTime = {0};
301 struct timespec64 realTime = {0};
302 struct timespec64 tmp = {0};
303
304#ifdef LOSCFG_SECURITY_CAPABILITY
305 if (!IsCapPermit(CAP_SET_TIMEOFDAY)) {
306 TIME_RETURN(EPERM);
307 }
308#endif
309
310 (VOID)tz;
311 OsGetHwTime(&hwTime);
312 setTime.tv_sec = tv->tv_sec;
313 setTime.tv_nsec = tv->tv_usec * OS_SYS_NS_PER_US;
314
315 LOS_SpinLockSave(&g_timeSpin, &intSave);
316 /* stop on-going continuous adjusement */
317 if (g_adjTimeLeft) {
318 g_adjTimeLeft = 0;
319 }
320 realTime = OsTimeSpecAdd(hwTime, g_accDeltaFromAdj);
321 realTime = OsTimeSpecAdd(realTime, g_accDeltaFromSet);
322
323 tmp = OsTimeSpecSub(setTime, realTime);
325
326 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
327
328 return 0;
329}
330
331int settimeofday(const struct timeval *tv, const struct timezone *tz)
332{
333 struct timeval64 stTimeVal64 = {0};
334
335 if (!ValidTimeval(tv)) {
336 TIME_RETURN(EINVAL);
337 }
338
339 stTimeVal64.tv_sec = tv->tv_sec;
340 stTimeVal64.tv_usec = tv->tv_usec;
341
342 return OsSetTimeOfDay(&stTimeVal64, tz);
343}
344
345#ifndef LOSCFG_AARCH64
346int settimeofday64(const struct timeval64 *tv, const struct timezone *tz)
347{
348 if (!ValidTimeval64(tv)) {
349 TIME_RETURN(EINVAL);
350 }
351
352 return OsSetTimeOfDay(tv, tz);
353}
354#endif
355/// 设置本地秒数
356int setlocalseconds(int seconds)
357{
358 struct timeval tv = {0};
359
360 tv.tv_sec = seconds;
361 tv.tv_usec = 0;
362
363 return settimeofday(&tv, NULL);
364}
365/// 获取日历时间
366STATIC INT32 OsGetTimeOfDay(struct timeval64 *tv, struct timezone *tz)
367{
368 UINT32 intSave;
369
370 (VOID)tz;
371 struct timespec64 hwTime = {0};
372 struct timespec64 realTime = {0};
373
374 OsGetHwTime(&hwTime);
375
376 LOS_SpinLockSave(&g_timeSpin, &intSave);
377 realTime = OsTimeSpecAdd(hwTime, g_accDeltaFromAdj);
378 realTime = OsTimeSpecAdd(realTime, g_accDeltaFromSet);
379 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
380
381 tv->tv_sec = realTime.tv_sec;
382 tv->tv_usec = realTime.tv_nsec / OS_SYS_NS_PER_US;
383
384 if (tv->tv_sec < 0) {
385 TIME_RETURN(EINVAL);
386 }
387 return 0;
388}
389
390#ifndef LOSCFG_AARCH64
391int gettimeofday64(struct timeval64 *tv, struct timezone *tz)
392{
393 if (tv == NULL) {
394 TIME_RETURN(EINVAL);
395 }
396
397 return OsGetTimeOfDay(tv, tz);
398}
399#endif
400/*!
401 * @brief gettimeofday
402 @verbatim
403 gettimeofday 函数是一个符合 POSIX 标准的函数,它可以检索当前时间,精度达到纳秒级
404 struct timeval{
405 long int tv_sec; // 秒数
406 long int tv_usec; // 微秒数
407 }
408 其中time_t和suseconds_t都是long int类型。在32位下为4个字节,能够表示的最大正整数是2147483647,
409 而这个表示的时间最大能到2038-01-19 03:14:07,超过了之后就变为-2147483648,这就是linux2038年的问题。
410 而64位系统下的time_t类型即long类型长度为8个字节,可以用到几千亿年,这么长的时间完全不用担心溢出的问题。
411 @endverbatim
412 * @param tv
413 * @param tz
414 * @return
415 *
416 * @see
417 */
418
419#ifdef LOSCFG_LIBC_NEWLIB
420int gettimeofday(struct timeval *tv, void *_tz)
421#else
422int gettimeofday(struct timeval *tv, struct timezone *tz)
423#endif
424{
425 struct timeval64 stTimeVal64 = {0};
426#ifdef LOSCFG_LIBC_NEWLIB
427 struct timezone *tz = (struct timezone *)_tz;
428#endif
429
430 if (tv == NULL) {
431 TIME_RETURN(EINVAL);
432 }
433
434 if (OsGetTimeOfDay(&stTimeVal64, tz) == -1) {
435 return -1;
436 }
437
438#ifdef LOSCFG_AARCH64
439 tv->tv_sec = stTimeVal64.tv_sec;
440 tv->tv_usec = stTimeVal64.tv_usec;
441#else
442 if (stTimeVal64.tv_sec > (long long)LONG_MAX) {
443 return -1;
444 }
445 tv->tv_sec = (time_t)stTimeVal64.tv_sec;
446 tv->tv_usec = (suseconds_t)stTimeVal64.tv_usec;
447#endif
448
449 return 0;
450}
451
452int clock_settime(clockid_t clockID, const struct timespec *tp)
453{
454 struct timeval tv = {0};
455
456 switch (clockID) {
457 case CLOCK_REALTIME:
458 /* we only support the realtime clock currently */
459 break;
460 case CLOCK_MONOTONIC_COARSE:
461 case CLOCK_REALTIME_COARSE:
462 case CLOCK_MONOTONIC_RAW:
463 case CLOCK_PROCESS_CPUTIME_ID:
464 case CLOCK_BOOTTIME:
465 case CLOCK_REALTIME_ALARM:
466 case CLOCK_BOOTTIME_ALARM:
467 case CLOCK_TAI:
468 case CLOCK_THREAD_CPUTIME_ID:
469 TIME_RETURN(ENOTSUP);
470 case CLOCK_MONOTONIC:
471 default:
472 TIME_RETURN(EINVAL);
473 }
474
475 if (!ValidTimeSpec(tp)) {
476 TIME_RETURN(EINVAL);
477 }
478
479#ifdef LOSCFG_SECURITY_CAPABILITY
480 if (!IsCapPermit(CAP_CLOCK_SETTIME)) {
481 TIME_RETURN(EPERM);
482 }
483#endif
484
485 tv.tv_sec = tp->tv_sec;
486 tv.tv_usec = tp->tv_nsec / OS_SYS_NS_PER_US;
487 return settimeofday(&tv, NULL);
488}
489
490#ifdef LOSCFG_KERNEL_CPUP
491inline UINT32 GetTidFromClockID(clockid_t clockID)
492{
493 // In musl/src/thread/pthread_getcpuclockid.c, we know 'clockid = (-tid - 1) * 8 + 6'
494 UINT32 tid = -(clockID - 6) / 8 - 1; // 6 8 1 inverse operation from clockID to tid
495 return tid;
496}
497
498inline const pid_t GetPidFromClockID(clockid_t clockID)
499{
500 // In musl/src/time/clock_getcpuclockid.c, we know 'clockid = (-pid - 1) * 8 + 2'
501 const pid_t pid = -(clockID - 2) / 8 - 1; // 2 8 1 inverse operation from clockID to pid
502 return pid;
503}
504
505static int PthreadGetCputime(clockid_t clockID, struct timespec *ats)
506{
507 uint64_t runtime;
508 UINT32 intSave;
509 UINT32 tid = GetTidFromClockID(clockID);
510
511 if (OS_TID_CHECK_INVALID(tid)) {
512 return -EINVAL;
513 }
514
515 LosTaskCB *task = OsGetTaskCB(tid);
516
517 if (OsCurrTaskGet()->processID != task->processID) {
518 return -EINVAL;
519 }
520
521 SCHEDULER_LOCK(intSave);
522 runtime = task->taskCpup.allTime;
523 SCHEDULER_UNLOCK(intSave);
524
525 ats->tv_sec = runtime / OS_SYS_NS_PER_SECOND;
526 ats->tv_nsec = runtime % OS_SYS_NS_PER_SECOND;
527
528 return 0;
529}
530
531static int ProcessGetCputime(clockid_t clockID, struct timespec *ats)
532{
533 UINT64 runtime;
534 UINT32 intSave;
535 const pid_t pid = GetPidFromClockID(clockID);
536 LosProcessCB *spcb = NULL;
537
538 if (OsProcessIDUserCheckInvalid(pid) || pid < 0) {
539 return -EINVAL;
540 }
541
542 spcb = OS_PCB_FROM_PID(pid);
543 if (OsProcessIsUnused(spcb)) {
544 return -EINVAL;
545 }
546
547 SCHEDULER_LOCK(intSave);
548 if (spcb->processCpup == NULL) {
549 SCHEDULER_UNLOCK(intSave);
550 return -EINVAL;
551 }
552 runtime = spcb->processCpup->allTime;
553 SCHEDULER_UNLOCK(intSave);
554
555 ats->tv_sec = runtime / OS_SYS_NS_PER_SECOND;
556 ats->tv_nsec = runtime % OS_SYS_NS_PER_SECOND;
557
558 return 0;
559}
560
561static int GetCputime(clockid_t clockID, struct timespec *tp)
562{
563 int ret;
564
565 if (clockID >= 0) {
566 return -EINVAL;
567 }
568
569 if ((UINT32)clockID & CPUCLOCK_PERTHREAD_MASK) {
570 ret = PthreadGetCputime(clockID, tp);
571 } else {
572 ret = ProcessGetCputime(clockID, tp);
573 }
574
575 return ret;
576}
577
578static int CheckClock(const clockid_t clockID)
579{
580 int error = 0;
581 const pid_t pid = GetPidFromClockID(clockID);
582
583 if (!((UINT32)clockID & CPUCLOCK_PERTHREAD_MASK)) {
584 LosProcessCB *spcb = NULL;
585 if (OsProcessIDUserCheckInvalid(pid) || pid < 0) {
586 return -EINVAL;
587 }
588 spcb = OS_PCB_FROM_PID(pid);
589 if (OsProcessIsUnused(spcb)) {
590 error = -EINVAL;
591 }
592 } else {
593 error = -EINVAL;
594 }
595
596 return error;
597}
598
599static int CpuClockGetres(const clockid_t clockID, struct timespec *tp)
600{
601 if (clockID > 0) {
602 return -EINVAL;
603 }
604
605 int error = CheckClock(clockID);
606 if (!error) {
607 error = ProcessGetCputime(clockID, tp);
608 }
609
610 return error;
611}
612#endif
613/// 当用户程序进行特定系统调用时(例如clock_gettime(CLOCK_REALTIME_COARSE, &ts)),VDSO代码页会将其拦截;
614int clock_gettime(clockid_t clockID, struct timespec *tp)
615{
616 UINT32 intSave;
617 struct timespec64 tmp = {0};
618 struct timespec64 hwTime = {0};
619
620 if (clockID > MAX_CLOCKS) {
621 goto ERROUT;
622 }
623
624 if (tp == NULL) {
625 goto ERROUT;
626 }
627
628 OsGetHwTime(&hwTime);
629
630 switch (clockID) {
631 case CLOCK_MONOTONIC_RAW:
632 tp->tv_sec = hwTime.tv_sec;
633 tp->tv_nsec = hwTime.tv_nsec;
634 break;
635 case CLOCK_MONOTONIC:
636 LOS_SpinLockSave(&g_timeSpin, &intSave);
637 tmp = OsTimeSpecAdd(hwTime, g_accDeltaFromAdj);
638 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
639 tp->tv_sec = tmp.tv_sec;
640 tp->tv_nsec = tmp.tv_nsec;
641 break;
642 case CLOCK_REALTIME:
643 LOS_SpinLockSave(&g_timeSpin, &intSave);
644 tmp = OsTimeSpecAdd(hwTime, g_accDeltaFromAdj);
646 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
647 tp->tv_sec = tmp.tv_sec;
648 tp->tv_nsec = tmp.tv_nsec;
649 break;
650 case CLOCK_MONOTONIC_COARSE:
651 case CLOCK_REALTIME_COARSE:
652 case CLOCK_THREAD_CPUTIME_ID:
653 case CLOCK_PROCESS_CPUTIME_ID:
654 case CLOCK_BOOTTIME:
655 case CLOCK_REALTIME_ALARM:
656 case CLOCK_BOOTTIME_ALARM:
657 case CLOCK_TAI:
658 TIME_RETURN(ENOTSUP);
659 default:
660 {
661#ifdef LOSCFG_KERNEL_CPUP
662 int ret = GetCputime(clockID, tp);
663 TIME_RETURN(-ret);
664#else
665 TIME_RETURN(EINVAL);
666#endif
667 }
668 }
669
670 return 0;
671
672 ERROUT:
673 TIME_RETURN(EINVAL);
674}
675
676int clock_getres(clockid_t clockID, struct timespec *tp)
677{
678 if (tp == NULL) {
679 TIME_RETURN(EINVAL);
680 }
681
682 switch (clockID) {
683 case CLOCK_MONOTONIC_RAW:
684 case CLOCK_MONOTONIC:
685 case CLOCK_REALTIME:
686 /* the accessable rtc resolution */
687 tp->tv_nsec = OS_SYS_NS_PER_US; /* the precision of clock_gettime is 1us */
688 tp->tv_sec = 0;
689 break;
690 case CLOCK_MONOTONIC_COARSE:
691 case CLOCK_REALTIME_COARSE:
692 /* the clock coarse resolution, supported by vdso.
693 * the precision of clock_gettime is 1tick */
694 tp->tv_nsec = OS_SYS_NS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND;
695 tp->tv_sec = 0;
696 break;
697 case CLOCK_THREAD_CPUTIME_ID:
698 case CLOCK_PROCESS_CPUTIME_ID:
699 case CLOCK_BOOTTIME:
700 case CLOCK_REALTIME_ALARM:
701 case CLOCK_BOOTTIME_ALARM:
702 case CLOCK_TAI:
703 TIME_RETURN(ENOTSUP);
704 default:
705#ifdef LOSCFG_KERNEL_CPUP
706 {
707 int ret = CpuClockGetres(clockID, tp);
708 TIME_RETURN(-ret);
709 }
710#else
711 TIME_RETURN(EINVAL);
712#endif
713 }
714
715 TIME_RETURN(0);
716}
717
718int clock_nanosleep(clockid_t clk, int flags, const struct timespec *req, struct timespec *rem)
719{
720 switch (clk) {
721 case CLOCK_REALTIME:
722 if (flags == 0) {
723 /* we only support the realtime clock currently */
724 return nanosleep(req, rem);
725 }
726 /* fallthrough */
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:
732 case CLOCK_BOOTTIME:
733 case CLOCK_REALTIME_ALARM:
734 case CLOCK_BOOTTIME_ALARM:
735 case CLOCK_TAI:
736 if (flags == 0 || flags == TIMER_ABSTIME) {
737 TIME_RETURN(ENOTSUP);
738 }
739 /* fallthrough */
740 case CLOCK_THREAD_CPUTIME_ID:
741 default:
742 TIME_RETURN(EINVAL);
743 }
744
745 TIME_RETURN(0);
746}
747
748typedef struct {
750 pid_t pid;
751 unsigned int tid;
752 union sigval sigev_value;
754
755static VOID SwtmrProc(UINTPTR tmrArg)
756{
757#ifdef LOSCFG_KERNEL_VM
758 INT32 sig, ret;
759 UINT32 intSave;
760 pid_t pid;
761 siginfo_t info;
762 LosTaskCB *stcb = NULL;
763
764 swtmr_proc_arg *arg = (swtmr_proc_arg *)tmrArg;
765 OS_GOTO_EXIT_IF(arg == NULL, EINVAL);
766
767 sig = arg->sigev_signo;
768 pid = arg->pid;
769 OS_GOTO_EXIT_IF(!GOOD_SIGNO(sig), EINVAL);
770
771 /* Create the siginfo structure */
772 info.si_signo = sig;
773 info.si_code = SI_TIMER;
774 info.si_value.sival_ptr = arg->sigev_value.sival_ptr;
775
776 /* Send signals to threads or processes */
777 if (arg->tid > 0) {
778 /* Make sure that the para is valid */
779 OS_GOTO_EXIT_IF(OS_TID_CHECK_INVALID(arg->tid), EINVAL);
780 stcb = OsGetTaskCB(arg->tid);
782 OS_GOTO_EXIT_IF(ret != LOS_OK, -ret);
783
784 /* Dispatch the signal to thread, bypassing normal task group thread
785 * dispatch rules. */
786 SCHEDULER_LOCK(intSave);
787 ret = OsTcbDispatch(stcb, &info);
788 SCHEDULER_UNLOCK(intSave);
789 OS_GOTO_EXIT_IF(ret != LOS_OK, -ret);
790 } else {
791 /* Make sure that the para is valid */
792 OS_GOTO_EXIT_IF(pid <= 0 || OS_PID_CHECK_INVALID(pid), EINVAL);
793 /* Dispatch the signal to process */
794 SCHEDULER_LOCK(intSave);
795 OsDispatch(pid, &info, OS_USER_KILL_PERMISSION);
796 SCHEDULER_UNLOCK(intSave);
797 }
798 return;
799EXIT:
800 PRINT_ERR("Dispatch signals failed!, ret: %d\r\n", ret);
801#endif
802 return;
803}
804
805int timer_create(clockid_t clockID, struct sigevent *restrict evp, timer_t *restrict timerID)
806{
807 UINT32 ret;
808 UINT16 swtmrID;
809#ifdef LOSCFG_SECURITY_VID
810 UINT16 vid;
811#endif
812
813 if (!timerID || (clockID != CLOCK_REALTIME) || !evp) {
814 errno = EINVAL;
815 return -1;
816 }
817
818 if ((evp->sigev_notify != SIGEV_THREAD) || evp->sigev_notify_attributes) {
819 errno = ENOTSUP;
820 return -1;
821 }
822
823 ret = LOS_SwtmrCreate(1, LOS_SWTMR_MODE_ONCE, (SWTMR_PROC_FUNC)evp->sigev_notify_function,
824 &swtmrID, (UINTPTR)evp->sigev_value.sival_ptr);
825 if (ret != LOS_OK) {
826 errno = (ret == LOS_ERRNO_SWTMR_MAXSIZE) ? EAGAIN : EINVAL;
827 return -1;
828 }
829
830#ifdef LOSCFG_SECURITY_VID
831 vid = AddNodeByRid(swtmrID);
832 if (vid == MAX_INVALID_TIMER_VID) {
833 (VOID)LOS_SwtmrDelete(swtmrID);
834 return -1;
835 }
836 swtmrID = vid;
837#endif
838 *timerID = (timer_t)(UINTPTR)swtmrID;
839 return 0;
840}
841
842int OsTimerCreate(clockid_t clockID, struct ksigevent *evp, timer_t *timerID)
843{
844 UINT32 ret;
845 UINT16 swtmrID;
846 swtmr_proc_arg *arg = NULL;
847 int signo;
848#ifdef LOSCFG_SECURITY_VID
849 UINT16 vid;
850#endif
851
852 if ((clockID != CLOCK_REALTIME) || (timerID == NULL)) {
853 errno = EINVAL;
854 return -1;
855 }
856
857 signo = evp ? evp->sigev_signo : SIGALRM;
858 if (signo > SIGRTMAX || signo < 1) {
859 errno = EINVAL;
860 return -1;
861 }
862 if (evp && (evp->sigev_notify != SIGEV_SIGNAL && evp->sigev_notify != SIGEV_THREAD_ID)) {
863 errno = ENOTSUP;
864 return -1;
865 }
866
867 arg = (swtmr_proc_arg *)malloc(sizeof(swtmr_proc_arg));
868 if (arg == NULL) {
869 errno = ENOMEM;
870 return -1;
871 }
872
873 arg->tid = evp ? evp->sigev_tid : 0;
874 arg->sigev_signo = signo;
875 arg->pid = LOS_GetCurrProcessID();
876 arg->sigev_value.sival_ptr = evp ? evp->sigev_value.sival_ptr : NULL;
877 ret = LOS_SwtmrCreate(1, LOS_SWTMR_MODE_ONCE, SwtmrProc, &swtmrID, (UINTPTR)arg);
878 if (ret != LOS_OK) {
879 errno = (ret == LOS_ERRNO_SWTMR_MAXSIZE) ? EAGAIN : EINVAL;
880 free(arg);
881 return -1;
882 }
883
884#ifdef LOSCFG_SECURITY_VID
885 vid = AddNodeByRid(swtmrID);
886 if (vid == MAX_INVALID_TIMER_VID) {
887 free(arg);
888 (VOID)LOS_SwtmrDelete(swtmrID);
889 return -1;
890 }
891 swtmrID = vid;
892#endif
893 *timerID = (timer_t)(UINTPTR)swtmrID;
894 return 0;
895}
896
897int timer_delete(timer_t timerID)
898{
899 UINT16 swtmrID = (UINT16)(UINTPTR)timerID;
900 VOID *arg = NULL;
901 UINTPTR swtmrProc;
902
903#ifdef LOSCFG_SECURITY_VID
904 swtmrID = GetRidByVid(swtmrID);
905#endif
906 if (OS_INT_ACTIVE || !ValidTimerID(swtmrID)) {
907 goto ERROUT;
908 }
909
910 arg = (VOID *)OS_SWT_FROM_SID(swtmrID)->uwArg;
911 swtmrProc = (UINTPTR)OS_SWT_FROM_SID(swtmrID)->pfnHandler;
912 if (LOS_SwtmrDelete(swtmrID)) {
913 goto ERROUT;
914 }
915 if ((swtmrProc == (UINTPTR)SwtmrProc) && (arg != NULL)) {
916 free(arg);
917 }
918
919#ifdef LOSCFG_SECURITY_VID
920 RemoveNodeByVid((UINT16)(UINTPTR)timerID);
921#endif
922 return 0;
923
924ERROUT:
925 errno = EINVAL;
926 return -1;
927}
928
929int timer_settime(timer_t timerID, int flags,
930 const struct itimerspec *value, /* new value */
931 struct itimerspec *oldValue) /* old value to return, always 0 */
932{
933 UINT16 swtmrID = (UINT16)(UINTPTR)timerID;
934 SWTMR_CTRL_S *swtmr = NULL;
935 UINT32 interval, expiry, ret;
936 UINT32 intSave;
937
938 if (flags != 0) {
939 /* flags not supported currently */
940 errno = ENOSYS;
941 return -1;
942 }
943
944#ifdef LOSCFG_SECURITY_VID
945 swtmrID = GetRidByVid(swtmrID);
946#endif
947 if ((value == NULL) || OS_INT_ACTIVE || !ValidTimerID(swtmrID)) {
948 errno = EINVAL;
949 return -1;
950 }
951
952 if (!ValidTimeSpec(&value->it_value) || !ValidTimeSpec(&value->it_interval)) {
953 errno = EINVAL;
954 return -1;
955 }
956
957 if (oldValue) {
958 (VOID)timer_gettime(timerID, oldValue);
959 }
960
961 swtmr = OS_SWT_FROM_SID(swtmrID);
962 ret = LOS_SwtmrStop(swtmr->usTimerID);
963 if ((ret != LOS_OK) && (ret != LOS_ERRNO_SWTMR_NOT_STARTED)) {
964 errno = EINVAL;
965 return -1;
966 }
967
968 expiry = OsTimeSpec2Tick(&value->it_value);
969 interval = OsTimeSpec2Tick(&value->it_interval);
970
971 LOS_SpinLockSave(&g_swtmrSpin, &intSave);
973 swtmr->uwExpiry = expiry + !!expiry; // PS: skip the first tick because it is NOT a full tick.
974 swtmr->uwInterval = interval;
975 swtmr->uwOverrun = 0;
977
978 if ((value->it_value.tv_sec == 0) && (value->it_value.tv_nsec == 0)) {
979 /*
980 * 1) when expiry is 0, means timer should be stopped.
981 * 2) If timer is ticking, stopping timer is already done before.
982 * 3) If timer is created but not ticking, return 0 as well.
983 */
984 return 0;
985 }
986
987 if (LOS_SwtmrStart(swtmr->usTimerID)) {
988 errno = EINVAL;
989 return -1;
990 }
991
992 return 0;
993}
994
995int timer_gettime(timer_t timerID, struct itimerspec *value)
996{
997 UINT32 tick = 0;
998 SWTMR_CTRL_S *swtmr = NULL;
999 UINT16 swtmrID = (UINT16)(UINTPTR)timerID;
1000 UINT32 ret;
1001
1002#ifdef LOSCFG_SECURITY_VID
1003 swtmrID = GetRidByVid(swtmrID);
1004#endif
1005 if ((value == NULL) || !ValidTimerID(swtmrID)) {
1006 errno = EINVAL;
1007 return -1;
1008 }
1009
1010 swtmr = OS_SWT_FROM_SID(swtmrID);
1011
1012 /* get expire time */
1013 ret = LOS_SwtmrTimeGet(swtmr->usTimerID, &tick);
1014 if ((ret != LOS_OK) && (ret != LOS_ERRNO_SWTMR_NOT_STARTED)) {
1015 errno = EINVAL;
1016 return -1;
1017 }
1018
1019 OsTick2TimeSpec(&value->it_value, tick);
1020 OsTick2TimeSpec(&value->it_interval, (swtmr->ucMode == LOS_SWTMR_MODE_ONCE) ? 0 : swtmr->uwInterval);
1021 return 0;
1022}
1023
1024int timer_getoverrun(timer_t timerID)
1025{
1026 UINT16 swtmrID = (UINT16)(UINTPTR)timerID;
1027 SWTMR_CTRL_S *swtmr = NULL;
1028 INT32 overRun;
1029
1030#ifdef LOSCFG_SECURITY_VID
1031 swtmrID = GetRidByVid(swtmrID);
1032#endif
1033 if (!ValidTimerID(swtmrID)) {
1034 errno = EINVAL;
1035 return -1;
1036 }
1037
1038 swtmr = OS_SWT_FROM_SID(swtmrID);
1039 if (swtmr->usTimerID >= OS_SWTMR_MAX_TIMERID) {
1040 errno = EINVAL;
1041 return -1;
1042 }
1043
1044 overRun = (INT32)(swtmr->uwOverrun);
1045 return (overRun > DELAYTIMER_MAX) ? DELAYTIMER_MAX : overRun;
1046}
1047
1048STATIC INT32 DoNanoSleep(UINT64 nanoseconds)
1049{
1050 UINT32 ret;
1051
1052 ret = LOS_TaskDelay(OsNS2Tick(nanoseconds));
1053 if (ret == LOS_OK || ret == LOS_ERRNO_TSK_YIELD_NOT_ENOUGH_TASK) {
1054 return 0;
1055 }
1056 return -1;
1057}
1058
1059#ifdef LOSCFG_LIBC_NEWLIB
1060int usleep(unsigned long useconds)
1061#else
1062int usleep(unsigned useconds)
1063#endif
1064{
1065 return DoNanoSleep((UINT64)useconds * OS_SYS_NS_PER_US);
1066}
1067
1068int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
1069{
1070 UINT64 nanoseconds;
1071 INT32 ret = -1;
1072
1073 (VOID)rmtp;
1074 /* expire time */
1075
1076 if (!ValidTimeSpec(rqtp)) {
1077 errno = EINVAL;
1078 return ret;
1079 }
1080
1081 nanoseconds = (UINT64)rqtp->tv_sec * OS_SYS_NS_PER_SECOND + rqtp->tv_nsec;
1082
1083 return DoNanoSleep(nanoseconds);
1084}
1085
1086unsigned int sleep(unsigned int seconds)
1087{
1088 return DoNanoSleep((UINT64)seconds * OS_SYS_NS_PER_SECOND);
1089}
1090
1091double difftime(time_t time2, time_t time1)
1092{
1093 return (double)(time2 - time1);
1094}
1095
1096clock_t clock(VOID)
1097{
1098 clock_t clockMsec;
1099 UINT64 nowNsec;
1100
1101 nowNsec = LOS_CurrNanosec();
1102 clockMsec = (clock_t)(nowNsec / (OS_SYS_NS_PER_SECOND / CLOCKS_PER_SEC));
1103
1104 return clockMsec;
1105}
1106
1107clock_t times(struct tms *buf)
1108{
1109 clock_t clockTick = -1;
1110
1111 (void)buf;
1112 set_errno(ENOSYS);
1113
1114 return clockTick;
1115}
1116
1117int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
1118{
1119 UINT32 intSave;
1120 LosTaskCB *taskCB = OS_TCB_FROM_TID(LOS_CurTaskIDGet());
1121 LosProcessCB *processCB = OS_PCB_FROM_PID(taskCB->processID);
1122 timer_t timerID = 0;
1123 struct itimerspec spec;
1124 struct itimerspec ospec;
1125 int ret = LOS_OK;
1126
1127 /* we only support the realtime clock timer currently */
1128 if (which != ITIMER_REAL || !value) {
1129 set_errno(EINVAL);
1130 return -1;
1131 }
1132
1133 /* To avoid creating an invalid timer after the timer has already been create */
1134 if (processCB->timerID == (timer_t)(UINTPTR)MAX_INVALID_TIMER_VID) {
1135 ret = OsTimerCreate(CLOCK_REALTIME, NULL, &timerID);
1136 if (ret != LOS_OK) {
1137 return ret;
1138 }
1139 }
1140
1141 /* The initialization of this global timer must be in spinlock
1142 * OsTimerCreate cannot be located in spinlock.
1143 */
1144 SCHEDULER_LOCK(intSave);
1145 if (processCB->timerID == (timer_t)(UINTPTR)MAX_INVALID_TIMER_VID) {
1146 processCB->timerID = timerID;
1147 SCHEDULER_UNLOCK(intSave);
1148 } else {
1149 SCHEDULER_UNLOCK(intSave);
1150 if (timerID) {
1151 timer_delete(timerID);
1152 }
1153 }
1154
1155 if (!ValidTimeval(&value->it_value) || !ValidTimeval(&value->it_interval)) {
1156 set_errno(EINVAL);
1157 return -1;
1158 }
1159
1160 TIMEVAL_TO_TIMESPEC(&value->it_value, &spec.it_value);
1161 TIMEVAL_TO_TIMESPEC(&value->it_interval, &spec.it_interval);
1162
1163 ret = timer_settime(processCB->timerID, 0, &spec, ovalue ? &ospec : NULL);
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);
1167 }
1168
1169 return ret;
1170}
1171
1172int getitimer(int which, struct itimerval *value)
1173{
1174 LosTaskCB *taskCB = OS_TCB_FROM_TID(LOS_CurTaskIDGet());
1175 LosProcessCB *processCB = OS_PCB_FROM_PID(taskCB->processID);
1176 struct itimerspec spec = {};
1177
1178 int ret = LOS_OK;
1179
1180 /* we only support the realtime clock timer currently */
1181 if (which != ITIMER_REAL || !value) {
1182 set_errno(EINVAL);
1183 return -1;
1184 }
1185
1186 if (processCB->timerID != (timer_t)(UINTPTR)MAX_INVALID_TIMER_VID) {
1187 ret = timer_gettime(processCB->timerID, &spec);
1188 }
1189
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);
1193 }
1194
1195 return ret;
1196}
1197
1198#ifdef LOSCFG_KERNEL_VDSO
1199/// 将最新的时间刷进数据页
1200VOID OsVdsoTimeGet(VdsoDataPage *vdsoDataPage)
1201{
1202 UINT32 intSave;
1203 struct timespec64 tmp = {0};
1204 struct timespec64 hwTime = {0};
1205
1206 if (vdsoDataPage == NULL) {
1207 return;
1208 }
1209
1210 OsGetHwTime(&hwTime);//获取硬件时间
1211
1212 LOS_SpinLockSave(&g_timeSpin, &intSave);
1213 tmp = OsTimeSpecAdd(hwTime, g_accDeltaFromAdj);//
1214 vdsoDataPage->monoTimeSec = tmp.tv_sec;
1215 vdsoDataPage->monoTimeNsec = tmp.tv_nsec;
1216
1217 tmp = OsTimeSpecAdd(tmp, g_accDeltaFromSet);
1218 vdsoDataPage->realTimeSec = tmp.tv_sec;
1219 vdsoDataPage->realTimeNsec = tmp.tv_nsec;
1220 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
1221}
1222#endif
1223
1224time_t time(time_t *t)
1225{
1226 struct timeval tp;
1227 int ret;
1228
1229 /* Get the current time from the system */
1230 ret = gettimeofday(&tp, (struct timezone *)NULL);
1231 if (ret == LOS_OK) {
1232 /* Return the seconds since the epoch */
1233 if (t) {
1234 *t = tp.tv_sec;
1235 }
1236 return tp.tv_sec;
1237 }
1238 return (time_t)OS_ERROR;
1239}
BOOL IsCapPermit(UINT32 capIndex)
Definition: capability.c:43
LITE_OS_SEC_TEXT UINT32 LOS_SwtmrStop(UINT16 swtmrID)
接口函数 停止定时器 参数定时任务ID
Definition: los_swtmr.c:808
LITE_OS_SEC_TEXT UINT32 LOS_SwtmrStart(UINT16 swtmrID)
接口函数 启动定时器 参数定时任务ID
Definition: los_swtmr.c:764
LITE_OS_SEC_TEXT UINT32 LOS_SwtmrTimeGet(UINT16 swtmrID, UINT32 *tick)
接口函数 获得软件定时器剩余Tick数 通过 *tick 带走
Definition: los_swtmr.c:848
VOID(* SWTMR_PROC_FUNC)(UINTPTR arg)
Define the type of a callback function that handles software timer timeout.
Definition: los_swtmr.h:260
LITE_OS_SEC_TEXT UINT32 LOS_SwtmrDelete(UINT16 swtmrID)
接口函数 删除定时器
Definition: los_swtmr.c:889
LITE_OS_SEC_TEXT_INIT UINT32 LOS_SwtmrCreate(UINT32 interval, UINT8 mode, SWTMR_PROC_FUNC handler, UINT16 *swtmrID, UINTPTR arg)
创建定时器,设置定时器的定时时长、定时器模式、回调函数,并返回定时器ID
Definition: los_swtmr.c:712
@ LOS_SWTMR_MODE_OPP
Definition: los_swtmr.h:235
@ LOS_SWTMR_MODE_ONCE
Definition: los_swtmr.h:232
@ LOS_SWTMR_MODE_NO_SELFDELETE
Definition: los_swtmr.h:234
LITE_OS_SEC_TEXT_MINOR UINT32 OsNS2Tick(UINT64 nanoseconds)
纳秒转化成 tick
Definition: los_sys.c:125
LITE_OS_SEC_TEXT UINT32 LOS_CurTaskIDGet(VOID)
Obtain current running task ID.
Definition: los_task.c:331
LITE_OS_SEC_TEXT UINT32 LOS_TaskDelay(UINT32 tick)
任务延时等待,释放CPU,等待时间到期后该任务会重新进入ready状态
Definition: los_task.c:1020
LITE_OS_SEC_TEXT_MINOR UINT64 LOS_CurrNanosec(VOID)
获取自系统启动以来的纳秒数
Definition: los_hw_tick.c:62
LITE_OS_SEC_TEXT UINT32 LOS_GetCurrProcessID(VOID)
获取当前进程的进程ID
Definition: los_process.c:2161
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)
信号分发,发送信号权限/进程组过滤.
Definition: los_signal.c:412
static int GOOD_SIGNO(unsigned int sig)
信号ID是否有效
Definition: los_signal.h:158
VOID LOS_SpinUnlockRestore(SPIN_LOCK_S *lock, UINT32 intSave)
Definition: los_spinlock.c:108
VOID LOS_SpinLockSave(SPIN_LOCK_S *lock, UINT32 *intSave)
Definition: los_spinlock.c:98
SPIN_LOCK_S g_swtmrSpin
INT32 OsUserProcessOperatePermissionsCheck(const LosTaskCB *taskCB, UINT32 processID)
Definition: los_task.c:1385
STATIC INLINE LosTaskCB * OsGetTaskCB(UINT32 taskID)
通过任务ID获取任务实体,task由任务池分配,本质是个数组,彼此都挨在一块
Definition: los_task_pri.h:250
INT32 OsTcbDispatch(LosTaskCB *stcb, siginfo_t *info)
给任务(线程)发送一个信号
Definition: los_signal.c:182
unsigned short UINT16
Definition: los_typedef.h:56
signed int INT32
Definition: los_typedef.h:60
long unsigned int UINT64
Definition: los_typedef.h:66
unsigned long UINTPTR
Definition: los_typedef.h:68
unsigned int UINT32
Definition: los_typedef.h:57
size_t BOOL
Definition: los_typedef.h:88
long signed int INT64
Definition: los_typedef.h:67
void * malloc(size_t size)
动态分配内存块大小
Definition: malloc.c:81
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
UINT64 allTime
Definition: los_cpup_pri.h:50
timer_t timerID
OsCpupBase * processCpup
OsCpupBase taskCpup
UINT32 processID
INT64 monoTimeSec
系统运行时间,从系统启动时开始计时,速度更快精度更低,系统休眠时不再计时
INT64 realTimeNsec
单位纳秒: 系统实时时间
INT64 realTimeSec
单位秒: 系统实时时间
int sigev_tid
Definition: time_posix.h:49
int sigev_signo
Definition: time_posix.h:47
union sigval sigev_value
Definition: time_posix.h:46
int sigev_notify
Definition: time_posix.h:48
union sigval sigev_value
Definition: time.c:752
unsigned int tid
Definition: time.c:751
pid_t pid
Definition: time.c:750
int sigev_signo
Definition: time.c:749
UINT16 usTimerID
Definition: los_swtmr.h:271
UINT32 uwOverrun
Definition: los_swtmr.h:272
UINT8 ucMode
Definition: los_swtmr.h:270
UINT32 uwInterval
Definition: los_swtmr.h:274
UINT32 uwExpiry
Definition: los_swtmr.h:275
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)
获取硬件时间
Definition: time.c:287
int timer_delete(timer_t timerID)
Definition: time.c:897
int setlocalseconds(int seconds)
设置本地秒数
Definition: time.c:356
STATIC long long g_adjTimeLeft
Definition: time.c:138
VOID OsAdjTime(VOID)
Definition: time.c:150
STATIC INT32 DoNanoSleep(UINT64 nanoseconds)
Definition: time.c:1048
STATIC const long long g_adjPacement
Definition: time.c:142
STATIC struct timespec64 g_accDeltaFromAdj
Definition: time.c:146
int clock_nanosleep(clockid_t clk, int flags, const struct timespec *req, struct timespec *rem)
Definition: time.c:718
int settimeofday64(const struct timeval64 *tv, const struct timezone *tz)
Definition: time.c:346
int clock_settime(clockid_t clockID, const struct timespec *tp)
Definition: time.c:452
const pid_t GetPidFromClockID(clockid_t clockID)
Definition: time.c:498
static VOID SwtmrProc(UINTPTR tmrArg)
Definition: time.c:755
int getitimer(int which, struct itimerval *value)
Definition: time.c:1172
STATIC INT32 g_adjDirection
Definition: time.c:139
STATIC INT32 OsSetTimeOfDay(const struct timeval64 *tv, const struct timezone *tz)
设置日历
Definition: time.c:296
int gettimeofday64(struct timeval64 *tv, struct timezone *tz)
Definition: time.c:391
STATIC INLINE BOOL ValidTimeval(const struct timeval *tv)
Definition: time.c:92
int OsTimerCreate(clockid_t clockID, struct ksigevent *evp, timer_t *timerID)
Definition: time.c:842
int timer_settime(timer_t timerID, int flags, const struct itimerspec *value, struct itimerspec *oldValue)
Definition: time.c:929
UINT32 GetTidFromClockID(clockid_t clockID)
Definition: time.c:491
double difftime(time_t time2, time_t time1)
Definition: time.c:1091
int clock_getres(clockid_t clockID, struct timespec *tp)
Definition: time.c:676
unsigned int sleep(unsigned int seconds)
Definition: time.c:1086
int settimeofday(const struct timeval *tv, const struct timezone *tz)
Definition: time.c:331
STATIC INT32 OsGetTimeOfDay(struct timeval64 *tv, struct timezone *tz)
获取日历时间
Definition: time.c:366
static int PthreadGetCputime(clockid_t clockID, struct timespec *ats)
Definition: time.c:505
clock_t times(struct tms *buf)
Definition: time.c:1107
if(tv==NULL)
Definition: time.c:430
int gettimeofday(struct timeval *tv, void *_tz) int gettimeofday(struct timeval *tv
gettimeofday
static int ProcessGetCputime(clockid_t clockID, struct timespec *ats)
Definition: time.c:531
int struct timezone * tz
Definition: time.c:424
int timer_gettime(timer_t timerID, struct itimerspec *value)
Definition: time.c:995
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
Definition: time.c:1068
VOID OsVdsoTimeGet(VdsoDataPage *vdsoDataPage)
将最新的时间刷进数据页
Definition: time.c:1200
static int CheckClock(const clockid_t clockID)
Definition: time.c:578
STATIC INLINE BOOL ValidTimerID(UINT16 swtmrID)
Definition: time.c:122
clock_t clock(VOID)
Definition: time.c:1096
static int GetCputime(clockid_t clockID, struct timespec *tp)
Definition: time.c:561
int timer_create(clockid_t clockID, struct sigevent *restrict evp, timer_t *restrict timerID)
Definition: time.c:805
int usleep(unsigned long useconds) int usleep(unsigned useconds)
Definition: time.c:1060
int adjtime(const struct timeval *delta, struct timeval *oldDelta)
Definition: time.c:208
STATIC INLINE BOOL ValidTimeval64(const struct timeval64 *tv)
Definition: time.c:107
int timer_getoverrun(timer_t timerID)
Definition: time.c:1024
int clock_gettime(clockid_t clockID, struct timespec *tp)
当用户程序进行特定系统调用时(例如clock_gettime(CLOCK_REALTIME_COARSE, &ts)),VDSO代码页会将其拦截;
Definition: time.c:614
STATIC INLINE struct timespec64 OsTimeSpecAdd(const struct timespec64 t1, const struct timespec64 t2)
增加指定时间
Definition: time.c:255
static int CpuClockGetres(const clockid_t clockID, struct timespec *tp)
Definition: time.c:599
time_t time(time_t *t)
Definition: time.c:1224
STATIC struct timespec64 g_accDeltaFromSet
Definition: time.c:148
STATIC INLINE struct timespec64 OsTimeSpecSub(const struct timespec64 t1, const struct timespec64 t2)
减少指定时间
Definition: time.c:273
STATIC SPIN_LOCK_INIT(g_timeSpin)
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
Definition: time.c:1117
STATIC INLINE BOOL ValidTimeSpec(const struct timespec *tp)
Definition: time_posix.h:53
STATIC INLINE VOID OsTick2TimeSpec(struct timespec *tp, UINT32 tick)
Definition: time_posix.h:81
STATIC INLINE UINT32 OsTimeSpec2Tick(const struct timespec *tp)
Definition: time_posix.h:68
UINT16 GetRidByVid(UINT16 vid)
Definition: vid.c:212
void RemoveNodeByVid(UINT16 vid)
Definition: vid.c:221
UINT16 AddNodeByRid(UINT16 rid)
Definition: vid.c:178