@@ -56,10 +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
59
#define FIND_AND_LOAD _PyRuntime.imports.find_and_load
64
60
#define PKGCONTEXT (_PyRuntime.imports.pkgcontext)
65
61
@@ -83,6 +79,13 @@ static struct _inittab *inittab_copy = NULL;
83
79
#define IMPORT_FUNC (interp ) \
84
80
(interp)->imports.import_func
85
81
82
+ #define IMPORT_LOCK (interp ) \
83
+ (interp)->imports.lock.mutex
84
+ #define IMPORT_LOCK_THREAD (interp ) \
85
+ (interp)->imports.lock.thread
86
+ #define IMPORT_LOCK_LEVEL (interp ) \
87
+ (interp)->imports.lock.level
88
+
86
89
87
90
/*******************/
88
91
/* the import lock */
@@ -93,45 +96,45 @@ static struct _inittab *inittab_copy = NULL;
93
96
These calls are serialized by the global interpreter lock. */
94
97
95
98
void
96
- _PyImport_AcquireLock (void )
99
+ _PyImport_AcquireLock (PyInterpreterState * interp )
97
100
{
98
101
unsigned long me = PyThread_get_thread_ident ();
99
102
if (me == PYTHREAD_INVALID_THREAD_ID )
100
103
return ; /* Too bad */
101
- if (import_lock == NULL ) {
102
- import_lock = PyThread_allocate_lock ();
103
- if (import_lock == NULL )
104
+ if (IMPORT_LOCK ( interp ) == NULL ) {
105
+ IMPORT_LOCK ( interp ) = PyThread_allocate_lock ();
106
+ if (IMPORT_LOCK ( interp ) == NULL )
104
107
return ; /* Nothing much we can do. */
105
108
}
106
- if (import_lock_thread == me ) {
107
- import_lock_level ++ ;
109
+ if (IMPORT_LOCK_THREAD ( interp ) == me ) {
110
+ IMPORT_LOCK_LEVEL ( interp ) ++ ;
108
111
return ;
109
112
}
110
- if (import_lock_thread != PYTHREAD_INVALID_THREAD_ID ||
111
- !PyThread_acquire_lock (import_lock , 0 ))
113
+ if (IMPORT_LOCK_THREAD ( interp ) != PYTHREAD_INVALID_THREAD_ID ||
114
+ !PyThread_acquire_lock (IMPORT_LOCK ( interp ) , 0 ))
112
115
{
113
116
PyThreadState * tstate = PyEval_SaveThread ();
114
- PyThread_acquire_lock (import_lock , WAIT_LOCK );
117
+ PyThread_acquire_lock (IMPORT_LOCK ( interp ) , WAIT_LOCK );
115
118
PyEval_RestoreThread (tstate );
116
119
}
117
- assert (import_lock_level == 0 );
118
- import_lock_thread = me ;
119
- import_lock_level = 1 ;
120
+ assert (IMPORT_LOCK_LEVEL ( interp ) == 0 );
121
+ IMPORT_LOCK_THREAD ( interp ) = me ;
122
+ IMPORT_LOCK_LEVEL ( interp ) = 1 ;
120
123
}
121
124
122
125
int
123
- _PyImport_ReleaseLock (void )
126
+ _PyImport_ReleaseLock (PyInterpreterState * interp )
124
127
{
125
128
unsigned long me = PyThread_get_thread_ident ();
126
- if (me == PYTHREAD_INVALID_THREAD_ID || import_lock == NULL )
129
+ if (me == PYTHREAD_INVALID_THREAD_ID || IMPORT_LOCK ( interp ) == NULL )
127
130
return 0 ; /* Too bad */
128
- if (import_lock_thread != me )
131
+ if (IMPORT_LOCK_THREAD ( interp ) != me )
129
132
return -1 ;
130
- import_lock_level -- ;
131
- assert (import_lock_level >= 0 );
132
- if (import_lock_level == 0 ) {
133
- import_lock_thread = PYTHREAD_INVALID_THREAD_ID ;
134
- PyThread_release_lock (import_lock );
133
+ IMPORT_LOCK_LEVEL ( interp ) -- ;
134
+ assert (IMPORT_LOCK_LEVEL ( interp ) >= 0 );
135
+ if (IMPORT_LOCK_LEVEL ( interp ) == 0 ) {
136
+ IMPORT_LOCK_THREAD ( interp ) = PYTHREAD_INVALID_THREAD_ID ;
137
+ PyThread_release_lock (IMPORT_LOCK ( interp ) );
135
138
}
136
139
return 1 ;
137
140
}
@@ -142,23 +145,23 @@ _PyImport_ReleaseLock(void)
142
145
We now acquire the import lock around fork() calls but on some platforms
143
146
(Solaris 9 and earlier? see isue7242) that still left us with problems. */
144
147
PyStatus
145
- _PyImport_ReInitLock (void )
148
+ _PyImport_ReInitLock (PyInterpreterState * interp )
146
149
{
147
- if (import_lock != NULL ) {
148
- if (_PyThread_at_fork_reinit (& import_lock ) < 0 ) {
150
+ if (IMPORT_LOCK ( interp ) != NULL ) {
151
+ if (_PyThread_at_fork_reinit (& IMPORT_LOCK ( interp ) ) < 0 ) {
149
152
return _PyStatus_ERR ("failed to create a new lock" );
150
153
}
151
154
}
152
155
153
- if (import_lock_level > 1 ) {
156
+ if (IMPORT_LOCK_LEVEL ( interp ) > 1 ) {
154
157
/* Forked as a side effect of import */
155
158
unsigned long me = PyThread_get_thread_ident ();
156
- PyThread_acquire_lock (import_lock , WAIT_LOCK );
157
- import_lock_thread = me ;
158
- import_lock_level -- ;
159
+ PyThread_acquire_lock (IMPORT_LOCK ( interp ) , WAIT_LOCK );
160
+ IMPORT_LOCK_THREAD ( interp ) = me ;
161
+ IMPORT_LOCK_LEVEL ( interp ) -- ;
159
162
} else {
160
- import_lock_thread = PYTHREAD_INVALID_THREAD_ID ;
161
- import_lock_level = 0 ;
163
+ IMPORT_LOCK_THREAD ( interp ) = PYTHREAD_INVALID_THREAD_ID ;
164
+ IMPORT_LOCK_LEVEL ( interp ) = 0 ;
162
165
}
163
166
return _PyStatus_OK ();
164
167
}
@@ -2634,10 +2637,6 @@ _PyImport_Fini(void)
2634
2637
{
2635
2638
/* Destroy the database used by _PyImport_{Fixup,Find}Extension */
2636
2639
_extensions_cache_clear ();
2637
- if (import_lock != NULL ) {
2638
- PyThread_free_lock (import_lock );
2639
- import_lock = NULL ;
2640
- }
2641
2640
2642
2641
/* Use the same memory allocator as _PyImport_Init(). */
2643
2642
PyMemAllocatorEx old_alloc ;
@@ -2726,6 +2725,11 @@ _PyImport_FiniCore(PyInterpreterState *interp)
2726
2725
PyErr_WriteUnraisable (NULL );
2727
2726
}
2728
2727
2728
+ if (IMPORT_LOCK (interp ) != NULL ) {
2729
+ PyThread_free_lock (IMPORT_LOCK (interp ));
2730
+ IMPORT_LOCK (interp ) = NULL ;
2731
+ }
2732
+
2729
2733
_PyImport_ClearCore (interp );
2730
2734
}
2731
2735
@@ -2857,7 +2861,9 @@ static PyObject *
2857
2861
_imp_lock_held_impl (PyObject * module )
2858
2862
/*[clinic end generated code: output=8b89384b5e1963fc input=9b088f9b217d9bdf]*/
2859
2863
{
2860
- return PyBool_FromLong (import_lock_thread != PYTHREAD_INVALID_THREAD_ID );
2864
+ PyInterpreterState * interp = _PyInterpreterState_GET ();
2865
+ return PyBool_FromLong (
2866
+ IMPORT_LOCK_THREAD (interp ) != PYTHREAD_INVALID_THREAD_ID );
2861
2867
}
2862
2868
2863
2869
/*[clinic input]
@@ -2873,7 +2879,8 @@ static PyObject *
2873
2879
_imp_acquire_lock_impl (PyObject * module )
2874
2880
/*[clinic end generated code: output=1aff58cb0ee1b026 input=4a2d4381866d5fdc]*/
2875
2881
{
2876
- _PyImport_AcquireLock ();
2882
+ PyInterpreterState * interp = _PyInterpreterState_GET ();
2883
+ _PyImport_AcquireLock (interp );
2877
2884
Py_RETURN_NONE ;
2878
2885
}
2879
2886
@@ -2889,7 +2896,8 @@ static PyObject *
2889
2896
_imp_release_lock_impl (PyObject * module )
2890
2897
/*[clinic end generated code: output=7faab6d0be178b0a input=934fb11516dd778b]*/
2891
2898
{
2892
- if (_PyImport_ReleaseLock () < 0 ) {
2899
+ PyInterpreterState * interp = _PyInterpreterState_GET ();
2900
+ if (_PyImport_ReleaseLock (interp ) < 0 ) {
2893
2901
PyErr_SetString (PyExc_RuntimeError ,
2894
2902
"not holding the import lock" );
2895
2903
return NULL ;
0 commit comments