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