Aggiunte e correzioni alle funzioni per i file di lock
[gapil.git] / sources / wrappers.h
index 7120c4348a5fef08fc27671b4c592ffa30773ccd..ed4a19b32f06b190ead58071714cdef518df0aa3 100644 (file)
@@ -5,7 +5,7 @@
  *
  * Author: S. Piccardi
  *
- * $Id: wrappers.h,v 1.2 2001/06/10 11:47:17 piccardi Exp $
+ * $Id: wrappers.h,v 1.5 2002/12/02 23:06:59 piccardi Exp $
  *
  ***************************************************************/
 #include <sys/sem.h>     /* IPC semaphore declarations */
@@ -32,45 +32,8 @@ union semun {
 };
 #endif
 /*
- * Define the sem_lock and sem_ulock sembuf structures to 
- * lock and unlock the semaphore (used to implement a mutex)
- */
-struct sembuf sem_lock={      /* to lock semaphore */
-    0,    /* semaphore number (only one so 0) */
-    -1,   /* semaphore operation (-1 to use resource) */
-    0};   /* semaphore flag (in this case 0) */
-struct sembuf sem_ulock={     /* to unlock semaphore */
-    0,    /* semaphore number (only one so 0) */
-    1,    /* semaphore operation (-1 to release resource) */
-    0};   /* semaphore flag (in this case 0) */
-/*
- * Function MutexLock:
- * to lock a mutex/semaphore
- *
- * Input: a semaphore id #
- */
-inline void MutexLock(int sem_id) 
-{
-    if (semop(sem_id,&sem_lock,1)) {
-       perror("Cannot lock the semaphore");
-       exit(1);
-    }
-}
-/*
- * Function MutexUnlock
- * to unlock a mutex/semaphore
+ * Function MutexCreate: create a mutex/semaphore
  *
- * Input: a semaphore id #
- */
-inline void MutexUnlock(int sem_id) 
-{
-    if (semop(sem_id,&sem_ulock,1)) {
-       perror("Cannot unlock the semaphore");
-       exit(1);
-    }
-}
-/*
- * Function MutexCreate:
  * First call create a semaphore, using the given key. 
  * We want only one semaphore so we set second argument to 1; third 
  * parameter is the flag argument, and is set to create a semaphore 
@@ -78,55 +41,72 @@ inline void MutexUnlock(int sem_id)
  * Second call initialize the semaphore to 1 (unlocked)
  *
  * Input: an IPC key value (to create an unique semaphore)
- * Return: the semaphore id#
+ * Return: the semaphore id# or -1 on error
  */
-const union semun semunion={1};    /* semaphore union structure */
 inline int MutexCreate(key_t ipc_key) 
 {
-    int sem_id;
-    if( (sem_id=semget(ipc_key,1,IPC_CREAT|0666))<0 ){ /* get sem ID */
-       perror("cannot create semaphore");     /* a sem_id <0 is an error */
-       printf("semid=%d",sem_id);
-       exit(1);
+    const union semun semunion={1};             /* semaphore union structure */
+    int sem_id, ret;
+    sem_id = semget(ipc_key, 1, IPC_CREAT|0666);         /* get semaphore ID */
+    if (sem_id == -1) {                              /* if error return code */
+       return sem_id;
     }
-    if ( (semctl(sem_id,0,SETVAL,semunion)) < 0 ) {
-        perror("cannot init semaphore");       /* <0 is an error */
-       printf("on semid=%d",sem_id);
-       exit(1);
+    ret = semctl(sem_id, 0, SETVAL, semunion);             /* init semaphore */
+    if (ret == -1) {
+       return ret;
     }
     return sem_id;
 }
 /*
- * Find Mutex
- * get the semaphore/mutex Id given the IPC key value
+ * Function MutexFind: get the semaphore/mutex Id given the IPC key value
  *
  * Input: an IPC key value
  */
 inline int MutexFind(key_t ipc_key) 
 {
-    int sem_id;
-    if( (sem_id=semget(ipc_key,1,0))<0 ){       /* find sem .ID */
-       perror("cannot find semaphore");
-        exit(1);
-    }
-    return sem_id;
+    return semget(ipc_key,1,0);
 }
 /*
- * Function MutexRead:
- * Read the current value of the mutex/semaphore
+ * Function MutexRead: read the current value of the mutex/semaphore
  *
  * Input:  a semaphore id #
  * Return: the semaphore value
  */
 inline int MutexRead(int sem_id) 
 {
-    int value;
-    if ( (value=semctl(sem_id,0,GETVAL,semunion)) < 0 ) {
-             perror("cannot read semaphore");       /* a <0 is an error */
-             printf("on semid=%d\n",sem_id);
-             exit(1);
-    } 
-    return value;
+    return semctl(sem_id, 0, GETVAL);
+}
+/*
+ * Define sembuf structures to lock and unlock the semaphore 
+ * (used to implement a mutex)
+ */
+struct sembuf sem_lock={                                /* to lock semaphore */
+    0,                                   /* semaphore number (only one so 0) */
+    -1,                                    /* operation (-1 to use resource) */
+    SEM_UNDO};                                /* flag (set for undo at exit) */
+struct sembuf sem_ulock={                             /* to unlock semaphore */
+    0,                                   /* semaphore number (only one so 0) */
+    1,                                  /* operation (1 to release resource) */
+    SEM_UNO};                                       /* flag (in this case 0) */
+/*
+ * Function MutexLock: to lock a mutex/semaphore
+ *
+ * Input:  a semaphore id #
+ * Output: semop return code  (0 OK, -1 KO)
+ */
+inline int MutexLock(int sem_id) 
+{
+    return semop(sem_id, &sem_lock, 1);
+}
+/*
+ * Function MutexUnlock: to unlock a mutex/semaphore
+ *
+ * Input:  a semaphore id #
+ * Return: semop return code (0 OK, -1 KO)
+ */
+inline int MutexUnlock(int sem_id) 
+{
+    return semop(sem_id, &sem_ulock, 1);
 }
 /*
  * Function ShmCreate:
@@ -181,24 +161,18 @@ inline char * ShmFind(key_t ipc_key, int shm_size)
     return shptr;
 }
 /*
- * Function LockFile:
- * Create a lockfile of the given pathname.
+ * Function LockFile & UnlockFile:
+ * Create and remove a lockfile of the given pathname.
  * Fail and exit in case of error or existence of the same lock 
  * file, using unlink do not need to remove the file,
  */
-inline void LockFile(const char* path_name) 
+inline int LockFile(const char* path_name) 
 {
-    if (open(path_name, O_EXCL|O_CREAT)<0) {
-       perror("Already active");
-       exit(1);
-    } 
+    return open(path_name, O_EXCL|O_CREAT);
 }
-inline void UnlockFile(const char* path_name) 
+inline int UnlockFile(const char* path_name) 
 {
-    if (unlink(path_name)) {
-       perror("error, cannot unlink");
-       exit(1);
-    }  
+    return unlink(path_name);
 }
 /*
  * Function Signal
@@ -237,3 +211,4 @@ inline SigFunc * Signal(int signo, SigFunc *func)
 ssize_t SockRead(int fd, void *buf, size_t count);
 ssize_t SockWrite(int fd, const void *buf, size_t count);
 
+void HandSIGCHLD(int sig);