1

I was wondering if I could make a recursive mutex type on my own with a PTHREAD_MUTEX_ERRORCHECK mutex, this is the result:

typedef struct {
    pthread_mutex_t mutex;
    uint32_t deadlocks;
    pthread_t owner;
    BOOL isLocked;
} pthread_recursivemutex_t;

int pthread_recursivemutex_init(pthread_recursivemutex_t *mutex)
{
    int ret;
    pthread_mutexattr_t attr;

    mutex->deadlocks = 0;

    ret = pthread_mutexattr_init(&attr);

    if (ret != 0) {
        return ret;
    }

    (void)pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);

    ret = pthread_mutex_init(&mutex->mutex, &attr);

    (void)pthread_mutexattr_destroy(&attr);

    mutex->isLocked = FALSE;

    return ret;
}

void pthread_recursivemutex_lock(pthread_recursivemutex_t *mutex)
{
    int ret;
    BOOL locked;

    locked = mutex->isLocked;
    __sync_synchronize();

    if (locked == TRUE) {
        if (pthread_equal(pthread_self(), mutex->owner) == 0) {
            return;
        }
    }       

    ret = pthread_mutex_lock(&mutex->mutex);

    if (ret == 0) {
        mutex->deadlocks = 0;
        __sync_synchronize();
        mutex->isLocked = TRUE;
    } else if (ret == EDEADLK) {
        mutex->deadlocks += 1;
    }
}

void pthread_recursivemutex_unlock(pthread_recursivemutex_t *mutex)
{
    if (mutex->deadlocks == 0) {
        (void)pthread_mutex_unlock(&mutex->mutex);
        __sync_synchronize();
        mutex->isLocked = FALSE;
    } else {
        mutex->deadlocks -= 1;
    }
}

void pthread_recursivemutex_destroy(pthread_recursivemutex_t *mutex)
{
    (void)pthread_mutex_destroy(&mutex->mutex);
}

I found out that this type of recursive mutex is a lot faster than a mutex with the PTHREAD_MUTEX_RECURSIVE attribute:

iterations               : 1000000

pthread_mutex_t          : 71757 μSeconds
pthread_recursivemutex_t : 48583 μSeconds

Test code (each called 1000000 times):

void mutex_test()
{
    pthread_mutex_lock(&recursiveMutex);
    pthread_mutex_lock(&recursiveMutex);
    pthread_mutex_unlock(&recursiveMutex);
    pthread_mutex_unlock(&recursiveMutex);
}

void recursivemutex_test()
{
    pthread_recursivemutex_lock(&myMutex);
    pthread_recursivemutex_lock(&myMutex);
    pthread_recursivemutex_unlock(&myMutex);
    pthread_recursivemutex_unlock(&myMutex);
}

pthread_recursivemutex_t is almost twice as fast as pthread_mutex_t ?! But both behave the same way...?

Is this solution above safe?

jotik
  • 17,044
  • 13
  • 58
  • 123
Marco
  • 7,007
  • 2
  • 19
  • 49
  • they don't behave the same way - one of them is called recursive. – Karoly Horvath Apr 13 '14 at 12:45
  • I meant pthread_mutex_t (with recursion) and pthread_recursivemutex_t. Both do not deadlock the calling thread if locked twice. – Marco Apr 13 '14 at 12:47
  • possible duplicate of [My recursive mutex vs pthread\_mutex\_t (type: recursive) (repost, push)](http://stackoverflow.com/questions/24856103/my-recursive-mutex-vs-pthread-mutex-t-type-recursive-repost-push) – Marco Jul 21 '14 at 00:05

1 Answers1

2

Your mutex won't work: you don't check which thread is acquiring the lock.

You allow multiple threads to lock the same mutex.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176