3 * Copyright (C) 2002 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /*****************************************************************************
21 * File Mutex.c: define a set of functions for mutex manipulation
23 * Author: S. Piccardi Dec. 2002
25 * $Id: Mutex.c,v 1.4 2003/01/04 17:24:30 piccardi Exp $
27 *****************************************************************************/
28 #include <sys/sem.h> /* IPC semaphore declarations */
29 #include <sys/types.h>
32 #include <signal.h> /* signal handling declarations */
36 * Function MutexCreate: create a mutex/semaphore
38 * First call create a semaphore, using the given key.
39 * We want only one semaphore so we set second argument to 1; third
40 * parameter is the flag argument, and is set to create a semaphore
41 * with R/W privilege for the user.
42 * Second call initialize the semaphore to 1 (unlocked)
44 * Input: an IPC key value (to create an unique semaphore)
45 * Return: the semaphore id# or -1 on error
47 int MutexCreate(key_t ipc_key)
49 const union semun semunion={1}; /* semaphore union structure */
51 sem_id = semget(ipc_key, 1, IPC_CREAT|0666); /* get semaphore ID */
52 if (sem_id == -1) { /* if error return code */
55 ret = semctl(sem_id, 0, SETVAL, semunion); /* init semaphore */
62 * Function MutexFind: get the semaphore/mutex Id given the IPC key value
64 * Input: an IPC key value
66 int MutexFind(key_t ipc_key)
68 return semget(ipc_key, 1, 0);
71 * Function MutexRead: read the current value of the mutex/semaphore
73 * Input: a semaphore id #
74 * Return: the semaphore value
76 int MutexRead(int sem_id)
78 return semctl(sem_id, 0, GETVAL);
81 * Define sembuf structures to lock and unlock the semaphore
82 * (used to implement a mutex)
84 struct sembuf sem_lock={ /* to lock semaphore */
85 0, /* semaphore number (only one so 0) */
86 -1, /* operation (-1 to use resource) */
87 SEM_UNDO}; /* flag (set for undo at exit) */
88 struct sembuf sem_ulock={ /* to unlock semaphore */
89 0, /* semaphore number (only one so 0) */
90 1, /* operation (1 to release resource) */
91 SEM_UNDO}; /* flag (in this case 0) */
93 * Function MutexLock: to lock a mutex/semaphore
95 * Input: a semaphore id #
96 * Output: semop return code (0 OK, -1 KO)
98 int MutexLock(int sem_id)
100 return semop(sem_id, &sem_lock, 1);
103 * Function MutexUnlock: to unlock a mutex/semaphore
105 * Input: a semaphore id #
106 * Return: semop return code (0 OK, -1 KO)
108 int MutexUnlock(int sem_id)
110 return semop(sem_id, &sem_ulock, 1);
113 * Function MutexRemove: remove a mutex/semaphore
115 * Input: a semaphore id #
116 * Return: return code of semctl
118 int MutexRemove(int sem_id)
120 return semctl(sem_id, 0, IPC_RMID);
122 /*****************************************************************************
126 * Create a mutex usinf file locking. Use file locking to lock a file
127 * as a mutex request, and unlock it as a mutex release.
129 * Author: S. Piccardi Dec. 2002
131 *****************************************************************************/
133 * Function LockMutex: lock a file (creating it if not existent).
136 * Output: a return code (0 OK, -1 KO)
138 int LockMutex(const char *path_name)
141 struct flock lock; /* file lock structure */
142 /* first open the file (creating it if not existent) */
143 if ( (fd = open(path_name, O_EXCL|O_CREAT)) < 0) { /* first open file */
146 /* set flock structure */
147 lock.l_type = F_WRLCK; /* set type: read or write */
148 lock.l_whence = SEEK_SET; /* start from the beginning of the file */
149 lock.l_start = 0; /* set the start of the locked region */
150 lock.l_len = 0; /* set the length of the locked region */
152 if ( (res = fcntl(fd, F_SETLKW, &lock)) < 0 ) {
158 * Function UnlockMutex: unlock a file.
161 * Output: a return code (0 OK, -1 KO)
163 int UnlockMutex(const char *path_name)
166 struct flock lock; /* file lock structure */
167 /* first open the file */
168 if ( (fd = open(path_name, O_RDWR)) < 0) { /* first open file */
171 /* set flock structure */
172 lock.l_type = F_UNLCK; /* set type: unlock */
173 lock.l_whence = SEEK_SET; /* start from the beginning of the file */
174 lock.l_start = 0; /* set the start of the locked region */
175 lock.l_len = 0; /* set the length of the locked region */
177 if ( (res = fcntl(fd, F_SETLK, &lock)) < 0 ) {