1 | #include <semaphore.h> |
---|
2 | #include <fcntl.h> |
---|
3 | #include <sys/types.h> |
---|
4 | #include <sys/stat.h> |
---|
5 | #include <stdio.h> |
---|
6 | #include <stdlib.h> |
---|
7 | |
---|
8 | #include <kernel/mod2.h> |
---|
9 | |
---|
10 | #ifdef HAVE_SIMPLEIPC |
---|
11 | #include "simpleipc.h" |
---|
12 | |
---|
13 | // Not yet implemented: SYSV IPC Semaphores |
---|
14 | // They are more difficult to clean up after a process crash |
---|
15 | // but are supported more widely. |
---|
16 | |
---|
17 | static sem_t *semaphore[SIPC_MAX_SEMAPHORES]; |
---|
18 | |
---|
19 | /* return 1 on success, |
---|
20 | * 0 if already initialized, |
---|
21 | * -1 for errors |
---|
22 | */ |
---|
23 | int sipc_semaphore_init(int id, int count) |
---|
24 | { |
---|
25 | char buf[100]; |
---|
26 | sem_t *sem; |
---|
27 | if ((id<0) || (id >= SIPC_MAX_SEMAPHORES)) |
---|
28 | return -1; |
---|
29 | // Already initialized? |
---|
30 | if (semaphore[id]) |
---|
31 | return 0; |
---|
32 | // to make it completely safe, we should generate a name |
---|
33 | // from /dev/urandom. |
---|
34 | #if USE_SEM_INIT |
---|
35 | // TODO: This should really use mapped memory so that processes |
---|
36 | // can keep using the semaphore after fork + exec. |
---|
37 | sem = malloc(sizeof(sem_t)); |
---|
38 | if (!sem) |
---|
39 | return -1; |
---|
40 | if (sem_init(sem, 1, count) < 0) |
---|
41 | { |
---|
42 | free(sem); |
---|
43 | return -1; |
---|
44 | } |
---|
45 | #else |
---|
46 | sprintf(buf, "/%d:sem%d", getpid(), id); |
---|
47 | sem_unlink(buf); |
---|
48 | sem = sem_open(buf, O_CREAT, 0600, count); |
---|
49 | #endif |
---|
50 | if (sem == SEM_FAILED || !sem) |
---|
51 | return -1; |
---|
52 | semaphore[id] = sem; |
---|
53 | #if !USE_SEM_INIT |
---|
54 | sem_unlink(buf); |
---|
55 | #endif |
---|
56 | return 1; |
---|
57 | } |
---|
58 | |
---|
59 | int sipc_semaphore_exists(int id) |
---|
60 | { |
---|
61 | if ((id<0) || (id >= SIPC_MAX_SEMAPHORES)) return -1; |
---|
62 | return semaphore[id] != NULL; |
---|
63 | } |
---|
64 | |
---|
65 | int sipc_semaphore_acquire(int id) |
---|
66 | { |
---|
67 | if ((id<0) || (id >= SIPC_MAX_SEMAPHORES) || (semaphore[id]==NULL)) return -1; |
---|
68 | sem_wait(semaphore[id]); |
---|
69 | return 1; |
---|
70 | } |
---|
71 | |
---|
72 | int sipc_semaphore_try_acquire(int id) |
---|
73 | { |
---|
74 | if ((id<0) || (id >= SIPC_MAX_SEMAPHORES) || (semaphore[id]==NULL)) return -1; |
---|
75 | return !sem_trywait(semaphore[id]); |
---|
76 | } |
---|
77 | |
---|
78 | int sipc_semaphore_release(int id) |
---|
79 | { |
---|
80 | if ((id<0) || (id >= SIPC_MAX_SEMAPHORES) || (semaphore[id]==NULL)) return -1; |
---|
81 | sem_post(semaphore[id]); |
---|
82 | return 1; |
---|
83 | } |
---|
84 | |
---|
85 | int sipc_semaphore_get_value(int id) |
---|
86 | { |
---|
87 | int val; |
---|
88 | if ((id<0) || (id >= SIPC_MAX_SEMAPHORES) || (semaphore[id]==NULL)) return -1; |
---|
89 | sem_getvalue(semaphore[id], &val); |
---|
90 | return val; |
---|
91 | } |
---|
92 | |
---|
93 | int simpleipc_cmd(char *cmd, int id, int v) |
---|
94 | { |
---|
95 | if (strcmp(cmd,"init")==0) |
---|
96 | return sipc_semaphore_init(id,v); |
---|
97 | else if (strcmp(cmd,"exists")==0) |
---|
98 | return sipc_semaphore_exists(id); |
---|
99 | else if (strcmp(cmd,"acquire")==0) |
---|
100 | return sipc_semaphore_acquire(id); |
---|
101 | else if (strcmp(cmd,"try_acquire")==0) |
---|
102 | return sipc_semaphore_try_acquire(id); |
---|
103 | else if (strcmp(cmd,"release")==0) |
---|
104 | return sipc_semaphore_release(id); |
---|
105 | else if (strcmp(cmd,"get_value")==0) |
---|
106 | return sipc_semaphore_get_value(id); |
---|
107 | else printf("unknown\n"); |
---|
108 | return -2; |
---|
109 | } |
---|
110 | #endif |
---|