Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1)

Delta Between Two Patch Sets: Include/pytime.h

Issue 22043: Use a monotonic clock to compute timeouts
Left Patch Set: Created 3 years, 1 month ago
Right Patch Set: Created 3 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Doc/library/time.rst ('k') | Lib/queue.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #ifndef Py_LIMITED_API 1 #ifndef Py_LIMITED_API
2 #ifndef Py_PYTIME_H 2 #ifndef Py_PYTIME_H
3 #define Py_PYTIME_H 3 #define Py_PYTIME_H
4 4
5 #include "pyconfig.h" /* include for defines */ 5 #include "pyconfig.h" /* include for defines */
6 #include "object.h" 6 #include "object.h"
7 7
8 /************************************************************************** 8 /**************************************************************************
9 Symbols and macros to supply platform-independent interfaces to time related 9 Symbols and macros to supply platform-independent interfaces to time related
10 functions and constants 10 functions and constants
11 **************************************************************************/ 11 **************************************************************************/
12 #ifdef __cplusplus 12 #ifdef __cplusplus
13 extern "C" { 13 extern "C" {
14 #endif 14 #endif
15 15
16 #ifndef Py_LIMITED_API 16 #ifndef Py_LIMITED_API
17 17
18 /* Timestamp with nanosecond resolution */ 18 #ifdef HAVE_GETTIMEOFDAY
19 typedef struct timeval _PyTime_timeval;
20 #else
19 typedef struct { 21 typedef struct {
20 time_t tv_sec; /* seconds */ 22 time_t tv_sec; /* seconds since Jan. 1, 1970 */
21 long tv_nsec; /* nanoseconds */ 23 long tv_usec; /* and microseconds */
22 } _PyTimeSpec; 24 } _PyTime_timeval;
25 #endif
23 26
24 /* Structure used by time.get_clock_info() */ 27 /* Structure used by time.get_clock_info() */
25 typedef struct { 28 typedef struct {
26 const char *implementation; 29 const char *implementation;
27 int monotonic; 30 int monotonic;
28 int adjustable; 31 int adjustable;
29 double resolution; 32 double resolution;
30 } _Py_clock_info_t; 33 } _Py_clock_info_t;
31 34
32 /* Get the system clock. The function never fails. 35 /* Similar to POSIX gettimeofday but cannot fail. If system gettimeofday
33 _PyTimeSpec_Init() ensures that the system clock works. */ 36 * fails or is not available, fall back to lower resolution clocks.
34 PyAPI_FUNC(void) _PyTimeSpec_get_time(_PyTimeSpec *tp); 37 */
38 PyAPI_FUNC(void) _PyTime_gettimeofday(_PyTime_timeval *tp);
35 39
36 /* Get the system clock: fill into (if set) with information of the 40 /* Similar to _PyTime_gettimeofday() but retrieve also information on the
37 function used to get the system block. 41 * clock used to get the current time. */
38 Return 0 on success, raise an exceptio and return -1 on error. */ 42 PyAPI_FUNC(int) _PyTime_gettimeofday_info(
Charles-François Natali 2014/07/31 19:50:26 typo: "exceptio" How could _PyTimeSpec_get_time_i
haypo 2014/08/01 01:44:20 I created a new issue #22117 for this patch becaus
39 PyAPI_FUNC(int) _PyTimeSpec_get_time_info( 43 _PyTime_timeval *tp,
40 _PyTimeSpec *tp,
41 _Py_clock_info_t *info); 44 _Py_clock_info_t *info);
42 45
43 /* Add seconds to a _PyTimeSpec timestamp. 46 #define _PyTime_ADD_SECONDS(tv, interval) \
44 Return 0 on success, return -1 on overflow. */ 47 do { \
45 PyAPI_FUNC(int) _PyTimeSpec_add_sec(_PyTimeSpec *tv, double secs); 48 tv.tv_usec += (long) (((long) interval - interval) * 1000000); \
49 tv.tv_sec += (time_t) interval + (time_t) (tv.tv_usec / 1000000); \
50 tv.tv_usec %= 1000000; \
51 } while (0)
46 52
47 #if defined(HAVE_LONG_LONG) 53 #define _PyTime_INTERVAL(tv_start, tv_end) \
48 typedef unsigned PY_LONG_LONG _PyTime_unit_t; 54 ((tv_end.tv_sec - tv_start.tv_sec) + \
49 #define _PyTime_UNIT_MAX PY_ULLONG_MAX 55 (tv_end.tv_usec - tv_start.tv_usec) * 0.000001)
50 #else
51 typedef unsigned long _PyTime_unit_t;
52 #define _PyTime_UNIT_MAX ULONG_MAX
53 #endif
54
55 /* Add microseconds to a _PyTimeSpec timestamp.
56 Ensure that ts->ts_nsec is in range [0; 999999999].
57 Return 0 on success, -1 on overflow. */
58 PyAPI_FUNC(int) _PyTimeSpec_add_us(
59 _PyTimeSpec *ts,
60 _PyTime_unit_t microseconds);
61
62 /* Compute the different in seconds between two _PyTimeSpec timestamps */
63 PyAPI_FUNC(double) _PyTimeSpec_interval_sec(
64 _PyTimeSpec *start,
65 _PyTimeSpec *end);
66
67 /* Compute the different in microseconds between two _PyTimeSpec timestamps.
Charles-François Natali 2014/07/31 19:50:26 Why two difference computation functions? Why ret
haypo 2014/08/01 01:44:20 Thanks for your comment. It's now fixed in my new
68 Return (_PyTime_unit_t)-1 on overflow. */
69 PyAPI_FUNC(_PyTime_unit_t) _PyTimeSpec_interval_us(
70 _PyTimeSpec *start,
71 _PyTimeSpec *end);
72 56
73 typedef enum { 57 typedef enum {
74 /* Round towards zero. */ 58 /* Round towards zero. */
75 _PyTime_ROUND_DOWN=0, 59 _PyTime_ROUND_DOWN=0,
76 /* Round away from zero. */ 60 /* Round away from zero. */
77 _PyTime_ROUND_UP 61 _PyTime_ROUND_UP
78 } _PyTime_round_t; 62 } _PyTime_round_t;
79 63
80 /* Convert a number of seconds, int or float, to time_t. */ 64 /* Convert a number of seconds, int or float, to time_t. */
81 PyAPI_FUNC(int) _PyTime_ObjectToTime_t( 65 PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
(...skipping 20 matching lines...) Expand all
102 86
103 /* Convert a number of seconds, int or float, to a timespec structure. 87 /* Convert a number of seconds, int or float, to a timespec structure.
104 nsec is in the range [0; 999999999] and rounded towards zero. 88 nsec is in the range [0; 999999999] and rounded towards zero.
105 For example, -1.2 is converted to (-2, 800000000). */ 89 For example, -1.2 is converted to (-2, 800000000). */
106 PyAPI_FUNC(int) _PyTime_ObjectToTimespec( 90 PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
107 PyObject *obj, 91 PyObject *obj,
108 time_t *sec, 92 time_t *sec,
109 long *nsec, 93 long *nsec,
110 _PyTime_round_t); 94 _PyTime_round_t);
111 95
112 #endif 96 /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
97 The clock is not affected by system clock updates. The reference point of
98 the returned value is undefined, so that only the difference between the
99 results of consecutive calls is valid.
113 100
114 /* Dummy to force linking. */ 101 The function never fails. _PyTime_Init() ensures that a monotonic clock
115 PyAPI_FUNC(int) _PyTimeSpec_Init(void); 102 is available and works. */
103 PyAPI_FUNC(void) _PyTime_monotonic(
104 _PyTime_timeval *tp);
105
106 /* Similar to _PyTime_monotonic(), fill also info (if set) with information of
107 the function used to get the time.
108
109 Return 0 on success, raise an exception and return -1 on error. */
110 PyAPI_FUNC(int) _PyTime_monotonic_info(
111 _PyTime_timeval *tp,
112 _Py_clock_info_t *info);
113
114 /* Initialize time.
115 Return 0 on success, raise an exception and return -1 on error. */
116 PyAPI_FUNC(int) _PyTime_Init(void);
117 #endif /* Py_LIMITED_API */
116 118
117 #ifdef __cplusplus 119 #ifdef __cplusplus
118 } 120 }
119 #endif 121 #endif
120 122
121 #endif /* Py_PYTIME_H */ 123 #endif /* Py_PYTIME_H */
122 #endif /* Py_LIMITED_API */ 124 #endif /* Py_LIMITED_API */
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7