summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2024-05-05 10:15:19 (GMT)
committerGitHub <noreply@github.com>2024-05-05 10:15:19 (GMT)
commitaa61f8bfcf2584dd8345f1f9a07e240100b79192 (patch)
treedb196e9316a20ff332290c56b60c38a6b533fae6 /Python
parentc7c9b913c01afb8d2ff4048f82155969f7ef75b1 (diff)
downloadcpython-aa61f8bfcf2584dd8345f1f9a07e240100b79192.zip
cpython-aa61f8bfcf2584dd8345f1f9a07e240100b79192.tar.gz
cpython-aa61f8bfcf2584dd8345f1f9a07e240100b79192.tar.bz2
gh-110850: Remove _PyTime_TimeUnchecked() function (#118552)
Use the new public Raw functions: * _PyTime_PerfCounterUnchecked() with PyTime_PerfCounterRaw() * _PyTime_TimeUnchecked() with PyTime_TimeRaw() * _PyTime_MonotonicUnchecked() with PyTime_MonotonicRaw() Remove internal functions: * _PyTime_PerfCounterUnchecked() * _PyTime_TimeUnchecked() * _PyTime_MonotonicUnchecked()
Diffstat (limited to 'Python')
-rw-r--r--Python/gc.c1
-rw-r--r--Python/gc_free_threading.c8
-rw-r--r--Python/import.c9
-rw-r--r--Python/lock.c14
-rw-r--r--Python/parking_lot.c17
-rw-r--r--Python/pytime.c47
-rw-r--r--Python/thread_pthread.h18
7 files changed, 51 insertions, 63 deletions
diff --git a/Python/gc.c b/Python/gc.c
index a487388..aa8b216 100644
--- a/Python/gc.c
+++ b/Python/gc.c
@@ -12,7 +12,6 @@
#include "pycore_object_alloc.h" // _PyObject_MallocWithType()
#include "pycore_pyerrors.h"
#include "pycore_pystate.h" // _PyThreadState_GET()
-#include "pycore_time.h" // _PyTime_PerfCounterUnchecked()
#include "pycore_weakref.h" // _PyWeakref_ClearRef()
#include "pydtrace.h"
diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c
index ef6aaad..ee006bb 100644
--- a/Python/gc_free_threading.c
+++ b/Python/gc_free_threading.c
@@ -11,7 +11,6 @@
#include "pycore_object_stack.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h" // _PyThreadState_GET()
-#include "pycore_time.h" // _PyTime_GetPerfCounter()
#include "pycore_tstate.h" // _PyThreadStateImpl
#include "pycore_weakref.h" // _PyWeakref_ClearRef()
#include "pydtrace.h"
@@ -1164,7 +1163,8 @@ gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason)
if (gcstate->debug & _PyGC_DEBUG_STATS) {
PySys_WriteStderr("gc: collecting generation %d...\n", generation);
show_stats_each_generations(gcstate);
- t1 = _PyTime_PerfCounterUnchecked();
+ // ignore error: don't interrupt the GC if reading the clock fails
+ (void)PyTime_PerfCounterRaw(&t1);
}
if (PyDTrace_GC_START_ENABLED()) {
@@ -1184,7 +1184,9 @@ gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason)
n = state.uncollectable;
if (gcstate->debug & _PyGC_DEBUG_STATS) {
- double d = PyTime_AsSecondsDouble(_PyTime_PerfCounterUnchecked() - t1);
+ PyTime_t t2;
+ (void)PyTime_PerfCounterRaw(&t2);
+ double d = PyTime_AsSecondsDouble(t2 - t1);
PySys_WriteStderr(
"gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n",
n+m, n, d);
diff --git a/Python/import.c b/Python/import.c
index fa0e548..3327707 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -13,7 +13,7 @@
#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_sysmodule.h" // _PySys_Audit()
-#include "pycore_time.h" // _PyTime_PerfCounterUnchecked()
+#include "pycore_time.h" // _PyTime_AsMicroseconds()
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
#include "marshal.h" // PyMarshal_ReadObjectFromString()
@@ -3468,7 +3468,8 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
#undef header
import_level++;
- t1 = _PyTime_PerfCounterUnchecked();
+ // ignore error: don't block import if reading the clock fails
+ (void)PyTime_PerfCounterRaw(&t1);
accumulated = 0;
}
@@ -3483,7 +3484,9 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
mod != NULL);
if (import_time) {
- PyTime_t cum = _PyTime_PerfCounterUnchecked() - t1;
+ PyTime_t t2;
+ (void)PyTime_PerfCounterRaw(&t2);
+ PyTime_t cum = t2 - t1;
import_level--;
fprintf(stderr, "import time: %9ld | %10ld | %*s%s\n",
diff --git a/Python/lock.c b/Python/lock.c
index 5ed95fc..239e56a 100644
--- a/Python/lock.c
+++ b/Python/lock.c
@@ -5,13 +5,13 @@
#include "pycore_lock.h"
#include "pycore_parking_lot.h"
#include "pycore_semaphore.h"
-#include "pycore_time.h" // _PyTime_MonotonicUnchecked()
+#include "pycore_time.h" // _PyTime_Add()
#ifdef MS_WINDOWS
# define WIN32_LEAN_AND_MEAN
-# include <windows.h> // SwitchToThread()
+# include <windows.h> // SwitchToThread()
#elif defined(HAVE_SCHED_H)
-# include <sched.h> // sched_yield()
+# include <sched.h> // sched_yield()
#endif
// If a thread waits on a lock for longer than TIME_TO_BE_FAIR_NS (1 ms), then
@@ -66,7 +66,9 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
return PY_LOCK_FAILURE;
}
- PyTime_t now = _PyTime_MonotonicUnchecked();
+ PyTime_t now;
+ // silently ignore error: cannot report error to the caller
+ (void)PyTime_MonotonicRaw(&now);
PyTime_t endtime = 0;
if (timeout > 0) {
endtime = _PyTime_Add(now, timeout);
@@ -143,7 +145,9 @@ mutex_unpark(PyMutex *m, struct mutex_entry *entry, int has_more_waiters)
{
uint8_t v = 0;
if (entry) {
- PyTime_t now = _PyTime_MonotonicUnchecked();
+ PyTime_t now;
+ // silently ignore error: cannot report error to the caller
+ (void)PyTime_MonotonicRaw(&now);
int should_be_fair = now > entry->time_to_be_fair;
entry->handed_off = should_be_fair;
diff --git a/Python/parking_lot.c b/Python/parking_lot.c
index b368b50..e2def9e 100644
--- a/Python/parking_lot.c
+++ b/Python/parking_lot.c
@@ -6,7 +6,7 @@
#include "pycore_pyerrors.h" // _Py_FatalErrorFormat
#include "pycore_pystate.h" // _PyThreadState_GET
#include "pycore_semaphore.h" // _PySemaphore
-#include "pycore_time.h" //_PyTime_MonotonicUnchecked()
+#include "pycore_time.h" // _PyTime_Add()
#include <stdbool.h>
@@ -120,13 +120,18 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, PyTime_t timeout)
struct timespec ts;
#if defined(CLOCK_MONOTONIC) && defined(HAVE_SEM_CLOCKWAIT)
- PyTime_t deadline = _PyTime_Add(_PyTime_MonotonicUnchecked(), timeout);
-
+ PyTime_t now;
+ // silently ignore error: cannot report error to the caller
+ (void)PyTime_MonotonicRaw(&now);
+ PyTime_t deadline = _PyTime_Add(now, timeout);
_PyTime_AsTimespec_clamp(deadline, &ts);
err = sem_clockwait(&sema->platform_sem, CLOCK_MONOTONIC, &ts);
#else
- PyTime_t deadline = _PyTime_Add(_PyTime_TimeUnchecked(), timeout);
+ PyTime_t now;
+ // silently ignore error: cannot report error to the caller
+ (void)PyTime_TimeRaw(&now);
+ PyTime_t deadline = _PyTime_Add(now, timeout);
_PyTime_AsTimespec_clamp(deadline, &ts);
@@ -163,7 +168,9 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, PyTime_t timeout)
_PyTime_AsTimespec_clamp(timeout, &ts);
err = pthread_cond_timedwait_relative_np(&sema->cond, &sema->mutex, &ts);
#else
- PyTime_t deadline = _PyTime_Add(_PyTime_TimeUnchecked(), timeout);
+ PyTime_t now;
+ (void)PyTime_TimeRaw(&now);
+ PyTime_t deadline = _PyTime_Add(now, timeout);
_PyTime_AsTimespec_clamp(deadline, &ts);
err = pthread_cond_timedwait(&sema->cond, &sema->mutex, &ts);
diff --git a/Python/pytime.c b/Python/pytime.c
index 12b36bb..560aea3 100644
--- a/Python/pytime.c
+++ b/Python/pytime.c
@@ -1030,22 +1030,6 @@ PyTime_TimeRaw(PyTime_t *result)
}
-PyTime_t
-_PyTime_TimeUnchecked(void)
-{
- PyTime_t t;
-#ifdef Py_DEBUG
- int result = PyTime_TimeRaw(&t);
- if (result != 0) {
- Py_FatalError("unable to read the system clock");
- }
-#else
- (void)PyTime_TimeRaw(&t);
-#endif
- return t;
-}
-
-
int
_PyTime_TimeWithInfo(PyTime_t *t, _Py_clock_info_t *info)
{
@@ -1270,22 +1254,6 @@ PyTime_MonotonicRaw(PyTime_t *result)
}
-PyTime_t
-_PyTime_MonotonicUnchecked(void)
-{
- PyTime_t t;
-#ifdef Py_DEBUG
- int result = PyTime_MonotonicRaw(&t);
- if (result != 0) {
- Py_FatalError("unable to read the monotonic clock");
- }
-#else
- (void)PyTime_MonotonicRaw(&t);
-#endif
- return t;
-}
-
-
int
_PyTime_MonotonicWithInfo(PyTime_t *tp, _Py_clock_info_t *info)
{
@@ -1314,13 +1282,6 @@ PyTime_PerfCounterRaw(PyTime_t *result)
}
-PyTime_t
-_PyTime_PerfCounterUnchecked(void)
-{
- return _PyTime_MonotonicUnchecked();
-}
-
-
int
_PyTime_localtime(time_t t, struct tm *tm)
{
@@ -1391,7 +1352,9 @@ _PyTime_gmtime(time_t t, struct tm *tm)
PyTime_t
_PyDeadline_Init(PyTime_t timeout)
{
- PyTime_t now = _PyTime_MonotonicUnchecked();
+ PyTime_t now;
+ // silently ignore error: cannot report error to the caller
+ (void)PyTime_MonotonicRaw(&now);
return _PyTime_Add(now, timeout);
}
@@ -1399,6 +1362,8 @@ _PyDeadline_Init(PyTime_t timeout)
PyTime_t
_PyDeadline_Get(PyTime_t deadline)
{
- PyTime_t now = _PyTime_MonotonicUnchecked();
+ PyTime_t now;
+ // silently ignore error: cannot report error to the caller
+ (void)PyTime_MonotonicRaw(&now);
return deadline - now;
}
diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h
index 65d366e..f588b46 100644
--- a/Python/thread_pthread.h
+++ b/Python/thread_pthread.h
@@ -158,12 +158,14 @@ _PyThread_cond_after(long long us, struct timespec *abs)
PyTime_t t;
#ifdef CONDATTR_MONOTONIC
if (condattr_monotonic) {
- t = _PyTime_MonotonicUnchecked();
+ // silently ignore error: cannot report error to the caller
+ (void)PyTime_MonotonicRaw(&t);
}
else
#endif
{
- t = _PyTime_TimeUnchecked();
+ // silently ignore error: cannot report error to the caller
+ (void)PyTime_TimeRaw(&t);
}
t = _PyTime_Add(t, timeout);
_PyTime_AsTimespec_clamp(t, abs);
@@ -506,7 +508,10 @@ PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
struct timespec abs_timeout;
// Local scope for deadline
{
- PyTime_t deadline = _PyTime_Add(_PyTime_MonotonicUnchecked(), timeout);
+ PyTime_t now;
+ // silently ignore error: cannot report error to the caller
+ (void)PyTime_MonotonicRaw(&now);
+ PyTime_t deadline = _PyTime_Add(now, timeout);
_PyTime_AsTimespec_clamp(deadline, &abs_timeout);
}
#else
@@ -522,8 +527,11 @@ PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
status = fix_status(sem_clockwait(thelock, CLOCK_MONOTONIC,
&abs_timeout));
#else
- PyTime_t abs_time = _PyTime_Add(_PyTime_TimeUnchecked(),
- timeout);
+ PyTime_t now;
+ // silently ignore error: cannot report error to the caller
+ (void)PyTime_TimeRaw(&now);
+ PyTime_t abs_time = _PyTime_Add(now, timeout);
+
struct timespec ts;
_PyTime_AsTimespec_clamp(abs_time, &ts);
status = fix_status(sem_timedwait(thelock, &ts));