|
| 1 | +#define PY_SSIZE_T_CLEAN |
| 2 | +#include <Python.h> |
| 3 | +#include <time.h> |
| 4 | +#include <stdint.h> |
| 5 | +#include "librt_time.h" |
| 6 | +#include "pythoncapi_compat.h" |
| 7 | +#include "mypyc_util.h" |
| 8 | + |
| 9 | +#ifdef _WIN32 |
| 10 | +#include <windows.h> |
| 11 | +#else |
| 12 | +#include <sys/time.h> |
| 13 | +#endif |
| 14 | + |
| 15 | +#ifdef MYPYC_EXPERIMENTAL |
| 16 | + |
| 17 | +// Internal function that returns a C double for mypyc primitives |
| 18 | +// Returns high-precision time in seconds (like time.time()) |
| 19 | +static double |
| 20 | +time_time_internal(void) { |
| 21 | +#ifdef _WIN32 |
| 22 | + // Windows: Use GetSystemTimePreciseAsFileTime for ~100ns precision |
| 23 | + FILETIME ft; |
| 24 | + ULARGE_INTEGER large; |
| 25 | + |
| 26 | + GetSystemTimePreciseAsFileTime(&ft); |
| 27 | + large.LowPart = ft.dwLowDateTime; |
| 28 | + large.HighPart = ft.dwHighDateTime; |
| 29 | + |
| 30 | + // Windows FILETIME is 100-nanosecond intervals since January 1, 1601 |
| 31 | + // 116444736000000000 = number of 100-ns intervals between 1601 and 1970 |
| 32 | + // Convert directly to seconds: 100ns * 1e-9 = 1e-7 |
| 33 | + int64_t intervals = large.QuadPart - 116444736000000000LL; |
| 34 | + return (double)intervals * 1e-7; |
| 35 | + |
| 36 | +#else // Unix-like systems (Linux, macOS, BSD, etc.) |
| 37 | + |
| 38 | + // Try clock_gettime(CLOCK_REALTIME) for nanosecond precision |
| 39 | + // This is available on POSIX.1-2001 and later (widely available on modern systems) |
| 40 | +#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 |
| 41 | + struct timespec ts; |
| 42 | + if (clock_gettime(CLOCK_REALTIME, &ts) == 0) { |
| 43 | + // Convert seconds and nanoseconds separately to avoid large integer operations |
| 44 | + return (double)ts.tv_sec + (double)ts.tv_nsec * 1e-9; |
| 45 | + } |
| 46 | + // Fall through to gettimeofday if clock_gettime failed |
| 47 | +#endif |
| 48 | + |
| 49 | + // Fallback: gettimeofday for microsecond precision |
| 50 | + // This is widely available (POSIX.1-2001, BSD, etc.) |
| 51 | + struct timeval tv; |
| 52 | + if (unlikely(gettimeofday(&tv, NULL) != 0)) { |
| 53 | + PyErr_SetFromErrno(PyExc_OSError); |
| 54 | + return CPY_FLOAT_ERROR; |
| 55 | + } |
| 56 | + |
| 57 | + // Convert seconds and microseconds separately to avoid large integer operations |
| 58 | + return (double)tv.tv_sec + (double)tv.tv_usec * 1e-6; |
| 59 | +#endif |
| 60 | +} |
| 61 | + |
| 62 | +// Wrapper function for normal Python extension usage |
| 63 | +static PyObject* |
| 64 | +time_time(PyObject *self, PyObject *const *args, size_t nargs) { |
| 65 | + if (nargs != 0) { |
| 66 | + PyErr_SetString(PyExc_TypeError, "time() takes no arguments"); |
| 67 | + return NULL; |
| 68 | + } |
| 69 | + |
| 70 | + double result = time_time_internal(); |
| 71 | + if (result == CPY_FLOAT_ERROR) { |
| 72 | + return NULL; |
| 73 | + } |
| 74 | + return PyFloat_FromDouble(result); |
| 75 | +} |
| 76 | + |
| 77 | +#endif |
| 78 | + |
| 79 | +static PyMethodDef librt_time_module_methods[] = { |
| 80 | +#ifdef MYPYC_EXPERIMENTAL |
| 81 | + {"time", (PyCFunction)time_time, METH_FASTCALL, |
| 82 | + PyDoc_STR("Return the current time in seconds since the Unix epoch as a floating point number.")}, |
| 83 | +#endif |
| 84 | + {NULL, NULL, 0, NULL} |
| 85 | +}; |
| 86 | + |
| 87 | +#ifdef MYPYC_EXPERIMENTAL |
| 88 | + |
| 89 | +static int |
| 90 | +time_abi_version(void) { |
| 91 | + return LIBRT_TIME_ABI_VERSION; |
| 92 | +} |
| 93 | + |
| 94 | +static int |
| 95 | +time_api_version(void) { |
| 96 | + return LIBRT_TIME_API_VERSION; |
| 97 | +} |
| 98 | + |
| 99 | +#endif |
| 100 | + |
| 101 | +static int |
| 102 | +librt_time_module_exec(PyObject *m) |
| 103 | +{ |
| 104 | +#ifdef MYPYC_EXPERIMENTAL |
| 105 | + // Export mypyc internal C API via capsule |
| 106 | + static void *time_api[LIBRT_TIME_API_LEN] = { |
| 107 | + (void *)time_abi_version, |
| 108 | + (void *)time_api_version, |
| 109 | + (void *)time_time_internal, |
| 110 | + }; |
| 111 | + PyObject *c_api_object = PyCapsule_New((void *)time_api, "librt.time._C_API", NULL); |
| 112 | + if (PyModule_Add(m, "_C_API", c_api_object) < 0) { |
| 113 | + return -1; |
| 114 | + } |
| 115 | +#endif |
| 116 | + return 0; |
| 117 | +} |
| 118 | + |
| 119 | +static PyModuleDef_Slot librt_time_module_slots[] = { |
| 120 | + {Py_mod_exec, librt_time_module_exec}, |
| 121 | +#ifdef Py_MOD_GIL_NOT_USED |
| 122 | + {Py_mod_gil, Py_MOD_GIL_NOT_USED}, |
| 123 | +#endif |
| 124 | + {0, NULL} |
| 125 | +}; |
| 126 | + |
| 127 | +static PyModuleDef librt_time_module = { |
| 128 | + .m_base = PyModuleDef_HEAD_INIT, |
| 129 | + .m_name = "time", |
| 130 | + .m_doc = "Fast time() function optimized for mypyc", |
| 131 | + .m_size = 0, |
| 132 | + .m_methods = librt_time_module_methods, |
| 133 | + .m_slots = librt_time_module_slots, |
| 134 | +}; |
| 135 | + |
| 136 | +PyMODINIT_FUNC |
| 137 | +PyInit_time(void) |
| 138 | +{ |
| 139 | + return PyModuleDef_Init(&librt_time_module); |
| 140 | +} |
0 commit comments