mirror of
https://github.com/wjwwood/serial.git
synced 2026-01-22 19:54:57 +08:00
Switch get_time_now to return timespec, rather than operate on reference.
This commit is contained in:
parent
7a61d4545b
commit
4b23d52078
@ -38,20 +38,22 @@
|
||||
|
||||
/*! Smooth over platform variances in getting an accurate timespec
|
||||
* representing the present moment. */
|
||||
static inline void
|
||||
get_time_now (struct timespec &time)
|
||||
static inline struct timespec
|
||||
get_time_now ()
|
||||
{
|
||||
struct timespec ts;
|
||||
# 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;
|
||||
ts.tv_sec = mts.tv_sec;
|
||||
ts.tv_nsec = mts.tv_nsec;
|
||||
# else
|
||||
clock_gettime(CLOCK_REALTIME, &time);
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
# endif
|
||||
return ts;
|
||||
}
|
||||
|
||||
/*! Simple function to normalize the tv_nsec field to [0..1e9), carrying
|
||||
|
||||
@ -438,12 +438,11 @@ Serial::SerialImpl::read (uint8_t *buf, size_t size)
|
||||
FD_ZERO (&readfds);
|
||||
FD_SET (fd_, &readfds);
|
||||
// Begin timing select
|
||||
struct timespec start, end;
|
||||
get_time_now (start);
|
||||
struct timespec start(get_time_now());
|
||||
// Call select to block for serial data or a timeout
|
||||
int r = select (fd_ + 1, &readfds, NULL, NULL, &timeout);
|
||||
// Calculate difference and update the structure
|
||||
get_time_now (end);
|
||||
struct timespec end(get_time_now());
|
||||
// Calculate the time select took
|
||||
struct timespec diff(end - start);
|
||||
|
||||
@ -535,14 +534,13 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length)
|
||||
// does not occur.
|
||||
#if !defined(__linux__)
|
||||
// Begin timing select
|
||||
struct timespec start, end;
|
||||
get_time_now(start);
|
||||
struct timespec start(get_time_now());
|
||||
#endif
|
||||
// Do the select
|
||||
int r = select (fd_ + 1, NULL, &writefds, NULL, &timeout);
|
||||
#if !defined(__linux__)
|
||||
// Calculate difference and update the structure
|
||||
get_time_now(end);
|
||||
struct timespec end(get_time_now());
|
||||
// Calculate the time select took
|
||||
struct timespec diff(end - start);
|
||||
// Update the timeout
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user