Aggiunta funzione MurexRead, riscritti Mutex con il file locking
[gapil.git] / sources / Mutex.c
index ddbb9b11354447b120502d9f6924db08a8c5e2fc..52991f53026f4a13e3ceec4fbca3a2f34bb78b95 100644 (file)
@@ -22,7 +22,7 @@
  *
  * Author: S. Piccardi Dec. 2002
  *
- * $Id: Mutex.c,v 1.2 2002/12/03 22:30:11 piccardi Exp $
+ * $Id: Mutex.c,v 1.5 2003/01/06 21:24:41 piccardi Exp $
  *
  *****************************************************************************/
 #include <sys/sem.h>     /* IPC semaphore declarations */
@@ -65,7 +65,7 @@ int MutexCreate(key_t ipc_key)
  */
 int MutexFind(key_t ipc_key) 
 {
-    return semget(ipc_key,1,0);
+    return semget(ipc_key, 1, 0);
 }
 /*
  * Function MutexRead: read the current value of the mutex/semaphore
@@ -109,3 +109,119 @@ int MutexUnlock(int sem_id)
 {
     return semop(sem_id, &sem_ulock, 1);
 }
+/*
+ * Function MutexRemove: remove a mutex/semaphore
+ *
+ * Input:  a semaphore id #
+ * Return: return code of semctl
+ */
+int MutexRemove(int sem_id) 
+{
+    return semctl(sem_id, 0, IPC_RMID);
+}
+/***************************************************************************** 
+ *
+ * File locking mutex 
+ *
+ * Create a mutex usinf file locking. Use file locking to lock a file
+ * as a mutex request, and unlock it as a mutex release.
+ *
+ * Author: S. Piccardi Dec. 2002
+ *
+ *****************************************************************************/
+/*
+ * Function CreateMutex: Create a mutex using file locking.  
+ *
+ * Open a new lock file (creating it if not existent, and giving error
+ * otherwise). Is a simple wrapper for open.
+ *
+ * Input:  a filename
+ * Output: a file descriptor  (>0 OK, -1 KO)
+ */
+int CreateMutex(const char *path_name)
+{
+    return open(path_name, O_EXCL|O_CREAT);
+}
+/*
+ * Function UnlockMutex: unlock a file.  
+ * 
+ * Open a lock file (failing if not existent). Is a simple wrapper for
+ * open.
+ *
+ * Input:  a filename
+ * Output: a return code  (>0 OK, -1 KO)
+ */
+int FindMutex(const char *path_name)
+{
+    return open(path_name, O_RDWR);
+}
+/*
+ * Function LockMutex: lock mutex using file locking.
+ *
+ * Input:  a mutex (i.e. a file descriptor)
+ * Output: a return code  (0 OK, -1 KO)
+ */
+int LockMutex(int fd)
+{
+    struct flock lock;                                /* file lock structure */
+    /* first open the file (creating it if not existent) */
+    /* set flock structure */
+    lock.l_type = F_WRLCK;                        /* set type: read or write */
+    lock.l_whence = SEEK_SET;        /* start from the beginning of the file */
+    lock.l_start = 0;                  /* set the start of the locked region */
+    lock.l_len = 0;                   /* set the length of the locked region */
+    /* do locking */
+    return fcntl(fd, F_SETLKW, &lock);
+}
+/*
+ * Function UnlockMutex: unlock a file.  
+ *
+ * Input:  a mutex (i.e. a file descriptor)
+ * Output: a return code  (0 OK, -1 KO)
+ */
+int UnlockMutex(int fd)
+{
+    struct flock lock;                                /* file lock structure */
+    /* set flock structure */
+    lock.l_type = F_UNLCK;                               /* set type: unlock */
+    lock.l_whence = SEEK_SET;        /* start from the beginning of the file */
+    lock.l_start = 0;                  /* set the start of the locked region */
+    lock.l_len = 0;                   /* set the length of the locked region */
+    /* do locking */
+    return fcntl(fd, F_SETLK, &lock);
+}
+/*
+ * Function RemoveMutex: remove a mutex (unlinking the lock file).
+ * 
+ * Just remove the lock file.
+ *
+ * Input:  a filename
+ * Output: a return code (0 OK, -1 KO)
+ */
+int RemoveMutex(const char *path_name)
+{
+    return unlink(path_name);
+}
+/*
+ * Function ReadMutex: read a mutex status.
+ * 
+ * Read the status for a mutex.
+ *
+ * Input:  a mutex (i.e. a file descriptor)
+ * Output: the lock type (F_UNLCK,  F_RDLCK or  F_WRLCK, or -1 if KO)
+ */
+int ReadMutex(int fd)
+{
+    int res;
+    struct flock lock;                                /* file lock structure */
+    /* set flock structure */
+    lock.l_type = F_WRLCK;                               /* set type: unlock */
+    lock.l_whence = SEEK_SET;        /* start from the beginning of the file */
+    lock.l_start = 0;                  /* set the start of the locked region */
+    lock.l_len = 0;                   /* set the length of the locked region */
+    /* do locking */
+    if ( (res = fcntl(fd, F_GETLK, &lock)) ) {
+       return res;
+    }
+    return lock.l_type;
+}