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