@@ -82,11 +82,7 @@ static struct _inittab *inittab_copy = NULL;
8282 (interp)->imports.import_func
8383
8484#define IMPORT_LOCK (interp ) \
85- (interp)->imports.lock.mutex
86- #define IMPORT_LOCK_THREAD (interp ) \
87- (interp)->imports.lock.thread
88- #define IMPORT_LOCK_LEVEL (interp ) \
89- (interp)->imports.lock.level
85+ (interp)->imports.lock
9086
9187#define FIND_AND_LOAD (interp ) \
9288 (interp)->imports.find_and_load
@@ -103,74 +99,14 @@ static struct _inittab *inittab_copy = NULL;
10399void
104100_PyImport_AcquireLock (PyInterpreterState * interp )
105101{
106- unsigned long me = PyThread_get_thread_ident ();
107- if (me == PYTHREAD_INVALID_THREAD_ID )
108- return ; /* Too bad */
109- if (IMPORT_LOCK (interp ) == NULL ) {
110- IMPORT_LOCK (interp ) = PyThread_allocate_lock ();
111- if (IMPORT_LOCK (interp ) == NULL )
112- return ; /* Nothing much we can do. */
113- }
114- if (IMPORT_LOCK_THREAD (interp ) == me ) {
115- IMPORT_LOCK_LEVEL (interp )++ ;
116- return ;
117- }
118- if (IMPORT_LOCK_THREAD (interp ) != PYTHREAD_INVALID_THREAD_ID ||
119- !PyThread_acquire_lock (IMPORT_LOCK (interp ), 0 ))
120- {
121- PyThreadState * tstate = PyEval_SaveThread ();
122- PyThread_acquire_lock (IMPORT_LOCK (interp ), WAIT_LOCK );
123- PyEval_RestoreThread (tstate );
124- }
125- assert (IMPORT_LOCK_LEVEL (interp ) == 0 );
126- IMPORT_LOCK_THREAD (interp ) = me ;
127- IMPORT_LOCK_LEVEL (interp ) = 1 ;
102+ _PyRecursiveMutex_Lock (& IMPORT_LOCK (interp ));
128103}
129104
130- int
105+ void
131106_PyImport_ReleaseLock (PyInterpreterState * interp )
132107{
133- unsigned long me = PyThread_get_thread_ident ();
134- if (me == PYTHREAD_INVALID_THREAD_ID || IMPORT_LOCK (interp ) == NULL )
135- return 0 ; /* Too bad */
136- if (IMPORT_LOCK_THREAD (interp ) != me )
137- return -1 ;
138- IMPORT_LOCK_LEVEL (interp )-- ;
139- assert (IMPORT_LOCK_LEVEL (interp ) >= 0 );
140- if (IMPORT_LOCK_LEVEL (interp ) == 0 ) {
141- IMPORT_LOCK_THREAD (interp ) = PYTHREAD_INVALID_THREAD_ID ;
142- PyThread_release_lock (IMPORT_LOCK (interp ));
143- }
144- return 1 ;
145- }
146-
147- #ifdef HAVE_FORK
148- /* This function is called from PyOS_AfterFork_Child() to ensure that newly
149- created child processes do not share locks with the parent.
150- We now acquire the import lock around fork() calls but on some platforms
151- (Solaris 9 and earlier? see isue7242) that still left us with problems. */
152- PyStatus
153- _PyImport_ReInitLock (PyInterpreterState * interp )
154- {
155- if (IMPORT_LOCK (interp ) != NULL ) {
156- if (_PyThread_at_fork_reinit (& IMPORT_LOCK (interp )) < 0 ) {
157- return _PyStatus_ERR ("failed to create a new lock" );
158- }
159- }
160-
161- if (IMPORT_LOCK_LEVEL (interp ) > 1 ) {
162- /* Forked as a side effect of import */
163- unsigned long me = PyThread_get_thread_ident ();
164- PyThread_acquire_lock (IMPORT_LOCK (interp ), WAIT_LOCK );
165- IMPORT_LOCK_THREAD (interp ) = me ;
166- IMPORT_LOCK_LEVEL (interp )-- ;
167- } else {
168- IMPORT_LOCK_THREAD (interp ) = PYTHREAD_INVALID_THREAD_ID ;
169- IMPORT_LOCK_LEVEL (interp ) = 0 ;
170- }
171- return _PyStatus_OK ();
108+ _PyRecursiveMutex_Unlock (& IMPORT_LOCK (interp ));
172109}
173- #endif
174110
175111
176112/***************/
@@ -3446,11 +3382,6 @@ _PyImport_FiniCore(PyInterpreterState *interp)
34463382 PyErr_FormatUnraisable ("Exception ignored on clearing sys.modules" );
34473383 }
34483384
3449- if (IMPORT_LOCK (interp ) != NULL ) {
3450- PyThread_free_lock (IMPORT_LOCK (interp ));
3451- IMPORT_LOCK (interp ) = NULL ;
3452- }
3453-
34543385 _PyImport_ClearCore (interp );
34553386}
34563387
@@ -3583,8 +3514,7 @@ _imp_lock_held_impl(PyObject *module)
35833514/*[clinic end generated code: output=8b89384b5e1963fc input=9b088f9b217d9bdf]*/
35843515{
35853516 PyInterpreterState * interp = _PyInterpreterState_GET ();
3586- return PyBool_FromLong (
3587- IMPORT_LOCK_THREAD (interp ) != PYTHREAD_INVALID_THREAD_ID );
3517+ return PyBool_FromLong (PyMutex_IsLocked (& IMPORT_LOCK (interp ).mutex ));
35883518}
35893519
35903520/*[clinic input]
@@ -3618,11 +3548,12 @@ _imp_release_lock_impl(PyObject *module)
36183548/*[clinic end generated code: output=7faab6d0be178b0a input=934fb11516dd778b]*/
36193549{
36203550 PyInterpreterState * interp = _PyInterpreterState_GET ();
3621- if (_PyImport_ReleaseLock ( interp ) < 0 ) {
3551+ if (! _PyRecursiveMutex_IsLockedByCurrentThread ( & IMPORT_LOCK ( interp )) ) {
36223552 PyErr_SetString (PyExc_RuntimeError ,
36233553 "not holding the import lock" );
36243554 return NULL ;
36253555 }
3556+ _PyImport_ReleaseLock (interp );
36263557 Py_RETURN_NONE ;
36273558}
36283559
0 commit comments