Skip to content

Commit

Permalink
Move ticks_per_second to _PyRuntimeState.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently committed Dec 8, 2022
1 parent 9a86dd3 commit 7f6d5b8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
20 changes: 18 additions & 2 deletions Include/internal/pycore_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,27 @@ extern "C" {
#endif


#ifndef MS_WINDOWS
#define _OS_NEED_TICKS_PER_SECOND
# define need_ticks_per_second_STATE \
long ticks_per_second;
# define need_ticks_per_second_INIT \
.ticks_per_second = -1,
#else
# define need_ticks_per_second_STATE
# define need_ticks_per_second_INIT
#endif /* MS_WINDOWS */


struct _os_runtime_state {
int _not_used;
need_ticks_per_second_STATE
};

#define _OS_RUNTIME_INIT {0}
# define _OS_RUNTIME_INIT \
{ \
._not_used = 0, \
need_ticks_per_second_INIT \
}


#ifdef __cplusplus
Expand Down
36 changes: 22 additions & 14 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -9051,10 +9051,23 @@ build_times_result(PyObject *module, double user, double system,
}


#ifndef MS_WINDOWS
#define NEED_TICKS_PER_SECOND
static long ticks_per_second = -1;
#endif /* MS_WINDOWS */
#ifdef _OS_NEED_TICKS_PER_SECOND
#define ticks_per_second _PyRuntime.os.ticks_per_second
static void
ticks_per_second_init(void)
{
if (ticks_per_second != -1) {
return;
}
# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
ticks_per_second = sysconf(_SC_CLK_TCK);
# elif defined(HZ)
ticks_per_second = HZ;
# else
ticks_per_second = 60; /* magic fallback value; may be bogus */
# endif
}
#endif

/*[clinic input]
os.times
Expand Down Expand Up @@ -9089,10 +9102,10 @@ os_times_impl(PyObject *module)
(double)0,
(double)0);
}
#elif !defined(_OS_NEED_TICKS_PER_SECOND)
# error "missing ticks_per_second"
#else /* MS_WINDOWS */
{


struct tms t;
clock_t c;
errno = 0;
Expand Down Expand Up @@ -15922,14 +15935,9 @@ posixmodule_exec(PyObject *m)
}
PyModule_AddObject(m, "statvfs_result", Py_NewRef(StatVFSResultType));
state->StatVFSResultType = StatVFSResultType;
#ifdef NEED_TICKS_PER_SECOND
# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
ticks_per_second = sysconf(_SC_CLK_TCK);
# elif defined(HZ)
ticks_per_second = HZ;
# else
ticks_per_second = 60; /* magic fallback value; may be bogus */
# endif

#ifdef _OS_NEED_TICKS_PER_SECOND
ticks_per_second_init();
#endif

#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Expand Down
1 change: 0 additions & 1 deletion Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ Modules/faulthandler.c - old_stack -
## initialized once

Modules/posixmodule.c - structseq_new -
Modules/posixmodule.c - ticks_per_second -
Modules/timemodule.c _PyTime_GetClockWithInfo initialized -
Modules/timemodule.c _PyTime_GetProcessTimeWithInfo ticks_per_second -

Expand Down

0 comments on commit 7f6d5b8

Please sign in to comment.