Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Avoid blocking in pthread_mutex_lock() when PyThread_acquire_lock() is asked
not to block.
46 changes: 28 additions & 18 deletions Python/thread_pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,31 +410,41 @@ PyThread_free_lock(PyThread_type_lock lock)
int
PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
{
int success;
int success = 0;
pthread_lock *thelock = (pthread_lock *)lock;
int status, error = 0;

dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));

status = pthread_mutex_lock( &thelock->mut );
CHECK_STATUS("pthread_mutex_lock[1]");
success = thelock->locked == 0;

if ( !success && waitflag ) {
/* continue trying until we get the lock */

/* mut must be locked by me -- part of the condition
* protocol */
while ( thelock->locked ) {
status = pthread_cond_wait(&thelock->lock_released,
&thelock->mut);
CHECK_STATUS("pthread_cond_wait");
if (waitflag) {
status = pthread_mutex_lock( &thelock->mut );
CHECK_STATUS("pthread_mutex_lock[1]");
}
else {
status = pthread_mutex_trylock( &thelock->mut );
if (status != EBUSY)
CHECK_STATUS("pthread_mutex_trylock[1]");
}
if (status == 0) {
success = thelock->locked == 0;

if ( !success && waitflag ) {
/* continue trying until we get the lock */

/* mut must be locked by me -- part of the condition
* protocol */
while ( thelock->locked ) {
status = pthread_cond_wait(&thelock->lock_released,
&thelock->mut);
CHECK_STATUS("pthread_cond_wait");
}
success = 1;
}
success = 1;

if (success) thelock->locked = 1;
status = pthread_mutex_unlock( &thelock->mut );
CHECK_STATUS("pthread_mutex_unlock[1]");
}
if (success) thelock->locked = 1;
status = pthread_mutex_unlock( &thelock->mut );
CHECK_STATUS("pthread_mutex_unlock[1]");

if (error) success = 0;
dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Expand Down