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
4793dfa1
Commit
4793dfa1
authored
Apr 11, 2018
by
zhujiashun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
put clock_gettime into time.h in macos
parent
77180e84
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
23 additions
and
43 deletions
+23
-43
fd.cpp
src/bthread/fd.cpp
+2
-8
time.cpp
src/butil/time.cpp
+1
-8
time.h
src/butil/time.h
+19
-20
baidu_time_unittest.cpp
test/baidu_time_unittest.cpp
+1
-7
No files found.
src/bthread/fd.cpp
View file @
4793dfa1
...
...
@@ -450,14 +450,8 @@ int pthread_fd_wait(int fd, unsigned events,
int
diff_ms
=
-
1
;
if
(
abstime
)
{
timespec
now
;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t
cclock
;
mach_timespec_t
mts
;
host_get_clock_service
(
mach_host_self
(),
CALENDAR_CLOCK
,
&
cclock
);
clock_get_time
(
cclock
,
&
mts
);
mach_port_deallocate
(
mach_task_self
(),
cclock
);
now
.
tv_sec
=
mts
.
tv_sec
;
now
.
tv_nsec
=
mts
.
tv_nsec
;
#ifdef __MACH__
clock_gettime
(
CALENDAR_CLOCK
,
&
now
);
#else
clock_gettime
(
CLOCK_REALTIME
,
&
now
);
#endif
...
...
src/butil/time.cpp
View file @
4793dfa1
...
...
@@ -36,16 +36,9 @@ int64_t monotonic_time_ns() {
// NOTE: Not inline to keep ABI-compatible with previous versions.
timespec
now
;
#ifdef __MACH__
// OS X does not have clock_gettime, use clock_get_time.
// The value returned is a monotonically increasing value according to
// https://opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/man/clock_get_time.html
clock_serv_t
cclock
;
mach_timespec_t
mts
;
host_get_clock_service
(
mach_host_self
(),
CALENDAR_CLOCK
,
&
cclock
);
clock_get_time
(
cclock
,
&
mts
);
mach_port_deallocate
(
mach_task_self
(),
cclock
);
now
.
tv_sec
=
mts
.
tv_sec
;
now
.
tv_nsec
=
mts
.
tv_nsec
;
clock_gettime
(
CALENDAR_CLOCK
,
&
now
);
#else
clock_gettime
(
CLOCK_MONOTONIC
,
&
now
);
#endif
...
...
src/butil/time.h
View file @
4793dfa1
...
...
@@ -20,14 +20,25 @@
#ifndef BUTIL_BAIDU_TIME_H
#define BUTIL_BAIDU_TIME_H
#include <time.h> // timespec, clock_gettime
#include <sys/time.h> // timeval, gettimeofday
#include <stdint.h> // int64_t, uint64_t
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
#include <time.h> // timespec, clock_gettime
#include <sys/time.h> // timeval, gettimeofday
#include <stdint.h> // int64_t, uint64_t
inline
void
clock_gettime
(
clock_id_t
id
,
timespec
*
time
)
{
// clock_gettime is not available in MacOS, use clock_get_time instead
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
;
}
#endif
namespace
butil
{
...
...
@@ -92,14 +103,8 @@ inline timespec seconds_from(timespec start_time, int64_t seconds) {
// --------------------------------------------------------------------
inline
timespec
nanoseconds_from_now
(
int64_t
nanoseconds
)
{
timespec
time
;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t
cclock
;
mach_timespec_t
mts
;
host_get_clock_service
(
mach_host_self
(),
CALENDAR_CLOCK
,
&
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
;
#ifdef __MACH__
clock_gettime
(
CALENDAR_CLOCK
,
&
time
);
#else
clock_gettime
(
CLOCK_REALTIME
,
&
time
);
#endif
...
...
@@ -120,14 +125,8 @@ inline timespec seconds_from_now(int64_t seconds) {
inline
timespec
timespec_from_now
(
const
timespec
&
span
)
{
timespec
time
;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t
cclock
;
mach_timespec_t
mts
;
host_get_clock_service
(
mach_host_self
(),
CALENDAR_CLOCK
,
&
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
;
#ifdef __MACH__
clock_gettime
(
CALENDAR_CLOCK
,
&
time
);
#else
clock_gettime
(
CLOCK_REALTIME
,
&
time
);
#endif
...
...
test/baidu_time_unittest.cpp
View file @
4793dfa1
...
...
@@ -20,13 +20,7 @@ TEST(BaiduTimeTest, diff_between_gettimeofday_and_REALTIME) {
long
t1
=
butil
::
gettimeofday_us
();
timespec
time
;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t
cclock
;
mach_timespec_t
mts
;
host_get_clock_service
(
mach_host_self
(),
CALENDAR_CLOCK
,
&
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
;
clock_gettime
(
CALENDAR_CLOCK
,
&
time
);
#else
clock_gettime
(
CLOCK_REALTIME
,
&
time
);
#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