Details

[Home]

Issue of the Implementation # S0501

Brief

The pthread_mutex_trylock() function returns EDEADLK instead of EBUSY

Detailed Description

Function pthread_mutex_trylock() tries to lock a mutex. It shall return EBUSY error code if the mutex could not be acquired because it was already locked. But it returns error code EDEADLK in glibc 2.4-2.5 if the mutex is owned by the current thread.

Problem location(s) in the standard

Linux Standard Base Core Specification 3.1, Chapter 13. Base Libraries, 13.10. Data Definitions for libpthread pthread_mutex_trylock() function

Example

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>

int printResAsErrno( int res, const char * funcWithArgs ) {
    printf( "%s is %d ( %s )\n", funcWithArgs, res, strerror( res ) );
    return res;
}

#define callPrintResAsErrno( funcWithPars ) printResAsErrno( funcWithPars, #funcWithPars )

int main( int argc, char ** argv ) {
    pthread_mutexattr_t attr;
    pthread_mutex_t mutex;
    // prepare attr and mutex
    callPrintResAsErrno( pthread_mutexattr_init( & attr )                              );
    callPrintResAsErrno( pthread_mutexattr_settype( & attr, PTHREAD_MUTEX_ERRORCHECK ) );
    callPrintResAsErrno( pthread_mutex_init( & mutex, & attr )                         );
    callPrintResAsErrno( pthread_mutex_lock( & mutex )                                 );
    // find error
    if ( callPrintResAsErrno( pthread_mutex_trylock( & mutex ) ) != EBUSY ) {
        // error found
        printf( "error : pthread_mutex_trylock( ... ) should be %d ('%s')\n", EBUSY, strerror( EBUSY ) );
    } else {
        // error not found
        printf( "no error\n" );
    }
    // destroy attr
    callPrintResAsErrno( pthread_mutexattr_destroy( & attr ) );
    // destroy mutex
    callPrintResAsErrno( pthread_mutex_unlock( & mutex )  );
    callPrintResAsErrno( pthread_mutex_destroy( & mutex ) );
    return 0;
}

Component

glibc 2.4-2.5

Status

Fixed in glibc 2.6

[Home]