Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
ace912ba
Commit
ace912ba
authored
Apr 26, 2018
by
zhujiashun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement clock_gettime using mach_absolute_time
parent
23d6ecc7
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
15 deletions
+32
-15
time.h
src/butil/time.h
+32
-15
No files found.
src/butil/time.h
View file @
ace912ba
...
...
@@ -27,30 +27,47 @@
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
# ifndef clock_gettime
# define CLOCK_REALTIME CALENDAR_CLOCK
# define CLOCK_MONOTONIC SYSTEM_CLOCK
#include <pthread.h>
#include <stdlib.h> // exit
typedef
int
clockid_t
;
static
mach_timebase_info_data_t
timebase
;
static
timespec
inittime
;
static
uint64_t
initticks
;
static
pthread_once_t
init_clock_once
=
PTHREAD_ONCE_INIT
;
static
void
InitClock
()
{
if
(
mach_timebase_info
(
&
timebase
)
!=
0
)
{
exit
(
1
);
}
timeval
micro
;
if
(
gettimeofday
(
&
micro
,
NULL
)
!=
0
)
{
exit
(
1
);
}
inittime
.
tv_sec
=
micro
.
tv_sec
;
inittime
.
tv_nsec
=
micro
.
tv_usec
*
1000L
;
initticks
=
mach_absolute_time
();
}
// clock_gettime is not available in MacOS < 10.12
inline
int
clock_gettime
(
clockid_t
id
,
timespec
*
time
)
{
if
(
id
==
CLOCK_MONOTONIC
)
{
clock_serv_t
cclock
;
mach_timespec_t
mts
;
host_get_clock_service
(
mach_host_self
(),
id
,
&
cclock
);
clock_get_time
(
cclock
,
&
mts
);
mach_port_deallocate
(
mach_task_self
(),
cclock
);
time
->
tv_sec
=
mts
.
tv_sec
;
time
->
tv_nsec
=
mts
.
tv_nsec
;
}
else
if
(
id
==
CLOCK_REALTIME
)
{
struct
timeval
now
;
if
(
gettimeofday
(
&
now
,
NULL
)
<
0
)
{
return
-
1
;
}
time
->
tv_sec
=
now
.
tv_sec
;
time
->
tv_nsec
=
now
.
tv_usec
*
1000
;
if
(
pthread_once
(
&
init_clock_once
,
InitClock
)
!=
0
)
{
exit
(
1
);
}
uint64_t
clock
=
mach_absolute_time
()
-
initticks
;
uint64_t
elapsed
=
clock
*
(
uint64_t
)
timebase
.
numer
/
(
uint64_t
)
timebase
.
denom
;
*
time
=
inittime
;
time
->
tv_sec
+=
elapsed
/
1000000000L
;
time
->
tv_nsec
+=
elapsed
%
1000000000L
;
time
->
tv_sec
+=
time
->
tv_nsec
/
1000000000L
;
time
->
tv_nsec
=
time
->
tv_nsec
%
1000000000L
;
return
0
;
}
# endif
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment