aboutsummaryrefslogtreecommitdiff
path: root/src/lib/core/semaphore.c
blob: 579479a4363f65231f9abce9f5496f23889f5f5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @file semaphore.c
*/
#include <stdio.h>
#include <fcntl.h>

#include "core_message.h"
#include "sem.h"
#include "utils.h"

struct Semaphore *semaphores[1000] = {0};
bool semaphore_handle_exit_ready = false;

void semaphore_handle_exit() {
    for (size_t i = 0; i < sizeof(semaphores) / sizeof(*semaphores); ++i) {
        if (semaphores[i]) {
            SYSDEBUG("%s", semaphores[i]->name);
            semaphore_destroy(semaphores[i]);
        }
    }
}

static void register_semaphore(struct Semaphore *s) {
    struct Semaphore **cur = semaphores;
    size_t i = 0;
    while (i < sizeof(semaphores) / sizeof(*semaphores) && cur != NULL) {
        cur++;
        i++;
    }
    cur = &s;
}

int semaphore_init(struct Semaphore *s, const char *name, const int value) {
#if defined(STASIS_OS_DARWIN)
    // see: sem_open(2)
    const size_t max_namelen = PSEMNAMLEN;
#else
    // see: sem_open(3)
    const size_t max_namelen = STASIS_NAME_MAX;
#endif
    snprintf(s->name, max_namelen, "/%s", name);
    s->sem = sem_open(s->name, O_CREAT, 0644, value);
    if (s->sem == SEM_FAILED) {
        return -1;
    }
    SYSDEBUG("%s", s->name);
    register_semaphore(s);
    if (!semaphore_handle_exit_ready) {
        atexit(semaphore_handle_exit);
    }

    return 0;
}

int semaphore_wait(struct Semaphore *s) {
    return sem_wait(s->sem);
}

int semaphore_post(struct Semaphore *s) {
    return sem_post(s->sem);
}

void semaphore_destroy(struct Semaphore *s) {
    if (!s) {
        SYSDEBUG("%s", "would have crashed");
        return;
    }
    SYSDEBUG("%s", s->name);
    sem_close(s->sem);
    sem_unlink(s->name);
}