1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef __FUNCTIONALEXT_PTHREAD_UTIL_H__
17 #define __FUNCTIONALEXT_PTHREAD_UTIL_H__
18
19 #include <time.h>
20 #include <sys/shm.h>
21 #include "test.h"
22
23 #define EXPECT_EQ(a, b) do { \
24 if ((a) != (b)) \
25 t_error("failed %d != %d \n", a, b); \
26 } while (0)
27
28 #define EXPECT_GE(a, b) do { \
29 if ((a) < (b)) \
30 t_error("failed %d < %d \n", a, b); \
31 } while (0)
32
33 #define EXPECT_LE(a, b) do { \
34 if ((a) > (b)) \
35 t_error("failed %d < %d \n", a, b); \
36 } while (0)
37
38 #define TEST(c, ...) ((c) || (t_error(#c " failed: " __VA_ARGS__), 0))
39 #define NSEC_PER_SEC 1000000000
40 #define NSEC_PER_100MS 100000000
41 #define NSEC_PER_MSEC 1000000
42 #define US_PER_S 1000000
43 #define MS_PER_S 1000
44 #define SLEEP_200_MS 200
45 #define SLEEP_100_MS 100
46 #define DELAY_TIME_100_MS 100
47 #define SLEEP_50_MS 50
48 #define SLEEP_20_MS 20
49 #define SLEEP_10_MS 10
50 #define CHECK_STEP_FIVE 5
51 #define CHECK_STEP_FOUR 4
52 #define CHECK_STEP_THREE 3
53 #define CHECK_STEP_TWO 2
54 #define CHECK_STEP_ONE 1
55
56 static int CARRY = 4;
57 static int SHMID_CHECK_STEP = 0;
58 static long unsigned int ONE_KB = 1024;
59 static int FLAG = 0666;
60
61 typedef void(*TEST_FUN)(void);
62
63 // get cur-time plus ms
GetDelayedTimeByClockid(struct timespec * ts,unsigned int ms,clockid_t clockid)64 static inline void GetDelayedTimeByClockid(struct timespec *ts, unsigned int ms, clockid_t clockid)
65 {
66 const unsigned int nsecPerSec = NSEC_PER_SEC;
67 unsigned int setTimeNs = ms * US_PER_S;
68 struct timespec tsNow = {0};
69 clock_gettime(clockid, &tsNow);
70 ts->tv_sec = tsNow.tv_sec + (tsNow.tv_nsec + setTimeNs) / nsecPerSec;
71 ts->tv_nsec = (tsNow.tv_nsec + setTimeNs) % nsecPerSec;
72 }
73
74 // calculate time difference, in ms
GetTimeDiff(struct timespec ts1,struct timespec ts2)75 static inline int GetTimeDiff(struct timespec ts1, struct timespec ts2)
76 {
77 const unsigned int nsecPerSec = NSEC_PER_SEC;
78 int ms = (ts1.tv_sec - ts2.tv_sec) * nsecPerSec + (ts1.tv_nsec - ts2.tv_nsec);
79 ms = ms / NSEC_PER_MSEC;
80 return ms;
81 }
82
Msleep(int msec)83 static inline void Msleep(int msec)
84 {
85 usleep(msec * MS_PER_S);
86 }
87
88 #endif // __FUNCTIONALEXT_PTHREAD_UTIL_H__