Correzioni rimaste indietro ed espansione funzioni del resolver.
[gapil.git] / listati / Mutex.c
1 /* Function MutexCreate: create a mutex/semaphore */
2 int MutexCreate(key_t ipc_key) 
3 {
4     const union semun semunion={1}; /* semaphore union structure */
5     int sem_id, ret;
6     sem_id = semget(ipc_key, 1, IPC_CREAT|0666); /* get semaphore ID */
7     if (sem_id == -1) {             /* if error return code */
8         return sem_id;
9     }
10     ret = semctl(sem_id, 0, SETVAL, semunion);   /* init semaphore */
11     if (ret == -1) {
12         return ret;
13     }
14     return sem_id;
15 }
16 /* Function MutexFind: get the semaphore/mutex Id given the IPC key value */
17 int MutexFind(key_t ipc_key) 
18 {
19     return semget(ipc_key,1,0);
20 }
21 /* Function MutexRead: read the current value of the mutex/semaphore */
22 int MutexRead(int sem_id) 
23 {
24     return semctl(sem_id, 0, GETVAL);
25 }
26 /* Define sembuf structures to lock and unlock the semaphore  */
27 struct sembuf sem_lock={  /* to lock semaphore */
28     0,                    /* semaphore number (only one so 0) */
29     -1,                   /* operation (-1 to use resource) */
30     SEM_UNDO};            /* flag (set for undo at exit) */
31 struct sembuf sem_ulock={ /* to unlock semaphore */
32     0,                    /* semaphore number (only one so 0) */
33     1,                    /* operation (1 to release resource) */
34     SEM_UNDO};            /* flag (in this case 0) */
35 /* Function MutexLock: to lock a mutex/semaphore */
36 int MutexLock(int sem_id) 
37 {
38     return semop(sem_id, &sem_lock, 1);
39 }
40 /* Function MutexUnlock: to unlock a mutex/semaphore */
41 int MutexUnlock(int sem_id) 
42 {
43     return semop(sem_id, &sem_ulock, 1);
44 }
45 /* Function MutexRemove: remove a mutex/semaphore */
46 int MutexRemove(int sem_id) 
47 {
48     return semctl(sem_id, 0, IPC_RMID);
49 }