78b4fca524e7f4a4f9d13a8358db6a3a288c6341
[gapil.git] / sources / Mutex.c
1 /*****************************************************************************
2  *
3  * File Mutex.c: define a set of functions for mutex manipulation 
4  *
5  * Author: S. Piccardi Dec. 2002
6  *
7  * $Id: Mutex.c,v 1.1 2002/12/03 11:06:05 piccardi Exp $
8  *
9  *****************************************************************************/
10 #include <sys/sem.h>     /* IPC semaphore declarations */
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <signal.h>      /* signal handling declarations */
15
16 #include "Gapil.h"
17 /*
18  * Function MutexCreate: create a mutex/semaphore
19  *
20  * First call create a semaphore, using the given key. 
21  * We want only one semaphore so we set second argument to 1; third 
22  * parameter is the flag argument, and is set to create a semaphore 
23  * with R/W privilege for the user.
24  * Second call initialize the semaphore to 1 (unlocked)
25  *
26  * Input: an IPC key value (to create an unique semaphore)
27  * Return: the semaphore id# or -1 on error
28  */
29 int MutexCreate(key_t ipc_key) 
30 {
31     const union semun semunion={1};             /* semaphore union structure */
32     int sem_id, ret;
33     sem_id = semget(ipc_key, 1, IPC_CREAT|0666);         /* get semaphore ID */
34     if (sem_id == -1) {                              /* if error return code */
35         return sem_id;
36     }
37     ret = semctl(sem_id, 0, SETVAL, semunion);             /* init semaphore */
38     if (ret == -1) {
39         return ret;
40     }
41     return sem_id;
42 }
43 /*
44  * Function MutexFind: get the semaphore/mutex Id given the IPC key value
45  *
46  * Input: an IPC key value
47  */
48 int MutexFind(key_t ipc_key) 
49 {
50     return semget(ipc_key,1,0);
51 }
52 /*
53  * Function MutexRead: read the current value of the mutex/semaphore
54  *
55  * Input:  a semaphore id #
56  * Return: the semaphore value
57  */
58 int MutexRead(int sem_id) 
59 {
60     return semctl(sem_id, 0, GETVAL);
61 }
62 /*
63  * Define sembuf structures to lock and unlock the semaphore 
64  * (used to implement a mutex)
65  */
66 struct sembuf sem_lock={                                /* to lock semaphore */
67     0,                                   /* semaphore number (only one so 0) */
68     -1,                                    /* operation (-1 to use resource) */
69     SEM_UNDO};                                /* flag (set for undo at exit) */
70 struct sembuf sem_ulock={                             /* to unlock semaphore */
71     0,                                   /* semaphore number (only one so 0) */
72     1,                                  /* operation (1 to release resource) */
73     SEM_UNDO};                                      /* flag (in this case 0) */
74 /*
75  * Function MutexLock: to lock a mutex/semaphore
76  *
77  * Input:  a semaphore id #
78  * Output: semop return code  (0 OK, -1 KO)
79  */
80 int MutexLock(int sem_id) 
81 {
82     return semop(sem_id, &sem_lock, 1);
83 }
84 /*
85  * Function MutexUnlock: to unlock a mutex/semaphore
86  *
87  * Input:  a semaphore id #
88  * Return: semop return code (0 OK, -1 KO)
89  */
90 int MutexUnlock(int sem_id) 
91 {
92     return semop(sem_id, &sem_ulock, 1);
93 }