[SCM] an open source computer algebra system branch, cleanedupstream, updated. 6125e540ca6d66c307958938a9d53b245507c323
Bernhard R. Link
brlink at debian.org
Tue Apr 24 15:54:05 UTC 2012
The following commit has been merged in the cleanedupstream branch:
commit 53a255a0daff08e321445d218ece1095a1d64da6
Author: Hans Schoenemann <hannes at mathematik.uni-kl.de>
Date: Mon Mar 12 10:35:12 2012 +0100
fix: check args for semaphore stuff, fix return values
diff --git a/Singular/semaphore.c b/Singular/semaphore.c
index 1a4dc7f..dba6e08 100644
--- a/Singular/semaphore.c
+++ b/Singular/semaphore.c
@@ -14,14 +14,19 @@
static sem_t *semaphore[SIPC_MAX_SEMAPHORES];
-int sipc_semaphore_init(int id, int count) {
+/* return 1 on success,
+ * 0 if already initialized,
+ * -1 for errors
+ */
+int sipc_semaphore_init(int id, int count)
+{
char buf[100];
sem_t *sem;
- if (id >= SIPC_MAX_SEMAPHORES)
- return 0;
+ if ((id<0) || (id >= SIPC_MAX_SEMAPHORES))
+ return -1;
// Already initialized?
if (semaphore[id])
- return 1;
+ return 0;
// to make it completely safe, we should generate a name
// from /dev/urandom.
#if USE_SEM_INIT
@@ -29,11 +34,11 @@ int sipc_semaphore_init(int id, int count) {
// can keep using the semaphore after fork + exec.
sem = malloc(sizeof(sem_t));
if (!sem)
- return 0;
+ return -1;
if (sem_init(sem, 1, count) < 0)
{
free(sem);
- return 0;
+ return -1;
}
#else
sprintf(buf, "/%d:sem%d", getpid(), id);
@@ -41,7 +46,7 @@ int sipc_semaphore_init(int id, int count) {
sem = sem_open(buf, O_CREAT, 0600, count);
#endif
if (sem == SEM_FAILED || !sem)
- return 0;
+ return -1;
semaphore[id] = sem;
#if !USE_SEM_INIT
sem_unlink(buf);
@@ -49,24 +54,36 @@ int sipc_semaphore_init(int id, int count) {
return 1;
}
-int sipc_semaphore_exists(int id) {
+int sipc_semaphore_exists(int id)
+{
+ if ((id<0) || (id >= SIPC_MAX_SEMAPHORES)) return -1;
return semaphore[id] != NULL;
}
-void sipc_semaphore_acquire(int id) {
+int sipc_semaphore_acquire(int id)
+{
+ if ((id<0) || (id >= SIPC_MAX_SEMAPHORES)) return -1;
sem_wait(semaphore[id]);
+ return 1;
}
-int sipc_semaphore_try_acquire(int id) {
+int sipc_semaphore_try_acquire(int id)
+{
+ if ((id<0) || (id >= SIPC_MAX_SEMAPHORES)) return -1;
return !sem_trywait(semaphore[id]);
}
-void sipc_semaphore_release(int id) {
+int sipc_semaphore_release(int id)
+{
+ if ((id<0) || (id >= SIPC_MAX_SEMAPHORES)) return -1;
sem_post(semaphore[id]);
+ return 1;
}
-int sipc_semaphore_get_value(int id) {
+int sipc_semaphore_get_value(int id)
+{
int val;
+ if ((id<0) || (id >= SIPC_MAX_SEMAPHORES)) return -1;
sem_getvalue(semaphore[id], &val);
return val;
}
@@ -78,13 +95,13 @@ int simpleipc_cmd(char *cmd, int id, int v)
else if (strcmp(cmd,"exists")==0)
return sipc_semaphore_exists(id);
else if (strcmp(cmd,"acquire")==0)
- { sipc_semaphore_acquire(id); return -2;}
+ return sipc_semaphore_acquire(id);
else if (strcmp(cmd,"try_acquire")==0)
return sipc_semaphore_try_acquire(id);
else if (strcmp(cmd,"release")==0)
- { sipc_semaphore_release(id); return -2;}
+ return sipc_semaphore_release(id);
else if (strcmp(cmd,"get_value")==0)
- { return sipc_semaphore_get_value(id); }
+ return sipc_semaphore_get_value(id);
else printf("unknown\n");
return -2;
}
--
an open source computer algebra system
More information about the debian-science-commits
mailing list