vsync / spinlock / ttaslock.h
Test, Test and Set lock.
Example:
#include <vsync/spinlock/ttaslock.h>
#include <vsync/common/assert.h>
#include <pthread.h>
#include <stdio.h>
#define N 12
#define EXPECTED_VAL N
ttaslock_t g_lock = TTASLOCK_INIT();
vuint32_t g_x = 0;
vuint32_t g_y = 0;
void *
run(void *args)
{
ttaslock_acquire(&g_lock);
g_x++;
g_y++;
ttaslock_release(&g_lock);
(void)args;
return NULL;
}
int
main(void)
{
pthread_t threads[N];
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;
}
References:
Maurice Herlihy, Nir Shavit - The Art of Multiprocessor Programming 7.3
Macros
| Macro | Description |
|---|---|
| TTASLOCK_INIT | Initializer of ttaslock_t. |
Macro TTASLOCK_INIT
TTASLOCK_INIT()
Initializer of ttaslock_t.
Functions
| Function | Description |
|---|---|
| ttaslock_init | Initializes the TTAS lock. |
| ttaslock_acquire | Acquires the TAS lock. |
| ttaslock_tryacquire | Tries to acquire the TTAS lock. |
| ttaslock_release | Releases the TTAS lock. |
Function ttaslock_init
static void ttaslock_init(ttaslock_t *l)
Initializes the TTAS lock.
Parameters:
l: address of ttaslock_t object.
Note: alternatively use
TTASLOCK_INIT.
Function ttaslock_acquire
static void ttaslock_acquire(ttaslock_t *l)
Acquires the TAS lock.
Parameters:
l: address of ttaslock_t object.
Function ttaslock_tryacquire
static vbool_t ttaslock_tryacquire(ttaslock_t *l)
Tries to acquire the TTAS lock.
Parameters:
l: address of ttaslock_t object.
Returns: true, if lock is acquired successfully.
Returns: false, if failed to acquire the lock.
Function ttaslock_release
static void ttaslock_release(ttaslock_t *l)
Releases the TTAS lock.
Parameters:
l: address of ttaslock_t object.