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

Side by Side Diff: Modules/timemodule.c

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
OLDNEW
1 /* Time module */ 1 /* Time module */
2 2
3 #include "Python.h" 3 #include "Python.h"
4 4
5 #include <ctype.h> 5 #include <ctype.h>
6 6
7 #ifdef HAVE_SYS_TIMES_H 7 #ifdef HAVE_SYS_TIMES_H
8 #include <sys/times.h> 8 #include <sys/times.h>
9 #endif 9 #endif
10 10
(...skipping 1516 matching lines...) Expand 10 before | Expand all | Expand 10 after
1527 PyModule_AddIntConstant(m, "_STRUCT_TM_ITEMS", 9); 1527 PyModule_AddIntConstant(m, "_STRUCT_TM_ITEMS", 9);
1528 #endif 1528 #endif
1529 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType); 1529 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
1530 initialized = 1; 1530 initialized = 1;
1531 return m; 1531 return m;
1532 } 1532 }
1533 1533
1534 static PyObject* 1534 static PyObject*
1535 floattime(_Py_clock_info_t *info) 1535 floattime(_Py_clock_info_t *info)
1536 { 1536 {
1537 _PyTime_timeval t; 1537 _PyTimeSpec ts;
1538 #ifdef HAVE_CLOCK_GETTIME 1538 #ifdef HAVE_CLOCK_GETTIME
1539 struct timespec tp; 1539 struct timespec tp;
1540 int ret; 1540 int ret;
1541 1541
1542 /* _PyTime_gettimeofday() does not use clock_gettime() 1542 /* _PyTime_gettimeofday() does not use clock_gettime()
1543 because it would require to link Python to the rt (real-time) 1543 because it would require to link Python to the rt (real-time)
1544 library, at least on Linux */ 1544 library, at least on Linux */
1545 ret = clock_gettime(CLOCK_REALTIME, &tp); 1545 ret = clock_gettime(CLOCK_REALTIME, &tp);
1546 if (ret == 0) { 1546 if (ret == 0) {
1547 if (info) { 1547 if (info) {
1548 struct timespec res; 1548 struct timespec res;
1549 info->implementation = "clock_gettime(CLOCK_REALTIME)"; 1549 info->implementation = "clock_gettime(CLOCK_REALTIME)";
1550 info->monotonic = 0; 1550 info->monotonic = 0;
1551 info->adjustable = 1; 1551 info->adjustable = 1;
1552 if (clock_getres(CLOCK_REALTIME, &res) == 0) 1552 if (clock_getres(CLOCK_REALTIME, &res) == 0)
1553 info->resolution = res.tv_sec + res.tv_nsec * 1e-9; 1553 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
1554 else 1554 else
1555 info->resolution = 1e-9; 1555 info->resolution = 1e-9;
1556 } 1556 }
1557 return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); 1557 return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
1558 } 1558 }
1559 #endif 1559 #endif
1560 _PyTime_gettimeofday_info(&t, info); 1560 if (_PyTimeSpec_get_time_info(&ts, info) < 0) {
1561 return PyFloat_FromDouble((double)t.tv_sec + t.tv_usec * 1e-6); 1561 assert(0 && "_PyTimeSpec_get_time_info should never fail");
Charles-François Natali 2014/07/31 19:50:26 This is ugly.
haypo 2014/08/01 01:44:20 Done.
1562 return NULL;
1563 }
1564 return PyFloat_FromDouble((double)ts.tv_sec + ts.tv_nsec * 1e-9);
1562 } 1565 }
1563 1566
1564 1567
1565 /* Implement floatsleep() for various platforms. 1568 /* Implement floatsleep() for various platforms.
1566 When interrupted (or when another error occurs), return -1 and 1569 When interrupted (or when another error occurs), return -1 and
1567 set an exception; else return 0. */ 1570 set an exception; else return 0. */
1568 1571
1569 static int 1572 static int
1570 floatsleep(double secs) 1573 floatsleep(double secs)
1571 { 1574 {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1633 } 1636 }
1634 #else 1637 #else
1635 /* XXX Can't interrupt this sleep */ 1638 /* XXX Can't interrupt this sleep */
1636 Py_BEGIN_ALLOW_THREADS 1639 Py_BEGIN_ALLOW_THREADS
1637 sleep((int)secs); 1640 sleep((int)secs);
1638 Py_END_ALLOW_THREADS 1641 Py_END_ALLOW_THREADS
1639 #endif 1642 #endif
1640 1643
1641 return 0; 1644 return 0;
1642 } 1645 }
OLDNEW

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