Functions | Variables
semaphore.c File Reference
#include <kernel/mod2.h>
#include <semaphore.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "simpleipc.h"
#include <Singular/cntrlc.h>
#include <reporter/si_signals.h>

Go to the source code of this file.

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_release (int id)
 
int sipc_semaphore_get_value (int id)
 
int simpleipc_cmd (char *cmd, int id, int v)
 

Variables

sem_t * semaphore [SIPC_MAX_SEMAPHORES]
 
int sem_acquired [SIPC_MAX_SEMAPHORES]
 

Function Documentation

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

Definition at line 122 of file semaphore.c.

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 }
int sipc_semaphore_get_value(int id)
Definition: semaphore.c:114
int sipc_semaphore_try_acquire(int id)
Definition: semaphore.c:89
int sipc_semaphore_exists(int id)
Definition: semaphore.c:72
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
int sipc_semaphore_acquire(int id)
Definition: semaphore.c:78
int sipc_semaphore_acquire ( int  id)

Definition at line 78 of file semaphore.c.

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 }
void m2_end(int i)
Definition: misc_ip.cc:1074
sem_t * semaphore[SIPC_MAX_SEMAPHORES]
Definition: semaphore.c:29
volatile BOOLEAN do_shutdown
Definition: cntrlc.cc:84
#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_exists ( int  id)

Definition at line 72 of file semaphore.c.

73 {
74  if ((id<0) || (id >= SIPC_MAX_SEMAPHORES)) return -1;
75  return semaphore[id] != NULL;
76 }
sem_t * semaphore[SIPC_MAX_SEMAPHORES]
Definition: semaphore.c:29
#define NULL
Definition: omList.c:10
#define SIPC_MAX_SEMAPHORES
Definition: simpleipc.h:10
int sipc_semaphore_get_value ( int  id)

Definition at line 114 of file semaphore.c.

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 }
sem_t * semaphore[SIPC_MAX_SEMAPHORES]
Definition: semaphore.c:29
#define NULL
Definition: omList.c:10
#define SIPC_MAX_SEMAPHORES
Definition: simpleipc.h:10
int sipc_semaphore_init ( int  id,
int  count 
)

Definition at line 36 of file semaphore.c.

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 }
int status int void size_t count
Definition: si_signals.h:59
sem_t * semaphore[SIPC_MAX_SEMAPHORES]
Definition: semaphore.c:29
int status int void * buf
Definition: si_signals.h:59
void * malloc(size_t size)
Definition: omalloc.c:92
#define free
Definition: omAllocFunc.c:12
#define SIPC_MAX_SEMAPHORES
Definition: simpleipc.h:10
int sipc_semaphore_release ( int  id)

Definition at line 103 of file semaphore.c.

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 }
void m2_end(int i)
Definition: misc_ip.cc:1074
sem_t * semaphore[SIPC_MAX_SEMAPHORES]
Definition: semaphore.c:29
volatile BOOLEAN do_shutdown
Definition: cntrlc.cc:84
#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_try_acquire ( int  id)

Definition at line 89 of file semaphore.c.

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 }
void m2_end(int i)
Definition: misc_ip.cc:1074
sem_t * semaphore[SIPC_MAX_SEMAPHORES]
Definition: semaphore.c:29
volatile BOOLEAN do_shutdown
Definition: cntrlc.cc:84
#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

Variable Documentation

int sem_acquired[SIPC_MAX_SEMAPHORES]

Definition at line 30 of file semaphore.c.

sem_t* semaphore[SIPC_MAX_SEMAPHORES]

Definition at line 29 of file semaphore.c.