Skip to content

pthread_mutex_lock stuck when acquiring a locked mutex #124

@louisom

Description

@louisom

assume ping and pong thread was create, and have following code.

ping_thread

__USER_TEXT
void *ping_thread(void *arg)
{
    pthread_mutex_lock(&p);
        printf("PING...\n");

    value = 10;
    L4_Sleep(L4_TimePeriod(1000 * 1000));
    printf("I'm a sleepy man\n");
    pthread_mutex_unlock(&p);
    return NULL;
}

pong_thread

__USER_TEXT
void *pong_thread(void *arg)
{
    pthread_mutex_lock(&p);
    value = 20;
        printf("PONG...\n");
        pthread_mutex_unlock(&p);
    return NULL;
}

main

pager_start_thread(threads[PING_THREAD], ping_thread, NULL);
pager_start_thread(threads[PONG_THREAD], pong_thread, NULL);

we will get this result, after pong acquire mutex, it stuck in busy loop.

====================================================
 Copyright(C) 2013-2014 The F9 Microkernel Project  
====================================================
Git head: 58348872e98a7f833cd07690b086d7fa5abd8151
Host: x86_64
Build: 2016-10-04T21:50:39+0800

Press '?' to print KDB menu
PING: 10 1


## KDB ##

but if I add sleep for test, it work again.

__USER_TEXT int pthread_mutex_lock(pthread_mutex_t *mutex)
{
    /* Busy trying */
    while (pthread_mutex_trylock(mutex)) {
        L4_Sleep(L4_TimePeriod(1000));
    }

    return 0;
}

result

====================================================
 Copyright(C) 2013-2014 The F9 Microkernel Project  
====================================================
Git head: 58348872e98a7f833cd07690b086d7fa5abd8151
Host: x86_64
Build: 2016-10-04T21:50:39+0800

Press '?' to print KDB menu
PING: 10 1
I'm a sleepy man
PONG: 20 1


## KDB ##

I'll try to fix this. or maybe this just a recursive lock?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions