Skip to content
Snippets Groups Projects
Commit af39565c authored by Craig Tiller's avatar Craig Tiller
Browse files

Merge pull request #385 from dklempner/timespec

Make gpr_timespec no longer be a typedef for struct timespec in posix
parents 5de37830 0c61dc52
No related branches found
No related tags found
No related merge requests found
...@@ -34,31 +34,22 @@ ...@@ -34,31 +34,22 @@
#ifndef __GRPC_SUPPORT_TIME_H__ #ifndef __GRPC_SUPPORT_TIME_H__
#define __GRPC_SUPPORT_TIME_H__ #define __GRPC_SUPPORT_TIME_H__
/* Time support. /* Time support.
We use gpr_timespec, which is typedefed to struct timespec on platforms which We use gpr_timespec, which is analogous to struct timespec. On some
have it. On some machines, absolute times may be in local time. */ machines, absolute times may be in local time. */
/* Platform specific header declares gpr_timespec.
gpr_timespec contains:
time_t tv_sec; // seconds since start of 1970
int tv_nsec; // nanoseconds; always in 0..999999999; never negative.
*/
#include <grpc/support/port_platform.h> #include <grpc/support/port_platform.h>
#if defined(GPR_POSIX_TIME)
#include <grpc/support/time_posix.h>
#elif defined(GPR_WIN32)
#include <grpc/support/time_win32.h>
#else
#error could not determine platform for time
#endif
#include <stddef.h> #include <stddef.h>
#include <time.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
typedef struct gpr_timespec {
time_t tv_sec;
int tv_nsec;
} gpr_timespec;
/* Time constants. */ /* Time constants. */
extern const gpr_timespec gpr_time_0; /* The zero time interval. */ extern const gpr_timespec gpr_time_0; /* The zero time interval. */
extern const gpr_timespec gpr_inf_future; /* The far future */ extern const gpr_timespec gpr_inf_future; /* The far future */
......
/*
*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef __GRPC_SUPPORT_TIME_POSIX_H__
#define __GRPC_SUPPORT_TIME_POSIX_H__
/* Posix variant of gpr_time_platform.h */
#include <sys/time.h>
#include <time.h>
typedef struct timespec gpr_timespec;
#endif /* __GRPC_SUPPORT_TIME_POSIX_H__ */
/*
*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef __GRPC_SUPPORT_TIME_WIN32_H__
#define __GRPC_SUPPORT_TIME_WIN32_H__
/* Win32 variant of gpr_time_platform.h */
#include <Winsock.h>
#include <time.h>
typedef struct gpr_timespec {
time_t tv_sec;
long tv_nsec;
} gpr_timespec;
#endif /* __GRPC_SUPPORT_TIME_WIN32_H__ */
...@@ -33,11 +33,17 @@ ...@@ -33,11 +33,17 @@
/* Posix gpr synchroization support code. */ /* Posix gpr synchroization support code. */
#if !defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 199309L
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199309L
#endif
#include <grpc/support/port_platform.h> #include <grpc/support/port_platform.h>
#ifdef GPR_POSIX_SYNC #ifdef GPR_POSIX_SYNC
#include <errno.h> #include <errno.h>
#include <time.h>
#include <grpc/support/log.h> #include <grpc/support/log.h>
#include <grpc/support/sync.h> #include <grpc/support/sync.h>
#include <grpc/support/time.h> #include <grpc/support/time.h>
...@@ -67,7 +73,10 @@ int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline) { ...@@ -67,7 +73,10 @@ int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline) {
if (gpr_time_cmp(abs_deadline, gpr_inf_future) == 0) { if (gpr_time_cmp(abs_deadline, gpr_inf_future) == 0) {
err = pthread_cond_wait(cv, mu); err = pthread_cond_wait(cv, mu);
} else { } else {
err = pthread_cond_timedwait(cv, mu, &abs_deadline); struct timespec abs_deadline_ts;
abs_deadline_ts.tv_sec = abs_deadline.tv_sec;
abs_deadline_ts.tv_nsec = abs_deadline.tv_nsec;
err = pthread_cond_timedwait(cv, mu, &abs_deadline_ts);
} }
GPR_ASSERT(err == 0 || err == ETIMEDOUT || err == EAGAIN); GPR_ASSERT(err == 0 || err == ETIMEDOUT || err == EAGAIN);
return err == ETIMEDOUT; return err == ETIMEDOUT;
......
...@@ -34,7 +34,8 @@ ...@@ -34,7 +34,8 @@
/* Posix code for gpr time support. */ /* Posix code for gpr time support. */
/* So we get nanosleep and clock_* */ /* So we get nanosleep and clock_* */
#ifndef _POSIX_C_SOURCE #if !defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 199309L
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199309L #define _POSIX_C_SOURCE 199309L
#endif #endif
...@@ -47,11 +48,25 @@ ...@@ -47,11 +48,25 @@
#include <unistd.h> #include <unistd.h>
#include <grpc/support/time.h> #include <grpc/support/time.h>
static struct timespec timespec_from_gpr(gpr_timespec gts) {
struct timespec rv;
rv.tv_sec = gts.tv_sec;
rv.tv_nsec = gts.tv_nsec;
return rv;
}
#if _POSIX_TIMERS > 0 #if _POSIX_TIMERS > 0
static gpr_timespec gpr_from_timespec(struct timespec ts) {
gpr_timespec rv;
rv.tv_sec = ts.tv_sec;
rv.tv_nsec = ts.tv_nsec;
return rv;
}
gpr_timespec gpr_now(void) { gpr_timespec gpr_now(void) {
gpr_timespec now; struct timespec now;
clock_gettime(CLOCK_REALTIME, &now); clock_gettime(CLOCK_REALTIME, &now);
return now; return gpr_from_timespec(now);
} }
#else #else
/* For some reason Apple's OSes haven't implemented clock_gettime. */ /* For some reason Apple's OSes haven't implemented clock_gettime. */
...@@ -69,6 +84,7 @@ gpr_timespec gpr_now(void) { ...@@ -69,6 +84,7 @@ gpr_timespec gpr_now(void) {
void gpr_sleep_until(gpr_timespec until) { void gpr_sleep_until(gpr_timespec until) {
gpr_timespec now; gpr_timespec now;
gpr_timespec delta; gpr_timespec delta;
struct timespec delta_ts;
for (;;) { for (;;) {
/* We could simplify by using clock_nanosleep instead, but it might be /* We could simplify by using clock_nanosleep instead, but it might be
...@@ -79,7 +95,8 @@ void gpr_sleep_until(gpr_timespec until) { ...@@ -79,7 +95,8 @@ void gpr_sleep_until(gpr_timespec until) {
} }
delta = gpr_time_sub(until, now); delta = gpr_time_sub(until, now);
if (nanosleep(&delta, NULL) == 0) { delta_ts = timespec_from_gpr(delta);
if (nanosleep(&delta_ts, NULL) == 0) {
break; break;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment