Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 187aa54

Browse files
author
Kristján Valur Jónsson
committed
Signal condition variables with the mutex held. Destroy condition variables
before their mutexes.
1 parent 902274e commit 187aa54

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

Python/ceval_gil.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,14 @@ static void create_gil(void)
313313

314314
static void destroy_gil(void)
315315
{
316-
MUTEX_FINI(gil_mutex);
317-
#ifdef FORCE_SWITCHING
318-
MUTEX_FINI(switch_mutex);
319-
#endif
316+
/* some pthread-like implementations tie the mutex to the cond
317+
* and must have the cond destroyed first.
318+
*/
320319
COND_FINI(gil_cond);
320+
MUTEX_FINI(gil_mutex);
321321
#ifdef FORCE_SWITCHING
322322
COND_FINI(switch_cond);
323+
MUTEX_FINI(switch_mutex);
323324
#endif
324325
_Py_atomic_store_explicit(&gil_locked, -1, _Py_memory_order_release);
325326
_Py_ANNOTATE_RWLOCK_DESTROY(&gil_locked);

Python/thread_pthread.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,15 @@ PyThread_free_lock(PyThread_type_lock lock)
443443

444444
dprintf(("PyThread_free_lock(%p) called\n", lock));
445445

446-
status = pthread_mutex_destroy( &thelock->mut );
447-
CHECK_STATUS("pthread_mutex_destroy");
448-
446+
/* some pthread-like implementations tie the mutex to the cond
447+
* and must have the cond destroyed first.
448+
*/
449449
status = pthread_cond_destroy( &thelock->lock_released );
450450
CHECK_STATUS("pthread_cond_destroy");
451451

452+
status = pthread_mutex_destroy( &thelock->mut );
453+
CHECK_STATUS("pthread_mutex_destroy");
454+
452455
free((void *)thelock);
453456
}
454457

@@ -531,12 +534,12 @@ PyThread_release_lock(PyThread_type_lock lock)
531534

532535
thelock->locked = 0;
533536

534-
status = pthread_mutex_unlock( &thelock->mut );
535-
CHECK_STATUS("pthread_mutex_unlock[3]");
536-
537537
/* wake up someone (anyone, if any) waiting on the lock */
538538
status = pthread_cond_signal( &thelock->lock_released );
539539
CHECK_STATUS("pthread_cond_signal");
540+
541+
status = pthread_mutex_unlock( &thelock->mut );
542+
CHECK_STATUS("pthread_mutex_unlock[3]");
540543
}
541544

542545
#endif /* USE_SEMAPHORES */

0 commit comments

Comments
 (0)