Skip to content

Commit e412a4d

Browse files
Move init_condattr():ca to _PyRuntimeState.
1 parent f3bff05 commit e412a4d

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

Include/internal/pycore_pythread.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,30 @@ extern "C" {
3636

3737
#endif /* _POSIX_THREADS */
3838

39+
#if defined(_POSIX_THREADS) && !defined(HAVE_PTHREAD_STUBS)
40+
# define _USE_PTHREADS
41+
#endif
42+
43+
#if defined(_USE_PTHREADS) && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
44+
// monotonic is supported statically. It doesn't mean it works on runtime.
45+
# define CONDATTR_MONOTONIC
46+
#endif
47+
3948

4049
struct _pythread_runtime_state {
4150
int initialized;
42-
#if defined(_POSIX_THREADS) && !defined(HAVE_PTHREAD_STUBS)
51+
#ifdef _USE_PTHREADS
4352
// This matches when thread_pthread.h is used.
44-
/* NULL when pthread_condattr_setclock(CLOCK_MONOTONIC) is not supported. */
45-
pthread_condattr_t *condattr_monotonic;
46-
#endif
53+
struct {
54+
/* NULL when pthread_condattr_setclock(CLOCK_MONOTONIC) is not supported. */
55+
pthread_condattr_t *ptr;
56+
# ifdef CONDATTR_MONOTONIC
57+
/* The value to which condattr_monotonic is set. */
58+
pthread_condattr_t val;
59+
# endif
60+
} _condattr_monotonic;
61+
62+
#endif // USE_PTHREADS
4763
};
4864

4965

Python/thread.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ PyThread_init_thread(void)
3434
#if defined(HAVE_PTHREAD_STUBS)
3535
# define PYTHREAD_NAME "pthread-stubs"
3636
# include "thread_pthread_stubs.h"
37-
#elif defined(_POSIX_THREADS)
37+
#elif defined(_USE_PTHREADS) /* AKA _PTHREADS */
3838
# if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)
3939
# define PYTHREAD_NAME "pthread-stubs"
4040
# else

Python/thread_pthread.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,16 @@
119119
* pthread_cond support
120120
*/
121121

122-
#if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
123-
// monotonic is supported statically. It doesn't mean it works on runtime.
124-
#define CONDATTR_MONOTONIC
125-
#endif
126-
127-
#define condattr_monotonic _PyRuntime.threads.condattr_monotonic
122+
#define condattr_monotonic _PyRuntime.threads._condattr_monotonic.ptr
128123

129124
static void
130125
init_condattr(void)
131126
{
132127
#ifdef CONDATTR_MONOTONIC
133-
static pthread_condattr_t ca;
128+
# define ca _PyRuntime.threads._condattr_monotonic.val
129+
// XXX We need to check the return code?
134130
pthread_condattr_init(&ca);
131+
// XXX We need to run pthread_condattr_destroy() during runtime fini.
135132
if (pthread_condattr_setclock(&ca, CLOCK_MONOTONIC) == 0) {
136133
condattr_monotonic = &ca; // Use monotonic clock
137134
}

Tools/c-analyzer/cpython/ignored.tsv

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ Python/fileutils.c set_inheritable ioctl_works -
2020

2121
## guards around resource init
2222
Python/thread_pthread.h PyThread__init_thread lib_initialized -
23-
# static buffer used during one-time initialization
24-
Python/thread_pthread.h init_condattr ca -
2523

2624
##-----------------------
2725
## other values (not Python-specific)

0 commit comments

Comments
 (0)