@@ -56,11 +56,6 @@ static struct _inittab *inittab_copy = NULL;
56
56
#define LAST_MODULE_INDEX _PyRuntime.imports.last_module_index
57
57
#define EXTENSIONS _PyRuntime.imports.extensions
58
58
59
- #define import_lock _PyRuntime.imports.lock.mutex
60
- #define import_lock_thread _PyRuntime.imports.lock.thread
61
- #define import_lock_level _PyRuntime.imports.lock.level
62
-
63
- #define FIND_AND_LOAD _PyRuntime.imports.find_and_load
64
59
#define PKGCONTEXT (_PyRuntime.imports.pkgcontext)
65
60
66
61
@@ -85,6 +80,16 @@ static struct _inittab *inittab_copy = NULL;
85
80
#define IMPORT_FUNC (interp ) \
86
81
(interp)->imports.import_func
87
82
83
+ #define IMPORT_LOCK (interp ) \
84
+ (interp)->imports.lock.mutex
85
+ #define IMPORT_LOCK_THREAD (interp ) \
86
+ (interp)->imports.lock.thread
87
+ #define IMPORT_LOCK_LEVEL (interp ) \
88
+ (interp)->imports.lock.level
89
+
90
+ #define FIND_AND_LOAD (interp ) \
91
+ (interp)->imports.find_and_load
92
+
88
93
89
94
/*******************/
90
95
/* the import lock */
@@ -95,45 +100,45 @@ static struct _inittab *inittab_copy = NULL;
95
100
These calls are serialized by the global interpreter lock. */
96
101
97
102
void
98
- _PyImport_AcquireLock (void )
103
+ _PyImport_AcquireLock (PyInterpreterState * interp )
99
104
{
100
105
unsigned long me = PyThread_get_thread_ident ();
101
106
if (me == PYTHREAD_INVALID_THREAD_ID )
102
107
return ; /* Too bad */
103
- if (import_lock == NULL ) {
104
- import_lock = PyThread_allocate_lock ();
105
- if (import_lock == NULL )
108
+ if (IMPORT_LOCK ( interp ) == NULL ) {
109
+ IMPORT_LOCK ( interp ) = PyThread_allocate_lock ();
110
+ if (IMPORT_LOCK ( interp ) == NULL )
106
111
return ; /* Nothing much we can do. */
107
112
}
108
- if (import_lock_thread == me ) {
109
- import_lock_level ++ ;
113
+ if (IMPORT_LOCK_THREAD ( interp ) == me ) {
114
+ IMPORT_LOCK_LEVEL ( interp ) ++ ;
110
115
return ;
111
116
}
112
- if (import_lock_thread != PYTHREAD_INVALID_THREAD_ID ||
113
- !PyThread_acquire_lock (import_lock , 0 ))
117
+ if (IMPORT_LOCK_THREAD ( interp ) != PYTHREAD_INVALID_THREAD_ID ||
118
+ !PyThread_acquire_lock (IMPORT_LOCK ( interp ) , 0 ))
114
119
{
115
120
PyThreadState * tstate = PyEval_SaveThread ();
116
- PyThread_acquire_lock (import_lock , WAIT_LOCK );
121
+ PyThread_acquire_lock (IMPORT_LOCK ( interp ) , WAIT_LOCK );
117
122
PyEval_RestoreThread (tstate );
118
123
}
119
- assert (import_lock_level == 0 );
120
- import_lock_thread = me ;
121
- import_lock_level = 1 ;
124
+ assert (IMPORT_LOCK_LEVEL ( interp ) == 0 );
125
+ IMPORT_LOCK_THREAD ( interp ) = me ;
126
+ IMPORT_LOCK_LEVEL ( interp ) = 1 ;
122
127
}
123
128
124
129
int
125
- _PyImport_ReleaseLock (void )
130
+ _PyImport_ReleaseLock (PyInterpreterState * interp )
126
131
{
127
132
unsigned long me = PyThread_get_thread_ident ();
128
- if (me == PYTHREAD_INVALID_THREAD_ID || import_lock == NULL )
133
+ if (me == PYTHREAD_INVALID_THREAD_ID || IMPORT_LOCK ( interp ) == NULL )
129
134
return 0 ; /* Too bad */
130
- if (import_lock_thread != me )
135
+ if (IMPORT_LOCK_THREAD ( interp ) != me )
131
136
return -1 ;
132
- import_lock_level -- ;
133
- assert (import_lock_level >= 0 );
134
- if (import_lock_level == 0 ) {
135
- import_lock_thread = PYTHREAD_INVALID_THREAD_ID ;
136
- PyThread_release_lock (import_lock );
137
+ IMPORT_LOCK_LEVEL ( interp ) -- ;
138
+ assert (IMPORT_LOCK_LEVEL ( interp ) >= 0 );
139
+ if (IMPORT_LOCK_LEVEL ( interp ) == 0 ) {
140
+ IMPORT_LOCK_THREAD ( interp ) = PYTHREAD_INVALID_THREAD_ID ;
141
+ PyThread_release_lock (IMPORT_LOCK ( interp ) );
137
142
}
138
143
return 1 ;
139
144
}
@@ -144,23 +149,23 @@ _PyImport_ReleaseLock(void)
144
149
We now acquire the import lock around fork() calls but on some platforms
145
150
(Solaris 9 and earlier? see isue7242) that still left us with problems. */
146
151
PyStatus
147
- _PyImport_ReInitLock (void )
152
+ _PyImport_ReInitLock (PyInterpreterState * interp )
148
153
{
149
- if (import_lock != NULL ) {
150
- if (_PyThread_at_fork_reinit (& import_lock ) < 0 ) {
154
+ if (IMPORT_LOCK ( interp ) != NULL ) {
155
+ if (_PyThread_at_fork_reinit (& IMPORT_LOCK ( interp ) ) < 0 ) {
151
156
return _PyStatus_ERR ("failed to create a new lock" );
152
157
}
153
158
}
154
159
155
- if (import_lock_level > 1 ) {
160
+ if (IMPORT_LOCK_LEVEL ( interp ) > 1 ) {
156
161
/* Forked as a side effect of import */
157
162
unsigned long me = PyThread_get_thread_ident ();
158
- PyThread_acquire_lock (import_lock , WAIT_LOCK );
159
- import_lock_thread = me ;
160
- import_lock_level -- ;
163
+ PyThread_acquire_lock (IMPORT_LOCK ( interp ) , WAIT_LOCK );
164
+ IMPORT_LOCK_THREAD ( interp ) = me ;
165
+ IMPORT_LOCK_LEVEL ( interp ) -- ;
161
166
} else {
162
- import_lock_thread = PYTHREAD_INVALID_THREAD_ID ;
163
- import_lock_level = 0 ;
167
+ IMPORT_LOCK_THREAD ( interp ) = PYTHREAD_INVALID_THREAD_ID ;
168
+ IMPORT_LOCK_LEVEL ( interp ) = 0 ;
164
169
}
165
170
return _PyStatus_OK ();
166
171
}
@@ -2506,8 +2511,8 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
2506
2511
PyObject * mod = NULL ;
2507
2512
PyInterpreterState * interp = tstate -> interp ;
2508
2513
int import_time = _PyInterpreterState_GetConfig (interp )-> import_time ;
2509
- #define import_level FIND_AND_LOAD.import_level
2510
- #define accumulated FIND_AND_LOAD.accumulated
2514
+ #define import_level FIND_AND_LOAD(interp) .import_level
2515
+ #define accumulated FIND_AND_LOAD(interp) .accumulated
2511
2516
2512
2517 _PyTime_t t1 = 0 , accumulated_copy = accumulated ;
2513
2518
@@ -2528,7 +2533,7 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
2528
2533
* _PyDict_GetItemIdWithError().
2529
2534
*/
2530
2535
if (import_time ) {
2531
- #define header FIND_AND_LOAD.header
2536
+ #define header FIND_AND_LOAD(interp) .header
2532
2537
if (header ) {
2533
2538
fputs ("import time: self [us] | cumulative | imported package\n" ,
2534
2539
stderr );
@@ -2867,10 +2872,6 @@ _PyImport_Fini(void)
2867
2872
{
2868
2873
/* Destroy the database used by _PyImport_{Fixup,Find}Extension */
2869
2874
_extensions_cache_clear_all ();
2870
- if (import_lock != NULL ) {
2871
- PyThread_free_lock (import_lock );
2872
- import_lock = NULL ;
2873
- }
2874
2875
2875
2876
/* Use the same memory allocator as _PyImport_Init(). */
2876
2877
PyMemAllocatorEx old_alloc ;
@@ -2959,6 +2960,11 @@ _PyImport_FiniCore(PyInterpreterState *interp)
2959
2960
PyErr_WriteUnraisable (NULL );
2960
2961
}
2961
2962
2963
+ if (IMPORT_LOCK (interp ) != NULL ) {
2964
+ PyThread_free_lock (IMPORT_LOCK (interp ));
2965
+ IMPORT_LOCK (interp ) = NULL ;
2966
+ }
2967
+
2962
2968
_PyImport_ClearCore (interp );
2963
2969
}
2964
2970
@@ -3090,7 +3096,9 @@ static PyObject *
3090
3096
_imp_lock_held_impl (PyObject * module )
3091
3097
/*[clinic end generated code: output=8b89384b5e1963fc input=9b088f9b217d9bdf]*/
3092
3098
{
3093
- return PyBool_FromLong (import_lock_thread != PYTHREAD_INVALID_THREAD_ID );
3099
+ PyInterpreterState * interp = _PyInterpreterState_GET ();
3100
+ return PyBool_FromLong (
3101
+ IMPORT_LOCK_THREAD (interp ) != PYTHREAD_INVALID_THREAD_ID );
3094
3102
}
3095
3103
3096
3104
/*[clinic input]
@@ -3106,7 +3114,8 @@ static PyObject *
3106
3114
_imp_acquire_lock_impl (PyObject * module )
3107
3115
/*[clinic end generated code: output=1aff58cb0ee1b026 input=4a2d4381866d5fdc]*/
3108
3116
{
3109
- _PyImport_AcquireLock ();
3117
+ PyInterpreterState * interp = _PyInterpreterState_GET ();
3118
+ _PyImport_AcquireLock (interp );
3110
3119
Py_RETURN_NONE ;
3111
3120
}
3112
3121
@@ -3122,7 +3131,8 @@ static PyObject *
3122
3131
_imp_release_lock_impl (PyObject * module )
3123
3132
/*[clinic end generated code: output=7faab6d0be178b0a input=934fb11516dd778b]*/
3124
3133
{
3125
- if (_PyImport_ReleaseLock () < 0 ) {
3134
+ PyInterpreterState * interp = _PyInterpreterState_GET ();
3135
+ if (_PyImport_ReleaseLock (interp ) < 0 ) {
3126
3136
PyErr_SetString (PyExc_RuntimeError ,
3127
3137
"not holding the import lock" );
3128
3138
return NULL ;
0 commit comments