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

Delta Between Two Patch Sets: Include/pytime.h

Issue 22043: Use a monotonic clock to compute timeouts
Left Patch Set: Created 3 years, 4 months ago
Right Patch Set: Created 3 years, 2 months 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 /* Timestamp with nanosecond resolution */ 16 #ifndef Py_LIMITED_API
17
18 #ifdef HAVE_GETTIMEOFDAY
19 typedef struct timeval _PyTime_timeval;
20 #else
17 typedef struct { 21 typedef struct {
18 time_t tv_sec; /* seconds */ 22 time_t tv_sec; /* seconds since Jan. 1, 1970 */
19 long tv_nsec; /* nanoseconds */ 23 long tv_usec; /* and microseconds */
20 } _PyTime_timespec; 24 } _PyTime_timeval;
25 #endif
21 26
22 /* Structure used by time.get_clock_info() */ 27 /* Structure used by time.get_clock_info() */
23 typedef struct { 28 typedef struct {
24 const char *implementation; 29 const char *implementation;
25 int monotonic; 30 int monotonic;
26 int adjustable; 31 int adjustable;
27 double resolution; 32 double resolution;
28 } _Py_clock_info_t; 33 } _Py_clock_info_t;
29 34
30 /* Similar to POSIX gettimeofday but cannot fail. If system gettimeofday 35 /* Similar to POSIX gettimeofday but cannot fail. If system gettimeofday
31 * fails or is not available, fall back to lower resolution clocks. 36 * fails or is not available, fall back to lower resolution clocks.
32 */ 37 */
33 PyAPI_FUNC(void) _PyTime_gettimeofday(_PyTime_timespec *tp); 38 PyAPI_FUNC(void) _PyTime_gettimeofday(_PyTime_timeval *tp);
34 39
35 /* Similar to _PyTime_gettimeofday() but retrieve also information on the 40 /* Similar to _PyTime_gettimeofday() but retrieve also information on the
36 * clock used to get the current time. */ 41 * clock used to get the current time. */
37 PyAPI_FUNC(void) _PyTime_gettimeofday_info( 42 PyAPI_FUNC(int) _PyTime_gettimeofday_info(
38 _PyTime_timespec *tp, 43 _PyTime_timeval *tp,
39 _Py_clock_info_t *info); 44 _Py_clock_info_t *info);
40 45
41 #define _PyTime_ADD_SECONDS(tv, interval) \ 46 #define _PyTime_ADD_SECONDS(tv, interval) \
42 do { \ 47 do { \
43 tv.tv_nsec += (long) (((long) interval - interval) * 1000000000); \ 48 tv.tv_usec += (long) (((long) interval - interval) * 1000000); \
44 tv.tv_sec += (time_t) interval + (time_t) (tv.tv_nsec / 1000000000); \ 49 tv.tv_sec += (time_t) interval + (time_t) (tv.tv_usec / 1000000); \
45 tv.tv_nsec %= 1000000000; \ 50 tv.tv_usec %= 1000000; \
46 } while (0) 51 } while (0)
47 52
48 #define _PyTime_INTERVAL(tv_start, tv_end) \ 53 #define _PyTime_INTERVAL(tv_start, tv_end) \
49 ((tv_end.tv_sec - tv_start.tv_sec) + \ 54 ((tv_end.tv_sec - tv_start.tv_sec) + \
50 (tv_end.tv_nsec - tv_start.tv_nsec) * 1e-9) 55 (tv_end.tv_usec - tv_start.tv_usec) * 0.000001)
51
52 #ifndef Py_LIMITED_API
53 56
54 typedef enum { 57 typedef enum {
55 /* Round towards zero. */ 58 /* Round towards zero. */
56 _PyTime_ROUND_DOWN=0, 59 _PyTime_ROUND_DOWN=0,
57 /* Round away from zero. */ 60 /* Round away from zero. */
58 _PyTime_ROUND_UP 61 _PyTime_ROUND_UP
59 } _PyTime_round_t; 62 } _PyTime_round_t;
60 63
61 /* Convert a number of seconds, int or float, to time_t. */ 64 /* Convert a number of seconds, int or float, to time_t. */
62 PyAPI_FUNC(int) _PyTime_ObjectToTime_t( 65 PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
(...skipping 20 matching lines...) Expand all
83 86
84 /* 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.
85 nsec is in the range [0; 999999999] and rounded towards zero. 88 nsec is in the range [0; 999999999] and rounded towards zero.
86 For example, -1.2 is converted to (-2, 800000000). */ 89 For example, -1.2 is converted to (-2, 800000000). */
87 PyAPI_FUNC(int) _PyTime_ObjectToTimespec( 90 PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
88 PyObject *obj, 91 PyObject *obj,
89 time_t *sec, 92 time_t *sec,
90 long *nsec, 93 long *nsec,
91 _PyTime_round_t); 94 _PyTime_round_t);
92 95
93 /* Return the value (in fractional seconds) of a monotonic clock, i.e. a clock 96 /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
94 that cannot go backwards. The clock is not affected by system clock updates. 97 The clock is not affected by system clock updates. The reference point of
95 The reference point of the returned value is undefined, so that only the 98 the returned value is undefined, so that only the difference between the
96 difference between the results of consecutive calls is valid. */ 99 results of consecutive calls is valid.
100
101 The function never fails. _PyTime_Init() ensures that a monotonic clock
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. */
97 PyAPI_FUNC(int) _PyTime_monotonic_info( 110 PyAPI_FUNC(int) _PyTime_monotonic_info(
98 _PyTime_timespec *ts, 111 _PyTime_timeval *tp,
99 _Py_clock_info_t *info); 112 _Py_clock_info_t *info);
100 113
101 /* Similar to _PyTime_monotonic_info() but it cannot fail: set ts->tv_sec and 114 /* Initialize time.
102 tv->tv_nsec to zero instead of raising an exception. */ 115 Return 0 on success, raise an exception and return -1 on error. */
103 PyAPI_FUNC(void) _PyTime_monotonic( 116 PyAPI_FUNC(int) _PyTime_Init(void);
104 _PyTime_timespec *ts); 117 #endif /* Py_LIMITED_API */
105
106 #endif
107
108 /* Dummy to force linking. */
109 PyAPI_FUNC(void) _PyTime_Init(void);
110 118
111 #ifdef __cplusplus 119 #ifdef __cplusplus
112 } 120 }
113 #endif 121 #endif
114 122
115 #endif /* Py_PYTIME_H */ 123 #endif /* Py_PYTIME_H */
116 #endif /* Py_LIMITED_API */ 124 #endif /* Py_LIMITED_API */
LEFTRIGHT

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