You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

780 lines
18 KiB

  1. #include "Python.h"
  2. #ifdef MS_WINDOWS
  3. #include <windows.h>
  4. #endif
  5. #if defined(__APPLE__)
  6. #include <mach/mach_time.h> /* mach_absolute_time(), mach_timebase_info() */
  7. #endif
  8. #define _PyTime_check_mul_overflow(a, b) \
  9. (assert(b > 0), \
  10. (_PyTime_t)(a) < _PyTime_MIN / (_PyTime_t)(b) \
  11. || _PyTime_MAX / (_PyTime_t)(b) < (_PyTime_t)(a))
  12. /* To millisecond (10^-3) */
  13. #define SEC_TO_MS 1000
  14. /* To microseconds (10^-6) */
  15. #define MS_TO_US 1000
  16. #define SEC_TO_US (SEC_TO_MS * MS_TO_US)
  17. /* To nanoseconds (10^-9) */
  18. #define US_TO_NS 1000
  19. #define MS_TO_NS (MS_TO_US * US_TO_NS)
  20. #define SEC_TO_NS (SEC_TO_MS * MS_TO_NS)
  21. /* Conversion from nanoseconds */
  22. #define NS_TO_MS (1000 * 1000)
  23. #define NS_TO_US (1000)
  24. static void
  25. error_time_t_overflow(void)
  26. {
  27. PyErr_SetString(PyExc_OverflowError,
  28. "timestamp out of range for platform time_t");
  29. }
  30. time_t
  31. _PyLong_AsTime_t(PyObject *obj)
  32. {
  33. #if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
  34. PY_LONG_LONG val;
  35. val = PyLong_AsLongLong(obj);
  36. #else
  37. long val;
  38. Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
  39. val = PyLong_AsLong(obj);
  40. #endif
  41. if (val == -1 && PyErr_Occurred()) {
  42. if (PyErr_ExceptionMatches(PyExc_OverflowError))
  43. error_time_t_overflow();
  44. return -1;
  45. }
  46. return (time_t)val;
  47. }
  48. PyObject *
  49. _PyLong_FromTime_t(time_t t)
  50. {
  51. #if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
  52. return PyLong_FromLongLong((PY_LONG_LONG)t);
  53. #else
  54. Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
  55. return PyLong_FromLong((long)t);
  56. #endif
  57. }
  58. /* Round to nearest with ties going to nearest even integer
  59. (_PyTime_ROUND_HALF_EVEN) */
  60. static double
  61. _PyTime_RoundHalfEven(double x)
  62. {
  63. double rounded = round(x);
  64. if (fabs(x-rounded) == 0.5)
  65. /* halfway case: round to even */
  66. rounded = 2.0*round(x/2.0);
  67. return rounded;
  68. }
  69. static double
  70. _PyTime_Round(double x, _PyTime_round_t round)
  71. {
  72. /* volatile avoids optimization changing how numbers are rounded */
  73. volatile double d;
  74. d = x;
  75. if (round == _PyTime_ROUND_HALF_EVEN)
  76. d = _PyTime_RoundHalfEven(d);
  77. else if (round == _PyTime_ROUND_CEILING)
  78. d = ceil(d);
  79. else
  80. d = floor(d);
  81. return d;
  82. }
  83. static int
  84. _PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator,
  85. double denominator, _PyTime_round_t round)
  86. {
  87. double intpart, err;
  88. /* volatile avoids optimization changing how numbers are rounded */
  89. volatile double floatpart;
  90. floatpart = modf(d, &intpart);
  91. floatpart *= denominator;
  92. floatpart = _PyTime_Round(floatpart, round);
  93. if (floatpart >= denominator) {
  94. floatpart -= denominator;
  95. intpart += 1.0;
  96. }
  97. else if (floatpart < 0) {
  98. floatpart += denominator;
  99. intpart -= 1.0;
  100. }
  101. assert(0.0 <= floatpart && floatpart < denominator);
  102. *sec = (time_t)intpart;
  103. *numerator = (long)floatpart;
  104. err = intpart - (double)*sec;
  105. if (err <= -1.0 || err >= 1.0) {
  106. error_time_t_overflow();
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. static int
  112. _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
  113. double denominator, _PyTime_round_t round)
  114. {
  115. assert(denominator <= (double)LONG_MAX);
  116. if (PyFloat_Check(obj)) {
  117. double d = PyFloat_AsDouble(obj);
  118. return _PyTime_DoubleToDenominator(d, sec, numerator,
  119. denominator, round);
  120. }
  121. else {
  122. *sec = _PyLong_AsTime_t(obj);
  123. *numerator = 0;
  124. if (*sec == (time_t)-1 && PyErr_Occurred())
  125. return -1;
  126. return 0;
  127. }
  128. }
  129. int
  130. _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
  131. {
  132. if (PyFloat_Check(obj)) {
  133. double intpart, err;
  134. /* volatile avoids optimization changing how numbers are rounded */
  135. volatile double d;
  136. d = PyFloat_AsDouble(obj);
  137. d = _PyTime_Round(d, round);
  138. (void)modf(d, &intpart);
  139. *sec = (time_t)intpart;
  140. err = intpart - (double)*sec;
  141. if (err <= -1.0 || err >= 1.0) {
  142. error_time_t_overflow();
  143. return -1;
  144. }
  145. return 0;
  146. }
  147. else {
  148. *sec = _PyLong_AsTime_t(obj);
  149. if (*sec == (time_t)-1 && PyErr_Occurred())
  150. return -1;
  151. return 0;
  152. }
  153. }
  154. int
  155. _PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
  156. _PyTime_round_t round)
  157. {
  158. int res;
  159. res = _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round);
  160. assert(0 <= *nsec && *nsec < SEC_TO_NS);
  161. return res;
  162. }
  163. int
  164. _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
  165. _PyTime_round_t round)
  166. {
  167. int res;
  168. res = _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round);
  169. assert(0 <= *usec && *usec < SEC_TO_US);
  170. return res;
  171. }
  172. static void
  173. _PyTime_overflow(void)
  174. {
  175. PyErr_SetString(PyExc_OverflowError,
  176. "timestamp too large to convert to C _PyTime_t");
  177. }
  178. _PyTime_t
  179. _PyTime_FromSeconds(int seconds)
  180. {
  181. _PyTime_t t;
  182. t = (_PyTime_t)seconds;
  183. /* ensure that integer overflow cannot happen, int type should have 32
  184. bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
  185. bits). */
  186. Py_BUILD_ASSERT(INT_MAX <= _PyTime_MAX / SEC_TO_NS);
  187. Py_BUILD_ASSERT(INT_MIN >= _PyTime_MIN / SEC_TO_NS);
  188. assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS)
  189. || (t < 0 && t >= _PyTime_MIN / SEC_TO_NS));
  190. t *= SEC_TO_NS;
  191. return t;
  192. }
  193. _PyTime_t
  194. _PyTime_FromNanoseconds(PY_LONG_LONG ns)
  195. {
  196. _PyTime_t t;
  197. Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
  198. t = Py_SAFE_DOWNCAST(ns, PY_LONG_LONG, _PyTime_t);
  199. return t;
  200. }
  201. #ifdef HAVE_CLOCK_GETTIME
  202. static int
  203. _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts, int raise)
  204. {
  205. _PyTime_t t;
  206. int res = 0;
  207. Py_BUILD_ASSERT(sizeof(ts->tv_sec) <= sizeof(_PyTime_t));
  208. t = (_PyTime_t)ts->tv_sec;
  209. if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
  210. if (raise)
  211. _PyTime_overflow();
  212. res = -1;
  213. }
  214. t = t * SEC_TO_NS;
  215. t += ts->tv_nsec;
  216. *tp = t;
  217. return res;
  218. }
  219. #elif !defined(MS_WINDOWS)
  220. static int
  221. _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
  222. {
  223. _PyTime_t t;
  224. int res = 0;
  225. Py_BUILD_ASSERT(sizeof(tv->tv_sec) <= sizeof(_PyTime_t));
  226. t = (_PyTime_t)tv->tv_sec;
  227. if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
  228. if (raise)
  229. _PyTime_overflow();
  230. res = -1;
  231. }
  232. t = t * SEC_TO_NS;
  233. t += (_PyTime_t)tv->tv_usec * US_TO_NS;
  234. *tp = t;
  235. return res;
  236. }
  237. #endif
  238. static int
  239. _PyTime_FromFloatObject(_PyTime_t *t, double value, _PyTime_round_t round,
  240. long unit_to_ns)
  241. {
  242. double err;
  243. /* volatile avoids optimization changing how numbers are rounded */
  244. volatile double d;
  245. /* convert to a number of nanoseconds */
  246. d = value;
  247. d *= (double)unit_to_ns;
  248. d = _PyTime_Round(d, round);
  249. *t = (_PyTime_t)d;
  250. err = d - (double)*t;
  251. if (fabs(err) >= 1.0) {
  252. _PyTime_overflow();
  253. return -1;
  254. }
  255. return 0;
  256. }
  257. static int
  258. _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
  259. long unit_to_ns)
  260. {
  261. if (PyFloat_Check(obj)) {
  262. double d;
  263. d = PyFloat_AsDouble(obj);
  264. return _PyTime_FromFloatObject(t, d, round, unit_to_ns);
  265. }
  266. else {
  267. #ifdef HAVE_LONG_LONG
  268. PY_LONG_LONG sec;
  269. Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
  270. sec = PyLong_AsLongLong(obj);
  271. #else
  272. long sec;
  273. Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
  274. sec = PyLong_AsLong(obj);
  275. #endif
  276. if (sec == -1 && PyErr_Occurred()) {
  277. if (PyErr_ExceptionMatches(PyExc_OverflowError))
  278. _PyTime_overflow();
  279. return -1;
  280. }
  281. if (_PyTime_check_mul_overflow(sec, unit_to_ns)) {
  282. _PyTime_overflow();
  283. return -1;
  284. }
  285. *t = sec * unit_to_ns;
  286. return 0;
  287. }
  288. }
  289. int
  290. _PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
  291. {
  292. return _PyTime_FromObject(t, obj, round, SEC_TO_NS);
  293. }
  294. int
  295. _PyTime_FromMillisecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
  296. {
  297. return _PyTime_FromObject(t, obj, round, MS_TO_NS);
  298. }
  299. double
  300. _PyTime_AsSecondsDouble(_PyTime_t t)
  301. {
  302. /* volatile avoids optimization changing how numbers are rounded */
  303. volatile double d;
  304. if (t % SEC_TO_NS == 0) {
  305. _PyTime_t secs;
  306. /* Divide using integers to avoid rounding issues on the integer part.
  307. 1e-9 cannot be stored exactly in IEEE 64-bit. */
  308. secs = t / SEC_TO_NS;
  309. d = (double)secs;
  310. }
  311. else {
  312. d = (double)t;
  313. d /= 1e9;
  314. }
  315. return d;
  316. }
  317. PyObject *
  318. _PyTime_AsNanosecondsObject(_PyTime_t t)
  319. {
  320. #ifdef HAVE_LONG_LONG
  321. Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) >= sizeof(_PyTime_t));
  322. return PyLong_FromLongLong((PY_LONG_LONG)t);
  323. #else
  324. Py_BUILD_ASSERT(sizeof(long) >= sizeof(_PyTime_t));
  325. return PyLong_FromLong((long)t);
  326. #endif
  327. }
  328. static _PyTime_t
  329. _PyTime_Divide(const _PyTime_t t, const _PyTime_t k,
  330. const _PyTime_round_t round)
  331. {
  332. assert(k > 1);
  333. if (round == _PyTime_ROUND_HALF_EVEN) {
  334. _PyTime_t x, r, abs_r;
  335. x = t / k;
  336. r = t % k;
  337. abs_r = Py_ABS(r);
  338. if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) {
  339. if (t >= 0)
  340. x++;
  341. else
  342. x--;
  343. }
  344. return x;
  345. }
  346. else if (round == _PyTime_ROUND_CEILING) {
  347. if (t >= 0)
  348. return (t + k - 1) / k;
  349. else
  350. return t / k;
  351. }
  352. else {
  353. if (t >= 0)
  354. return t / k;
  355. else
  356. return (t - (k - 1)) / k;
  357. }
  358. }
  359. _PyTime_t
  360. _PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round)
  361. {
  362. return _PyTime_Divide(t, NS_TO_MS, round);
  363. }
  364. _PyTime_t
  365. _PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
  366. {
  367. return _PyTime_Divide(t, NS_TO_US, round);
  368. }
  369. static int
  370. _PyTime_AsTimeval_impl(_PyTime_t t, _PyTime_t *p_secs, int *p_us,
  371. _PyTime_round_t round)
  372. {
  373. _PyTime_t secs, ns;
  374. int usec;
  375. int res = 0;
  376. secs = t / SEC_TO_NS;
  377. ns = t % SEC_TO_NS;
  378. usec = (int)_PyTime_Divide(ns, US_TO_NS, round);
  379. if (usec < 0) {
  380. usec += SEC_TO_US;
  381. if (secs != _PyTime_MIN)
  382. secs -= 1;
  383. else
  384. res = -1;
  385. }
  386. else if (usec >= SEC_TO_US) {
  387. usec -= SEC_TO_US;
  388. if (secs != _PyTime_MAX)
  389. secs += 1;
  390. else
  391. res = -1;
  392. }
  393. assert(0 <= usec && usec < SEC_TO_US);
  394. *p_secs = secs;
  395. *p_us = usec;
  396. return res;
  397. }
  398. static int
  399. _PyTime_AsTimevalStruct_impl(_PyTime_t t, struct timeval *tv,
  400. _PyTime_round_t round, int raise)
  401. {
  402. _PyTime_t secs, secs2;
  403. int us;
  404. int res;
  405. res = _PyTime_AsTimeval_impl(t, &secs, &us, round);
  406. #ifdef MS_WINDOWS
  407. tv->tv_sec = (long)secs;
  408. #else
  409. tv->tv_sec = secs;
  410. #endif
  411. tv->tv_usec = us;
  412. secs2 = (_PyTime_t)tv->tv_sec;
  413. if (res < 0 || secs2 != secs) {
  414. if (raise)
  415. error_time_t_overflow();
  416. return -1;
  417. }
  418. return 0;
  419. }
  420. int
  421. _PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
  422. {
  423. return _PyTime_AsTimevalStruct_impl(t, tv, round, 1);
  424. }
  425. int
  426. _PyTime_AsTimeval_noraise(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
  427. {
  428. return _PyTime_AsTimevalStruct_impl(t, tv, round, 0);
  429. }
  430. int
  431. _PyTime_AsTimevalTime_t(_PyTime_t t, time_t *p_secs, int *us,
  432. _PyTime_round_t round)
  433. {
  434. _PyTime_t secs;
  435. int res;
  436. res = _PyTime_AsTimeval_impl(t, &secs, us, round);
  437. *p_secs = secs;
  438. if (res < 0 || (_PyTime_t)*p_secs != secs) {
  439. error_time_t_overflow();
  440. return -1;
  441. }
  442. return 0;
  443. }
  444. #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
  445. int
  446. _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
  447. {
  448. _PyTime_t secs, nsec;
  449. secs = t / SEC_TO_NS;
  450. nsec = t % SEC_TO_NS;
  451. if (nsec < 0) {
  452. nsec += SEC_TO_NS;
  453. secs -= 1;
  454. }
  455. ts->tv_sec = (time_t)secs;
  456. assert(0 <= nsec && nsec < SEC_TO_NS);
  457. ts->tv_nsec = nsec;
  458. if ((_PyTime_t)ts->tv_sec != secs) {
  459. error_time_t_overflow();
  460. return -1;
  461. }
  462. return 0;
  463. }
  464. #endif
  465. static int
  466. pygettimeofday(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
  467. {
  468. #ifdef MS_WINDOWS
  469. FILETIME system_time;
  470. ULARGE_INTEGER large;
  471. assert(info == NULL || raise);
  472. GetSystemTimeAsFileTime(&system_time);
  473. large.u.LowPart = system_time.dwLowDateTime;
  474. large.u.HighPart = system_time.dwHighDateTime;
  475. /* 11,644,473,600,000,000,000: number of nanoseconds between
  476. the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
  477. days). */
  478. *tp = large.QuadPart * 100 - 11644473600000000000;
  479. if (info) {
  480. DWORD timeAdjustment, timeIncrement;
  481. BOOL isTimeAdjustmentDisabled, ok;
  482. info->implementation = "GetSystemTimeAsFileTime()";
  483. info->monotonic = 0;
  484. ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
  485. &isTimeAdjustmentDisabled);
  486. if (!ok) {
  487. PyErr_SetFromWindowsErr(0);
  488. return -1;
  489. }
  490. info->resolution = timeIncrement * 1e-7;
  491. info->adjustable = 1;
  492. }
  493. #else /* MS_WINDOWS */
  494. int err;
  495. #ifdef HAVE_CLOCK_GETTIME
  496. struct timespec ts;
  497. #else
  498. struct timeval tv;
  499. #endif
  500. assert(info == NULL || raise);
  501. #ifdef HAVE_CLOCK_GETTIME
  502. err = clock_gettime(CLOCK_REALTIME, &ts);
  503. if (err) {
  504. if (raise)
  505. PyErr_SetFromErrno(PyExc_OSError);
  506. return -1;
  507. }
  508. if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
  509. return -1;
  510. if (info) {
  511. struct timespec res;
  512. info->implementation = "clock_gettime(CLOCK_REALTIME)";
  513. info->monotonic = 0;
  514. info->adjustable = 1;
  515. if (clock_getres(CLOCK_REALTIME, &res) == 0)
  516. info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
  517. else
  518. info->resolution = 1e-9;
  519. }
  520. #else /* HAVE_CLOCK_GETTIME */
  521. /* test gettimeofday() */
  522. #ifdef GETTIMEOFDAY_NO_TZ
  523. err = gettimeofday(&tv);
  524. #else
  525. err = gettimeofday(&tv, (struct timezone *)NULL);
  526. #endif
  527. if (err) {
  528. if (raise)
  529. PyErr_SetFromErrno(PyExc_OSError);
  530. return -1;
  531. }
  532. if (_PyTime_FromTimeval(tp, &tv, raise) < 0)
  533. return -1;
  534. if (info) {
  535. info->implementation = "gettimeofday()";
  536. info->resolution = 1e-6;
  537. info->monotonic = 0;
  538. info->adjustable = 1;
  539. }
  540. #endif /* !HAVE_CLOCK_GETTIME */
  541. #endif /* !MS_WINDOWS */
  542. return 0;
  543. }
  544. _PyTime_t
  545. _PyTime_GetSystemClock(void)
  546. {
  547. _PyTime_t t;
  548. if (pygettimeofday(&t, NULL, 0) < 0) {
  549. /* should not happen, _PyTime_Init() checked the clock at startup */
  550. assert(0);
  551. /* use a fixed value instead of a random value from the stack */
  552. t = 0;
  553. }
  554. return t;
  555. }
  556. int
  557. _PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
  558. {
  559. return pygettimeofday(t, info, 1);
  560. }
  561. static int
  562. pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
  563. {
  564. #if defined(MS_WINDOWS)
  565. ULONGLONG ticks;
  566. _PyTime_t t;
  567. assert(info == NULL || raise);
  568. ticks = GetTickCount64();
  569. Py_BUILD_ASSERT(sizeof(ticks) <= sizeof(_PyTime_t));
  570. t = (_PyTime_t)ticks;
  571. if (_PyTime_check_mul_overflow(t, MS_TO_NS)) {
  572. if (raise) {
  573. _PyTime_overflow();
  574. return -1;
  575. }
  576. /* Hello, time traveler! */
  577. assert(0);
  578. }
  579. *tp = t * MS_TO_NS;
  580. if (info) {
  581. DWORD timeAdjustment, timeIncrement;
  582. BOOL isTimeAdjustmentDisabled, ok;
  583. info->implementation = "GetTickCount64()";
  584. info->monotonic = 1;
  585. ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
  586. &isTimeAdjustmentDisabled);
  587. if (!ok) {
  588. PyErr_SetFromWindowsErr(0);
  589. return -1;
  590. }
  591. info->resolution = timeIncrement * 1e-7;
  592. info->adjustable = 0;
  593. }
  594. #elif defined(__APPLE__)
  595. static mach_timebase_info_data_t timebase;
  596. uint64_t time;
  597. if (timebase.denom == 0) {
  598. /* According to the Technical Q&A QA1398, mach_timebase_info() cannot
  599. fail: https://developer.apple.com/library/mac/#qa/qa1398/ */
  600. (void)mach_timebase_info(&timebase);
  601. }
  602. time = mach_absolute_time();
  603. /* apply timebase factor */
  604. time *= timebase.numer;
  605. time /= timebase.denom;
  606. *tp = time;
  607. if (info) {
  608. info->implementation = "mach_absolute_time()";
  609. info->resolution = (double)timebase.numer / timebase.denom * 1e-9;
  610. info->monotonic = 1;
  611. info->adjustable = 0;
  612. }
  613. #else
  614. struct timespec ts;
  615. #ifdef CLOCK_HIGHRES
  616. const clockid_t clk_id = CLOCK_HIGHRES;
  617. const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
  618. #else
  619. const clockid_t clk_id = CLOCK_MONOTONIC;
  620. const char *implementation = "clock_gettime(CLOCK_MONOTONIC)";
  621. #endif
  622. assert(info == NULL || raise);
  623. if (clock_gettime(clk_id, &ts) != 0) {
  624. if (raise) {
  625. PyErr_SetFromErrno(PyExc_OSError);
  626. return -1;
  627. }
  628. return -1;
  629. }
  630. if (info) {
  631. struct timespec res;
  632. info->monotonic = 1;
  633. info->implementation = implementation;
  634. info->adjustable = 0;
  635. if (clock_getres(clk_id, &res) != 0) {
  636. PyErr_SetFromErrno(PyExc_OSError);
  637. return -1;
  638. }
  639. info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
  640. }
  641. if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
  642. return -1;
  643. #endif
  644. return 0;
  645. }
  646. _PyTime_t
  647. _PyTime_GetMonotonicClock(void)
  648. {
  649. _PyTime_t t;
  650. if (pymonotonic(&t, NULL, 0) < 0) {
  651. /* should not happen, _PyTime_Init() checked that monotonic clock at
  652. startup */
  653. assert(0);
  654. /* use a fixed value instead of a random value from the stack */
  655. t = 0;
  656. }
  657. return t;
  658. }
  659. int
  660. _PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
  661. {
  662. return pymonotonic(tp, info, 1);
  663. }
  664. int
  665. _PyTime_Init(void)
  666. {
  667. _PyTime_t t;
  668. /* ensure that the system clock works */
  669. if (_PyTime_GetSystemClockWithInfo(&t, NULL) < 0)
  670. return -1;
  671. /* ensure that the operating system provides a monotonic clock */
  672. if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0)
  673. return -1;
  674. return 0;
  675. }