]>
git.saurik.com Git - apple/security.git/blob - SecurityTests/clxutils/threadTest/timeThread.cpp
1 /* quick time() and gmtime() tester. */
2 /* As of Dec. 20, 2000, this test demonstrates that stdlib time() is thread-safe, but
3 * gmtime() is NOT thread safe. */
6 #include "testParams.h"
7 #include <security_utilities/threading.h>
8 #include <CoreFoundation/CFDate.h>
11 #define DO_TIME_LOCK 1
14 int timeInit(TestParams
*tp
)
19 #define INNER_LOOPS 100
21 int timeThread(TestParams
*tp
)
23 CFAbsoluteTime cfTimes
[INNER_LOOPS
];
25 for(unsigned dex
=0; dex
<tp
->numLoops
; dex
++) {
27 printf("timeThread loop %u\n", dex
);
30 printChar(tp
->progressChar
);
33 for(unsigned innerLoop
=0; innerLoop
<INNER_LOOPS
; innerLoop
++) {
34 cfTimes
[innerLoop
] = CFAbsoluteTimeGetCurrent();
42 /* process-wide time base */
43 static time_t baseTime
= 0;
45 static Mutex timeLock
;
47 /* init time base first time thru */
48 int timeInit(TestParams
*tp
)
53 baseTime
= time(NULL
);
54 baseTm
= *gmtime(&baseTime
);
58 int timeThread(TestParams
*tp
)
62 for(dex
=0; dex
<(100 * tp
->numLoops
); dex
++) {
69 nowTm
= *gmtime(&nowTime
);
73 if(nowTime
< baseTime
) {
74 printf("\n***time() went backwards: base %d now %d\n",
75 (int)baseTime
, (int)nowTime
);
78 if((nowTm
.tm_year
< baseTm
.tm_year
) ||
79 (nowTm
.tm_mon
< baseTm
.tm_mon
) ||
80 (nowTm
.tm_mday
< baseTm
.tm_mday
) ||
81 /* careful, this overflows at midnight */
82 (nowTm
.tm_hour
< baseTm
.tm_hour
)) {
83 printf("\n***gmtime() went backwards\n");
84 printf(" baseTm y:%d m:%d d:%d h:%d m:%d\n",
85 baseTm
.tm_year
, baseTm
.tm_mon
, baseTm
.tm_mday
,
86 baseTm
.tm_hour
, baseTm
.tm_min
);
87 printf(" nowTm y:%d m:%d d:%d h:%d m:%d\n",
88 nowTm
.tm_year
, nowTm
.tm_mon
, nowTm
.tm_mday
,
89 nowTm
.tm_hour
, nowTm
.tm_min
);
92 if(((dex
% 100) == 0) && !tp
->quiet
) {
93 printChar(tp
->progressChar
);
99 #endif /* DO_CF_TIME */