vsync / thread / mutex / musl.h

A simplified version of the mutex algorithm in musl libc.

Example:

#include <vsync/thread/mutex.h>
#include <vsync/common/assert.h>
#include <pthread.h>
#include <stdio.h>

#define N            12
#define EXPECTED_VAL N

vmutex_t g_mutex;
vuint32_t g_x = 0;
vuint32_t g_y = 0;

void *
run(void *args)
{
    vmutex_acquire(&g_mutex);
    g_x++;
    g_y++;
    vmutex_release(&g_mutex);

    (void)args;
    return NULL;
}

int
main(void)
{
    pthread_t threads[N];

    vmutex_init(&g_mutex);

    for (vsize_t i = 0; i < N; i++) {
        pthread_create(&threads[i], NULL, run, (void *)i);
    }

    for (vsize_t i = 0; i < N; i++) {
        pthread_join(threads[i], NULL);
    }

    ASSERT(g_x == EXPECTED_VAL);
    ASSERT(g_x == g_y);
    printf("Final value %u\n", g_x);
    return 0;
}

Note: replace #include <vsync/thread/mutex.h> with #include <vsync/thread/mutex/musl.h> in the example above.

Note: on linux compile with -D_GNU_SOURCE.

References:

Check mutex implementation in libc

Note: It is the normal mutex without support for reentrance.


Macros

Macro Description
MUSL_MAX_SPIN times of spinning before going to the macro.

Macro MUSL_MAX_SPIN

times of spinning before going to the macro.

default value is 100, compile with -DMUSL_MAX_SPIN=N to overwrite the default.

Note: spinning is deactivated on verification.


Functions

Function Description
vmutex_init Initializes the mutex m.
vmutex_acquire Acquires the mutex m.
vmutex_release Releases the mutex m.

Function vmutex_init

static void vmutex_init(vmutex_t *m)

Initializes the mutex m.

Parameters:

Function vmutex_acquire

static void vmutex_acquire(vmutex_t *m)

Acquires the mutex m.

Parameters:

Function vmutex_release

static void vmutex_release(vmutex_t *m)

Releases the mutex m.

Parameters: