AmigaLabs / clib4

Clib4 for AmigaOS4
Other
14 stars 7 forks source link

pthread_mutex_lock and MutexAttempt causing Deadlock #182

Closed 3246251196 closed 4 months ago

3246251196 commented 4 months ago

PTHREAD_MUTEX_ERRORCHECK.zip The code above are snippets from Mednafen.

While porting Mednafen I noticed that I was getting a deadlock situation. I think the following:

    if (mutex->kind == PTHREAD_MUTEX_ERRORCHECK) {
        SHOWMSG("MutexAttempt");
        BOOL isLocked = MutexAttempt(mutex->mutex);
        if (!isLocked) {
            SHOWMSG("DeadLock");
            return EDEADLK;
        }
    }

MutexAttempt (ExecSG):

*   NAME
*   MutexAttempt -- Try to lock a mutex                              (V52)
*
*   SYNOPSIS
*   BOOL MutexAttempt(APTR mx);
*
*   FUNCTION
*   This function attempts to lock the mutex mx. This function will never

It looks like we are locking the mutex, checking that it was not locked and then, because of that, trying to lock it again. Although, I can see the idea for MutexAttempt is to see whether or not we can get the mutex and if it already is locked the idea would be to check !isLocked and return EDEADLK - but that is not happening in the test case. But if it does lock then we need to handle that and not try to lock it again.

3246251196 commented 4 months ago

Resolved.