X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2FMutex.c;h=52991f53026f4a13e3ceec4fbca3a2f34bb78b95;hp=076c59360c430c4efe973064b8ce7e45765cddfe;hb=2c87ecb104050dd0cd78865a5390d8635b989b1a;hpb=b8248eaf35d824e8816c76ad9f9561c76d67b915 diff --git a/sources/Mutex.c b/sources/Mutex.c index 076c593..52991f5 100644 --- a/sources/Mutex.c +++ b/sources/Mutex.c @@ -22,7 +22,7 @@ * * Author: S. Piccardi Dec. 2002 * - * $Id: Mutex.c,v 1.4 2003/01/04 17:24:30 piccardi Exp $ + * $Id: Mutex.c,v 1.5 2003/01/06 21:24:41 piccardi Exp $ * *****************************************************************************/ #include /* IPC semaphore declarations */ @@ -130,52 +130,98 @@ int MutexRemove(int sem_id) * *****************************************************************************/ /* - * Function LockMutex: lock a file (creating it if not existent). + * 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(const char *path_name) +int LockMutex(int fd) { - int fd, res; struct flock lock; /* file lock structure */ /* first open the file (creating it if not existent) */ - if ( (fd = open(path_name, O_EXCL|O_CREAT)) < 0) { /* first open file */ - return fd; - } /* 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 */ - if ( (res = fcntl(fd, F_SETLKW, &lock)) < 0 ) { - return res; - } - return 0; + return fcntl(fd, F_SETLKW, &lock); } /* * Function UnlockMutex: unlock a file. * - * Input: a filename + * Input: a mutex (i.e. a file descriptor) * Output: a return code (0 OK, -1 KO) */ -int UnlockMutex(const char *path_name) +int UnlockMutex(int fd) { - int fd, res; struct flock lock; /* file lock structure */ - /* first open the file */ - if ( (fd = open(path_name, O_RDWR)) < 0) { /* first open file */ - return fd; - } /* 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 */ - if ( (res = fcntl(fd, F_SETLK, &lock)) < 0 ) { + 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 0; + return lock.l_type; }