My Project
Loading...
Searching...
No Matches
Macros | Typedefs | Functions | Variables
simpleipc.h File Reference
#include <semaphore.h>

Go to the source code of this file.

Macros

#define SIPC_MAX_SEMAPHORES   512
 
#define USE_SEM_INIT   0
 

Typedefs

typedef sem_t sipc_sem_t
 

Functions

int sipc_semaphore_init (int id, int count)
 
int sipc_semaphore_exists (int id)
 
int sipc_semaphore_acquire (int id)
 
int sipc_semaphore_try_acquire (int id)
 
int sipc_semaphore_get_value (int id)
 
int sipc_semaphore_release (int id)
 
int simpleipc_cmd (char *cmd, int id, int v)
 

Variables

EXTERN_VAR sipc_sem_tsemaphore [SIPC_MAX_SEMAPHORES]
 
EXTERN_VAR int sem_acquired [SIPC_MAX_SEMAPHORES]
 

Macro Definition Documentation

◆ SIPC_MAX_SEMAPHORES

#define SIPC_MAX_SEMAPHORES   512

Definition at line 10 of file simpleipc.h.

◆ USE_SEM_INIT

#define USE_SEM_INIT   0

Definition at line 12 of file simpleipc.h.

Typedef Documentation

◆ sipc_sem_t

typedef sem_t sipc_sem_t

Definition at line 27 of file simpleipc.h.

Function Documentation

◆ simpleipc_cmd()

int simpleipc_cmd ( char *  cmd,
int  id,
int  v 
)

Definition at line 167 of file semaphore.c.

168{
169 if (strcmp(cmd,"init")==0)
170 return sipc_semaphore_init(id,v);
171 else if (strcmp(cmd,"exists")==0)
172 return sipc_semaphore_exists(id);
173 else if (strcmp(cmd,"acquire")==0)
174 return sipc_semaphore_acquire(id);
175 else if (strcmp(cmd,"try_acquire")==0)
177 else if (strcmp(cmd,"release")==0)
178 return sipc_semaphore_release(id);
179 else if (strcmp(cmd,"get_value")==0)
180 return sipc_semaphore_get_value(id);
181 else printf("unknown\n");
182 return -2;
183}
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:39
int sipc_semaphore_get_value(int id)
Definition: semaphore.c:153
int sipc_semaphore_acquire(int id)
Definition: semaphore.c:94
int sipc_semaphore_release(int id)
Definition: semaphore.c:135
int sipc_semaphore_exists(int id)
Definition: semaphore.c:88
int sipc_semaphore_try_acquire(int id)
Definition: semaphore.c:112
int sipc_semaphore_init(int id, int count)
Definition: semaphore.c:31

◆ sipc_semaphore_acquire()

int sipc_semaphore_acquire ( int  id)

Definition at line 94 of file semaphore.c.

95{
96 if ((id<0) || (id >= SIPC_MAX_SEMAPHORES) || (semaphore[id]==NULL)) return -1;
98#if PORTABLE_SEMAPHORES
99 si_sem_wait(semaphore[id]->sig);
100 si_sem_wait(semaphore[id]->guard);
101 semaphore[id]->count--;
102 sem_post(semaphore[id]->guard);
103#else
104 si_sem_wait(semaphore[id]);
105#endif
106 sem_acquired[id]++;
109 return 1;
110}
VAR volatile BOOLEAN do_shutdown
Definition: cntrlc.cc:74
VAR volatile int defer_shutdown
Definition: cntrlc.cc:75
void m2_end(int i)
Definition: misc_ip.cc:1097
#define NULL
Definition: omList.c:12
VAR sipc_sem_t * semaphore[SIPC_MAX_SEMAPHORES]
Definition: semaphore.c:24
VAR int sem_acquired[SIPC_MAX_SEMAPHORES]
Definition: semaphore.c:25
#define SIPC_MAX_SEMAPHORES
Definition: simpleipc.h:10

◆ sipc_semaphore_exists()

int sipc_semaphore_exists ( int  id)

Definition at line 88 of file semaphore.c.

89{
90 if ((id<0) || (id >= SIPC_MAX_SEMAPHORES)) return -1;
91 return semaphore[id] != NULL;
92}

◆ sipc_semaphore_get_value()

int sipc_semaphore_get_value ( int  id)

Definition at line 153 of file semaphore.c.

154{
155 int val;
156 if ((id<0) || (id >= SIPC_MAX_SEMAPHORES) || (semaphore[id]==NULL)) return -1;
157#if PORTABLE_SEMAPHORES
158 sem_wait(semaphore[id]->guard);
159 val = semaphore[id]->count;
160 sem_post(semaphore[id]->guard);
161#else
162 sem_getvalue(semaphore[id], &val);
163#endif
164 return val;
165}

◆ sipc_semaphore_init()

int sipc_semaphore_init ( int  id,
int  count 
)

Definition at line 31 of file semaphore.c.

32{
33 char buf[100];
34 sipc_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#if PORTABLE_SEMAPHORES
55#ifndef MAP_ANONYMOUS
56#define MAP_ANONYMOUS MAP_ANON
57#endif
58 sem = mmap(NULL, getpagesize(),
59 PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
60 if (sem == MAP_FAILED)
61 return -1;
62 sem->count = count;
63 sprintf(buf, "/%d:sem%d:g", getpid(), id);
64 sem_unlink(buf);
65 sem->guard = sem_open(buf, O_CREAT, 0600, count);
66 sem_unlink(buf);
67 sprintf(buf, "/%d:sem%d:s", getpid(), id);
68 sem_unlink(buf);
69 sem->sig = sem_open(buf, O_CREAT, 0600, count);
70 sem_unlink(buf);
71#else
72 sprintf(buf, "/%d:sem%d", getpid(), id);
73 sem_unlink(buf);
74 sem = sem_open(buf, O_CREAT, 0600, count);
75#endif
76#endif
77#if !PORTABLE_SEMAPHORES
78 if (sem == SEM_FAILED || !sem)
79 return -1;
80#endif
81 semaphore[id] = sem;
82#if !USE_SEM_INIT
83 sem_unlink(buf);
84#endif
85 return 1;
86}
#define free
Definition: omAllocFunc.c:14
#define malloc
Definition: omAllocFunc.c:12
int status int void size_t count
Definition: si_signals.h:59
int status int void * buf
Definition: si_signals.h:59
sem_t sipc_sem_t
Definition: simpleipc.h:27

◆ sipc_semaphore_release()

int sipc_semaphore_release ( int  id)

Definition at line 135 of file semaphore.c.

136{
137 if ((id<0) || (id >= SIPC_MAX_SEMAPHORES) || (semaphore[id]==NULL)) return -1;
139#if PORTABLE_SEMAPHORES
140 sem_wait(semaphore[id]->guard);
141 semaphore[id]->count++;
142 sem_post(semaphore[id]->sig);
143 sem_post(semaphore[id]->guard);
144#else
145 sem_post(semaphore[id]);
146#endif
147 sem_acquired[id]--;
150 return 1;
151}

◆ sipc_semaphore_try_acquire()

int sipc_semaphore_try_acquire ( int  id)

Definition at line 112 of file semaphore.c.

113{
114 if ((id<0) || (id >= SIPC_MAX_SEMAPHORES) || (semaphore[id]==NULL)) return -1;
116#if PORTABLE_SEMAPHORES
117 int trywait = si_sem_trywait(semaphore[id]->sig);
118#else
119 int trywait = si_sem_trywait(semaphore[id]);
120#endif
121 if (!trywait)
122 {
123#if PORTABLE_SEMAPHORES
124 si_sem_wait(semaphore[id]->guard);
125 semaphore[id]->count--;
126 sem_post(semaphore[id]->guard);
127#endif
128 sem_acquired[id]++;
129 }
132 return !trywait;
133}

Variable Documentation

◆ sem_acquired

EXTERN_VAR int sem_acquired[SIPC_MAX_SEMAPHORES]

Definition at line 32 of file simpleipc.h.

◆ semaphore

Definition at line 31 of file simpleipc.h.