diff --git a/compat/posix/src/time.c b/compat/posix/src/time.c index 97a82ae03c4029b747280c97f8950b39df7304a1..6c262960609813a795acbe9cee22d84169f2afd7 100644 --- a/compat/posix/src/time.c +++ b/compat/posix/src/time.c @@ -50,6 +50,8 @@ #include "los_swtmr_pri.h" #include "los_sys_pri.h" +#define CPUCLOCK_PERTHREAD_MASK 4 +#define CPUCLOCK_ID_OFFSET 3 /* * Do a time package defined return. This requires the error code @@ -450,13 +452,82 @@ int clock_settime(clockid_t clockID, const struct timespec *tp) return settimeofday(&tv, NULL); } +static int PthreadGetCputime(clockid_t clockID, struct timespec *ats) +{ + uint64_t runtime; + UINT32 intSave; + UINT32 tid = ((UINT32) ~((clockID) >> CPUCLOCK_ID_OFFSET)); + + if (OS_TID_CHECK_INVALID(tid)) { + return -ESRCH; + } + LosTaskCB *task = OsGetTaskCB(tid); + + if (OsCurrTaskGet()->processID != task->processID) { + return -EINVAL; + } + + SCHEDULER_LOCK(intSave); + runtime = task->taskCpup.allTime; + SCHEDULER_UNLOCK(intSave); + + ats->tv_sec = runtime / OS_SYS_NS_PER_SECOND; + ats->tv_nsec = runtime % OS_SYS_NS_PER_SECOND; + + return 0; +} + +static int ProcessGetCputime(clockid_t clockID, struct timespec *ats) +{ + UINT64 runtime; + UINT32 intSave; + const pid_t pid = ((pid_t) ~((clockID) >> CPUCLOCK_ID_OFFSET)); + LosProcessCB *spcb = NULL; + + if (OsProcessIDUserCheckInvalid(pid) || pid < 0) { + return -EINVAL; + } + + spcb = OS_PCB_FROM_PID(pid); + if (OsProcessIsUnused(spcb)) { + return -EINVAL; + } + + SCHEDULER_LOCK(intSave); + runtime = spcb->processCpup.allTime; + SCHEDULER_UNLOCK(intSave); + + ats->tv_sec = runtime / OS_SYS_NS_PER_SECOND; + ats->tv_nsec = runtime % OS_SYS_NS_PER_SECOND; + + return 0; +} + +static int GetCputime(clockid_t clockID, struct timespec *tp) +{ + int ret; + + if (clockID >= 0) { + return -EINVAL; + } + + if ((UINT32)clockID & CPUCLOCK_PERTHREAD_MASK) { + ret = PthreadGetCputime(clockID, tp); + } else { + ret = ProcessGetCputime(clockID, tp); + } + + return ret; +} + int clock_gettime(clockid_t clockID, struct timespec *tp) { UINT32 intSave; struct timespec64 tmp = {0}; struct timespec64 hwTime = {0}; + int ret; - if ((clockID > MAX_CLOCKS) || (clockID < CLOCK_REALTIME)) { + if (clockID > MAX_CLOCKS) { goto ERROUT; } @@ -497,7 +568,14 @@ int clock_gettime(clockid_t clockID, struct timespec *tp) case CLOCK_TAI: TIME_RETURN(ENOTSUP); default: - goto ERROUT; + { +#ifdef LOSCFG_KERNEL_CPUP + ret = GetCputime(clockID, tp); + TIME_RETURN(-ret); +#else + TIME_RETURN(EINVAL); +#endif + } } return 0; @@ -506,8 +584,45 @@ ERROUT: TIME_RETURN(EINVAL); } +static int CheckClock(const clockid_t clockID) +{ + int error = 0; + const pid_t pid = ((pid_t) ~((clockID) >> CPUCLOCK_ID_OFFSET)); + + if (!((UINT32)clockID & CPUCLOCK_PERTHREAD_MASK)) { + LosProcessCB *spcb = NULL; + if (OsProcessIDUserCheckInvalid(pid) || pid < 0) { + return -EINVAL; + } + spcb = OS_PCB_FROM_PID(pid); + if (OsProcessIsUnused(spcb)) { + error = -EINVAL; + } + } else { + error = -EINVAL; + } + + return error; +} + +static int CpuClockGetres(const clockid_t clockID, struct timespec *tp) +{ + if (clockID > 0) { + return -EINVAL; + } + + int error = CheckClock(clockID); + if (!error) { + error = ProcessGetCputime(clockID, tp); + } + + return error; +} + int clock_getres(clockid_t clockID, struct timespec *tp) { + int ret; + if (tp == NULL) { TIME_RETURN(EINVAL); } @@ -536,7 +651,14 @@ int clock_getres(clockid_t clockID, struct timespec *tp) case CLOCK_TAI: TIME_RETURN(ENOTSUP); default: +#ifdef LOSCFG_KERNEL_CPUP + { + ret = CpuClockGetres(clockID, tp); + TIME_RETURN(-ret); + } +#else TIME_RETURN(EINVAL); +#endif } TIME_RETURN(0); diff --git a/testsuites/unittest/posix/pthread/posix_pthread_test.cpp b/testsuites/unittest/posix/pthread/posix_pthread_test.cpp index 99821340cd5095f68ae7dad10be92208e1541d1a..5b28bf5fbffdc80a13e356388e8bbed6bcbef3f7 100644 --- a/testsuites/unittest/posix/pthread/posix_pthread_test.cpp +++ b/testsuites/unittest/posix/pthread/posix_pthread_test.cpp @@ -327,6 +327,17 @@ HWTEST_F(PosixPthreadTest, ItPosixPthread022, TestSize.Level0) ItPosixPthread022(); // pthread_cancel } +/* * + * @tc.name: IT_POSIX_PTHREAD_203 + * @tc.desc: function for pthread concurrency + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(PosixPthreadTest, ItPosixPthread203, TestSize.Level0) +{ + ItPosixPthread203(); +} + #endif #if defined(LOSCFG_USER_TEST_FULL) diff --git a/testsuites/unittest/process/mutex/process_mutex_test.cpp b/testsuites/unittest/process/mutex/process_mutex_test.cpp index 91b588f9ec25a0cee64f74cb02afe46167bcb2da..804d67d1369bba4b3f3c50ee6e4ee58d0ecea4ec 100644 --- a/testsuites/unittest/process/mutex/process_mutex_test.cpp +++ b/testsuites/unittest/process/mutex/process_mutex_test.cpp @@ -293,5 +293,38 @@ HWTEST_F(ProcessMutexTest, ItTestPthreadMutex022, TestSize.Level0) { ItTestPthreadMutex022(); } + +/* * + * @tc.name: it_test_pthread_mutex_023 + * @tc.desc: function for test mutexattr robust + * @tc.type: FUNC + * @tc.require: AR000E0QAB + */ +HWTEST_F(ProcessMutexTest, ItTestPthreadMutex023, TestSize.Level0) +{ + ItTestPthreadMutex023(); +} + +/* * + * @tc.name: it_test_pthread_mutex_024 + * @tc.desc: function for test mutexattr robust:error return value + * @tc.type: FUNC + * @tc.require: AR000E0QAB + */ +HWTEST_F(ProcessMutexTest, ItTestPthreadMutex024, TestSize.Level0) +{ + ItTestPthreadMutex024(); +} + +/* * + * @tc.name: it_test_pthread_mutex_025 + * @tc.desc: test mutexattr robust:robustness product deadlock is not set + * @tc.type: FUNC + * @tc.require: AR000E0QAB + */ +HWTEST_F(ProcessMutexTest, ItTestPthreadMutex025, TestSize.Level0) +{ + ItTestPthreadMutex025(); +} #endif } // namespace OHOS diff --git a/testsuites/unittest/time/clock/time_clock_test.cpp b/testsuites/unittest/time/clock/time_clock_test.cpp index 8b061b99b3fc2063163bbc749d2c04f7b3032e94..43655d17bc146d8026235631500199f9b34b059c 100644 --- a/testsuites/unittest/time/clock/time_clock_test.cpp +++ b/testsuites/unittest/time/clock/time_clock_test.cpp @@ -147,5 +147,27 @@ HWTEST_F(TimeClockTest, ClockTest010, TestSize.Level0) ClockTest010(); } +/* * + * @tc.name: ClockTest011 + * @tc.desc: test pthread_getcpuclockid:get pthread time + * @tc.type: FUNC + * @tc.require: AR000E0QAB + */ +HWTEST_F(TimeClockTest, ClockTest011, TestSize.Level0) +{ + ClockTest011(); +} + +/* * + * @tc.name: ClockTest012 + * @tc.desc: test clock_getcpuclockid:get process time + * @tc.type: FUNC + * @tc.require: AR000E0QAB + */ +HWTEST_F(TimeClockTest, ClockTest012, TestSize.Level0) +{ + ClockTest012(); +} + #endif } // namespace OHOS diff --git a/testsuites/unittest/time/timer/time_timer_test.cpp b/testsuites/unittest/time/timer/time_timer_test.cpp index d57d908db54b3d42d472646024632e5ceaeca728..187ea91149f72ab6ab2c1d4daab331e01d1b7c4f 100644 --- a/testsuites/unittest/time/timer/time_timer_test.cpp +++ b/testsuites/unittest/time/timer/time_timer_test.cpp @@ -84,8 +84,29 @@ HWTEST_F(TimeTimerTest, TimerTest003, TestSize.Level0) */ /*HWTEST_F(TimeTimerTest, TimerTest004, TestSize.Level0) { - TimerTest004(); // bug: musl sigaction handler have only one param. + TimerTest004(); // TODO: musl sigaction handler have only one param. }*/ +/* * + * @tc.name: TIME_TEST_TZSET_001 + * @tc.desc: function for TIME_TEST_TZSET_001 + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(TimeTimerTest, TIME_TEST_TZSET_001, TestSize.Level0) +{ + TIME_TEST_TZSET_001(); +} + +/* * + * @tc.name: TIME_TEST_TZSET_002 + * @tc.desc: function for TimeTimerTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(TimeTimerTest, TIME_TEST_TZSET_002, TestSize.Level0) +{ + TIME_TEST_TZSET_002(); +} #endif } // namespace OHOS