Skip to content

Commit 2d25480

Browse files
catapmascguy
authored andcommitted
time.c: added missing CLOCK_*
1 parent bb525c1 commit 2d25480

File tree

3 files changed

+82
-16
lines changed

3 files changed

+82
-16
lines changed

include/time.h

+28-7
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,35 @@ __MP__END_DECLS
4444
#if __MP_LEGACY_SUPPORT_GETTIME__
4545

4646
/* One define types and methods if not already defined. */
47-
#if !defined(CLOCK_REALTIME) && !defined(CLOCK_MONOTONIC) && !defined(CLOCK_MONOTONIC_RAW)
48-
49-
#define CLOCK_REALTIME 0
50-
#define CLOCK_MONOTONIC 6
51-
#define CLOCK_MONOTONIC_RAW 4
47+
#if !defined(CLOCK_REALTIME) && !defined(CLOCK_MONOTONIC)
5248
typedef int clockid_t;
49+
#endif /* !defined(CLOCK_REALTIME) && !defined(CLOCK_MONOTONIC) */
50+
51+
#ifndef CLOCK_REALTIME
52+
#define CLOCK_REALTIME 0
53+
#endif
54+
#ifndef CLOCK_MONOTONIC
55+
#define CLOCK_MONOTONIC 6
56+
#endif
57+
58+
#ifndef CLOCK_MONOTONIC_RAW
59+
#define CLOCK_MONOTONIC_RAW 4
60+
#endif
61+
#ifndef CLOCK_MONOTONIC_RAW_APPROX
62+
#define CLOCK_MONOTONIC_RAW_APPROX 5
63+
#endif
64+
65+
#ifndef CLOCK_UPTIME_RAW
66+
#define CLOCK_UPTIME_RAW 8
67+
#endif
68+
69+
#ifndef CLOCK_UPTIME_RAW_APPROX
70+
#define CLOCK_UPTIME_RAW_APPROX 8
71+
#endif
72+
73+
#ifndef CLOCK_PROCESS_CPUTIME_ID
74+
#define CLOCK_PROCESS_CPUTIME_ID 12
75+
#endif
5376

5477
__MP__BEGIN_DECLS
5578

@@ -58,8 +81,6 @@ extern int clock_getres ( clockid_t clk_id, struct timespec *ts );
5881

5982
__MP__END_DECLS
6083

61-
#endif /* !defined(CLOCK_REALTIME) && !defined(CLOCK_MONOTONIC) */
62-
6384
#endif /* _MP_LEGACY_SUPPORT_GETTIME__ */
6485

6586
/* Legacy implementation of timespec */

src/time.c

+21-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <sys/time.h>
2323
#include <sys/sysctl.h>
24+
#include <sys/resource.h>
2425
#include <mach/mach_time.h>
2526

2627
#define BILLION 1000000000L
@@ -44,19 +45,26 @@ int clock_gettime( clockid_t clk_id, struct timespec *ts )
4445
struct timeval boottime;
4546
size_t boottime_len = sizeof(boottime);
4647
ret = sysctlbyname("kern.boottime", &boottime, &boottime_len, NULL, 0);
47-
if (ret == -1)
48-
{
49-
boottime.tv_sec = 0;
50-
boottime.tv_usec = 0;
51-
}
48+
if (ret != KERN_SUCCESS) { return ret; }
5249
struct timeval tv;
5350
ret = gettimeofday(&tv, NULL);
5451
timersub(&tv, &boottime, &tv);
5552
ts->tv_sec = tv.tv_sec;
5653
ts->tv_nsec = tv.tv_usec * 1000;
5754
ret = 0;
5855
}
59-
else if ( CLOCK_MONOTONIC_RAW == clk_id )
56+
else if ( CLOCK_PROCESS_CPUTIME_ID == clk_id )
57+
{
58+
struct rusage ru;
59+
ret = getrusage(RUSAGE_SELF, &ru);
60+
timeradd(&ru.ru_utime, &ru.ru_stime, &ru.ru_utime);
61+
ts->tv_sec = ru.ru_utime.tv_sec;
62+
ts->tv_nsec = ru.ru_utime.tv_usec * 1000;
63+
}
64+
else if ( CLOCK_MONOTONIC_RAW == clk_id ||
65+
CLOCK_MONOTONIC_RAW_APPROX == clk_id ||
66+
CLOCK_UPTIME_RAW == clk_id ||
67+
CLOCK_UPTIME_RAW_APPROX == clk_id )
6068
{
6169
static mach_timebase_info_data_t timebase;
6270
if ( 0 == timebase.numer || 0 == timebase.denom ) {
@@ -78,14 +86,19 @@ int clock_getres( clockid_t clk_id, struct timespec *ts )
7886
if ( ts )
7987
{
8088
if ( CLOCK_REALTIME == clk_id ||
81-
CLOCK_MONOTONIC == clk_id )
89+
CLOCK_MONOTONIC == clk_id ||
90+
CLOCK_PROCESS_CPUTIME_ID == clk_id )
8291
{
8392
// return 1us precision
8493
ts->tv_sec = 0;
8594
ts->tv_nsec = THOUSAND;
8695
ret = 0;
8796
}
88-
else if ( CLOCK_MONOTONIC_RAW == clk_id )
97+
else if ( CLOCK_MONOTONIC_RAW == clk_id ||
98+
CLOCK_MONOTONIC_RAW_APPROX == clk_id ||
99+
CLOCK_PROCESS_CPUTIME_ID == clk_id ||
100+
CLOCK_UPTIME_RAW == clk_id ||
101+
CLOCK_UPTIME_RAW_APPROX == clk_id )
89102
{
90103
// return 1ns precision
91104
ts->tv_sec = 0;

test/test_time.cpp

+33-1
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,45 @@ int main()
4444
std::cout << "CLOCK_MONOTONIC ("<< CLOCK_MONOTONIC << ") " << time(CLOCK_MONOTONIC) << std::endl;
4545
}
4646
}
47-
{
47+
{
4848
int c = 0;
4949
res(CLOCK_MONOTONIC_RAW);
5050
while ( ++c < 10 )
5151
{
5252
std::cout << "CLOCK_MONOTONIC_RAW ("<< CLOCK_MONOTONIC_RAW << ") " << time(CLOCK_MONOTONIC_RAW) << std::endl;
5353
}
5454
}
55+
{
56+
int c = 0;
57+
res(CLOCK_MONOTONIC_RAW_APPROX);
58+
while ( ++c < 10 )
59+
{
60+
std::cout << "CLOCK_MONOTONIC_RAW_APPROX ("<< CLOCK_MONOTONIC_RAW_APPROX << ") " << time(CLOCK_MONOTONIC_RAW_APPROX) << std::endl;
61+
}
62+
}
63+
{
64+
int c = 0;
65+
res(CLOCK_UPTIME_RAW);
66+
while ( ++c < 10 )
67+
{
68+
std::cout << "CLOCK_UPTIME_RAW ("<< CLOCK_UPTIME_RAW << ") " << time(CLOCK_UPTIME_RAW) << std::endl;
69+
}
70+
}
71+
{
72+
int c = 0;
73+
res(CLOCK_UPTIME_RAW_APPROX);
74+
while ( ++c < 10 )
75+
{
76+
std::cout << "CLOCK_UPTIME_RAW_APPROX ("<< CLOCK_UPTIME_RAW_APPROX << ") " << time(CLOCK_UPTIME_RAW_APPROX) << std::endl;
77+
}
78+
}
79+
{
80+
int c = 0;
81+
res(CLOCK_PROCESS_CPUTIME_ID);
82+
while ( ++c < 10 )
83+
{
84+
std::cout << "CLOCK_PROCESS_CPUTIME_ID ("<< CLOCK_PROCESS_CPUTIME_ID << ") " << time(CLOCK_PROCESS_CPUTIME_ID) << std::endl;
85+
}
86+
}
5587
return 0;
5688
}

0 commit comments

Comments
 (0)