1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-23 04:04:54 +08:00

Switch get_time_now to return timespec, rather than operate on reference.

This commit is contained in:
Mike Purvis 2013-10-06 10:49:25 -04:00
parent 7a61d4545b
commit 4b23d52078
2 changed files with 11 additions and 11 deletions

View File

@ -38,20 +38,22 @@
/*! Smooth over platform variances in getting an accurate timespec /*! Smooth over platform variances in getting an accurate timespec
* representing the present moment. */ * representing the present moment. */
static inline void static inline struct timespec
get_time_now (struct timespec &time) get_time_now ()
{ {
struct timespec ts;
# ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time # ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock; clock_serv_t cclock;
mach_timespec_t mts; mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts); clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock); mach_port_deallocate(mach_task_self(), cclock);
time.tv_sec = mts.tv_sec; ts.tv_sec = mts.tv_sec;
time.tv_nsec = mts.tv_nsec; ts.tv_nsec = mts.tv_nsec;
# else # else
clock_gettime(CLOCK_REALTIME, &time); clock_gettime(CLOCK_REALTIME, &ts);
# endif # endif
return ts;
} }
/*! Simple function to normalize the tv_nsec field to [0..1e9), carrying /*! Simple function to normalize the tv_nsec field to [0..1e9), carrying

View File

@ -438,12 +438,11 @@ Serial::SerialImpl::read (uint8_t *buf, size_t size)
FD_ZERO (&readfds); FD_ZERO (&readfds);
FD_SET (fd_, &readfds); FD_SET (fd_, &readfds);
// Begin timing select // Begin timing select
struct timespec start, end; struct timespec start(get_time_now());
get_time_now (start);
// Call select to block for serial data or a timeout // Call select to block for serial data or a timeout
int r = select (fd_ + 1, &readfds, NULL, NULL, &timeout); int r = select (fd_ + 1, &readfds, NULL, NULL, &timeout);
// Calculate difference and update the structure // Calculate difference and update the structure
get_time_now (end); struct timespec end(get_time_now());
// Calculate the time select took // Calculate the time select took
struct timespec diff(end - start); struct timespec diff(end - start);
@ -535,14 +534,13 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length)
// does not occur. // does not occur.
#if !defined(__linux__) #if !defined(__linux__)
// Begin timing select // Begin timing select
struct timespec start, end; struct timespec start(get_time_now());
get_time_now(start);
#endif #endif
// Do the select // Do the select
int r = select (fd_ + 1, NULL, &writefds, NULL, &timeout); int r = select (fd_ + 1, NULL, &writefds, NULL, &timeout);
#if !defined(__linux__) #if !defined(__linux__)
// Calculate difference and update the structure // Calculate difference and update the structure
get_time_now(end); struct timespec end(get_time_now());
// Calculate the time select took // Calculate the time select took
struct timespec diff(end - start); struct timespec diff(end - start);
// Update the timeout // Update the timeout