vsync / spinlock / semaphore.h
Counting Semaphore.
Example:
#include <vsync/spinlock/semaphore.h>
#include <vsync/common/assert.h>
#include <pthread.h>
#include <stdio.h>
#define N 12
#define EXPECTED_VAL N
#define NUM_RESOURCES 1
semaphore_t g_semaphore = SEMAPHORE_INIT(NUM_RESOURCES);
vuint32_t g_x = 0;
vuint32_t g_y = 0;
void *
run(void *args)
{
semaphore_acquire(&g_semaphore, NUM_RESOURCES);
g_x++;
g_y++;
semaphore_release(&g_semaphore, NUM_RESOURCES);
(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;
}
Macros
| Macro | Description |
|---|---|
| SEMAPHORE_INIT | Initializer of semaphore_t with n resources. |
Macro SEMAPHORE_INIT
SEMAPHORE_INIT(n)
Initializer of semaphore_t with n resources.
Functions
| Function | Description |
|---|---|
| semaphore_init | Initializes the semaphore. |
| semaphore_acquire | Acquires i resources of the semaphore if available. |
| semaphore_tryacquire | Tries to acquire i resources of the semaphore if available. |
| semaphore_release | Releases i resources of the semaphore. |
Function semaphore_init
static void semaphore_init(semaphore_t *s, vuint32_t n)
Initializes the semaphore.
Parameters:
s: address of semaphore_t object.n: number of resources.
Note: alternatively use
SEMAPHORE_INIT
Function semaphore_acquire
static void semaphore_acquire(semaphore_t *s, vuint32_t i)
Acquires i resources of the semaphore if available.
other blocks until enough resources are available in the semaphore.
Parameters:
s: address of semaphore_t object.i: number of resources to acquire.
Precondition: call semaphore_acquire with i <= N.
Function semaphore_tryacquire
static vbool_t semaphore_tryacquire(semaphore_t *s, vuint32_t i)
Tries to acquire i resources of the semaphore if available.
Parameters:
s: address of semaphore_t object.i: number of resources to acquire.
Returns: true, if i resources were acquired.
Returns: false, if failed to acquire i resources.
Precondition: call semaphore_tryacquire with i <= N.
Function semaphore_release
static void semaphore_release(semaphore_t *s, vuint32_t i)
Releases i resources of the semaphore.
Parameters:
s: address of semaphore_t object.i: number of resources to release.