X-Git-Url: https://gapil.gnulinux.it/gitweb/?a=blobdiff_plain;f=sources%2FMutex.c;h=076c59360c430c4efe973064b8ce7e45765cddfe;hb=441dd1e480e625b9e8943261b706d0018c5f0faa;hp=78b4fca524e7f4a4f9d13a8358db6a3a288c6341;hpb=6483a787322c614bc6282a0bf0ee001f1bf54b44;p=gapil.git diff --git a/sources/Mutex.c b/sources/Mutex.c index 78b4fca..076c593 100644 --- a/sources/Mutex.c +++ b/sources/Mutex.c @@ -1,10 +1,28 @@ +/* Mutex.c + * + * Copyright (C) 2002 Simone Piccardi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ /***************************************************************************** * * File Mutex.c: define a set of functions for mutex manipulation * * Author: S. Piccardi Dec. 2002 * - * $Id: Mutex.c,v 1.1 2002/12/03 11:06:05 piccardi Exp $ + * $Id: Mutex.c,v 1.4 2003/01/04 17:24:30 piccardi Exp $ * *****************************************************************************/ #include /* IPC semaphore declarations */ @@ -47,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 @@ -91,3 +109,73 @@ 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 LockMutex: lock a file (creating it if not existent). + * + * Input: a filename + * Output: a return code (0 OK, -1 KO) + */ +int LockMutex(const char *path_name) +{ + 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; +} +/* + * Function UnlockMutex: unlock a file. + * + * Input: a filename + * Output: a return code (0 OK, -1 KO) + */ +int UnlockMutex(const char *path_name) +{ + 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 res; + } + return 0; +}