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

Side by Side Diff: Include/pytime.h

Issue 22043: Use a monotonic clock to compute timeouts
Patch Set: Created 5 years, 3 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:
View unified diff | Download patch
« no previous file with comments | « no previous file | Modules/_datetimemodule.c » ('j') | Modules/timemodule.c » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #ifdef HAVE_GETTIMEOFDAY 16 #ifndef Py_LIMITED_API
17 typedef struct timeval _PyTime_timeval; 17
18 #else 18 /* Timestamp with nanosecond resolution */
19 typedef struct { 19 typedef struct {
20 time_t tv_sec; /* seconds since Jan. 1, 1970 */ 20 time_t tv_sec; /* seconds */
21 long tv_usec; /* and microseconds */ 21 long tv_nsec; /* nanoseconds */
22 } _PyTime_timeval; 22 } _PyTimeSpec;
23 #endif
24 23
25 /* Structure used by time.get_clock_info() */ 24 /* Structure used by time.get_clock_info() */
26 typedef struct { 25 typedef struct {
27 const char *implementation; 26 const char *implementation;
28 int monotonic; 27 int monotonic;
29 int adjustable; 28 int adjustable;
30 double resolution; 29 double resolution;
31 } _Py_clock_info_t; 30 } _Py_clock_info_t;
32 31
33 /* Similar to POSIX gettimeofday but cannot fail. If system gettimeofday 32 /* Get the system clock. The function never fails.
34 * fails or is not available, fall back to lower resolution clocks. 33 _PyTimeSpec_Init() ensures that the system clock works. */
35 */ 34 PyAPI_FUNC(void) _PyTimeSpec_get_time(_PyTimeSpec *tp);
36 PyAPI_FUNC(void) _PyTime_gettimeofday(_PyTime_timeval *tp);
37 35
38 /* Similar to _PyTime_gettimeofday() but retrieve also information on the 36 /* Get the system clock: fill into (if set) with information of the
39 * clock used to get the current time. */ 37 function used to get the system block.
40 PyAPI_FUNC(void) _PyTime_gettimeofday_info( 38 Return 0 on success, raise an exceptio and return -1 on error. */
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
41 _PyTime_timeval *tp, 39 PyAPI_FUNC(int) _PyTimeSpec_get_time_info(
40 _PyTimeSpec *tp,
42 _Py_clock_info_t *info); 41 _Py_clock_info_t *info);
43 42
44 #define _PyTime_ADD_SECONDS(tv, interval) \ 43 /* Add seconds to a _PyTimeSpec timestamp.
45 do { \ 44 Return 0 on success, return -1 on overflow. */
46 tv.tv_usec += (long) (((long) interval - interval) * 1000000); \ 45 PyAPI_FUNC(int) _PyTimeSpec_add_sec(_PyTimeSpec *tv, double secs);
47 tv.tv_sec += (time_t) interval + (time_t) (tv.tv_usec / 1000000); \
48 tv.tv_usec %= 1000000; \
49 } while (0)
50 46
51 #define _PyTime_INTERVAL(tv_start, tv_end) \ 47 #if defined(HAVE_LONG_LONG)
52 ((tv_end.tv_sec - tv_start.tv_sec) + \ 48 typedef unsigned PY_LONG_LONG _PyTime_unit_t;
53 (tv_end.tv_usec - tv_start.tv_usec) * 0.000001) 49 #define _PyTime_UNIT_MAX PY_ULLONG_MAX
50 #else
51 typedef unsigned long _PyTime_unit_t;
52 #define _PyTime_UNIT_MAX ULONG_MAX
53 #endif
54 54
55 #ifndef Py_LIMITED_API 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);
56 72
57 typedef enum { 73 typedef enum {
58 /* Round towards zero. */ 74 /* Round towards zero. */
59 _PyTime_ROUND_DOWN=0, 75 _PyTime_ROUND_DOWN=0,
60 /* Round away from zero. */ 76 /* Round away from zero. */
61 _PyTime_ROUND_UP 77 _PyTime_ROUND_UP
62 } _PyTime_round_t; 78 } _PyTime_round_t;
63 79
64 /* Convert a number of seconds, int or float, to time_t. */ 80 /* Convert a number of seconds, int or float, to time_t. */
65 PyAPI_FUNC(int) _PyTime_ObjectToTime_t( 81 PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
(...skipping 19 matching lines...) Expand all
85 _PyTime_round_t); 101 _PyTime_round_t);
86 102
87 /* Convert a number of seconds, int or float, to a timespec structure. 103 /* Convert a number of seconds, int or float, to a timespec structure.
88 nsec is in the range [0; 999999999] and rounded towards zero. 104 nsec is in the range [0; 999999999] and rounded towards zero.
89 For example, -1.2 is converted to (-2, 800000000). */ 105 For example, -1.2 is converted to (-2, 800000000). */
90 PyAPI_FUNC(int) _PyTime_ObjectToTimespec( 106 PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
91 PyObject *obj, 107 PyObject *obj,
92 time_t *sec, 108 time_t *sec,
93 long *nsec, 109 long *nsec,
94 _PyTime_round_t); 110 _PyTime_round_t);
111
95 #endif 112 #endif
96 113
97 /* Dummy to force linking. */ 114 /* Dummy to force linking. */
98 PyAPI_FUNC(void) _PyTime_Init(void); 115 PyAPI_FUNC(int) _PyTimeSpec_Init(void);
99 116
100 #ifdef __cplusplus 117 #ifdef __cplusplus
101 } 118 }
102 #endif 119 #endif
103 120
104 #endif /* Py_PYTIME_H */ 121 #endif /* Py_PYTIME_H */
105 #endif /* Py_LIMITED_API */ 122 #endif /* Py_LIMITED_API */
OLDNEW
« no previous file with comments | « no previous file | Modules/_datetimemodule.c » ('j') | Modules/timemodule.c » ('J')

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