X-Git-Url: https://gapil.gnulinux.it/gitweb/?a=blobdiff_plain;ds=sidebyside;f=sources%2FMutex.c;fp=sources%2FMutex.c;h=78b4fca524e7f4a4f9d13a8358db6a3a288c6341;hb=6483a787322c614bc6282a0bf0ee001f1bf54b44;hp=0000000000000000000000000000000000000000;hpb=78a82bdb333ca71e395ba39a5c03745761341150;p=gapil.git diff --git a/sources/Mutex.c b/sources/Mutex.c new file mode 100644 index 0000000..78b4fca --- /dev/null +++ b/sources/Mutex.c @@ -0,0 +1,93 @@ +/***************************************************************************** + * + * 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 $ + * + *****************************************************************************/ +#include /* IPC semaphore declarations */ +#include +#include +#include +#include /* signal handling declarations */ + +#include "Gapil.h" +/* + * Function MutexCreate: create a mutex/semaphore + * + * First call create a semaphore, using the given key. + * We want only one semaphore so we set second argument to 1; third + * parameter is the flag argument, and is set to create a semaphore + * with R/W privilege for the user. + * Second call initialize the semaphore to 1 (unlocked) + * + * Input: an IPC key value (to create an unique semaphore) + * Return: the semaphore id# or -1 on error + */ +int MutexCreate(key_t ipc_key) +{ + const union semun semunion={1}; /* semaphore union structure */ + int sem_id, ret; + sem_id = semget(ipc_key, 1, IPC_CREAT|0666); /* get semaphore ID */ + if (sem_id == -1) { /* if error return code */ + return sem_id; + } + ret = semctl(sem_id, 0, SETVAL, semunion); /* init semaphore */ + if (ret == -1) { + return ret; + } + return sem_id; +} +/* + * Function MutexFind: get the semaphore/mutex Id given the IPC key value + * + * Input: an IPC key value + */ +int MutexFind(key_t ipc_key) +{ + return semget(ipc_key,1,0); +} +/* + * Function MutexRead: read the current value of the mutex/semaphore + * + * Input: a semaphore id # + * Return: the semaphore value + */ +int MutexRead(int sem_id) +{ + return semctl(sem_id, 0, GETVAL); +} +/* + * Define sembuf structures to lock and unlock the semaphore + * (used to implement a mutex) + */ +struct sembuf sem_lock={ /* to lock semaphore */ + 0, /* semaphore number (only one so 0) */ + -1, /* operation (-1 to use resource) */ + SEM_UNDO}; /* flag (set for undo at exit) */ +struct sembuf sem_ulock={ /* to unlock semaphore */ + 0, /* semaphore number (only one so 0) */ + 1, /* operation (1 to release resource) */ + SEM_UNDO}; /* flag (in this case 0) */ +/* + * Function MutexLock: to lock a mutex/semaphore + * + * Input: a semaphore id # + * Output: semop return code (0 OK, -1 KO) + */ +int MutexLock(int sem_id) +{ + return semop(sem_id, &sem_lock, 1); +} +/* + * Function MutexUnlock: to unlock a mutex/semaphore + * + * Input: a semaphore id # + * Return: semop return code (0 OK, -1 KO) + */ +int MutexUnlock(int sem_id) +{ + return semop(sem_id, &sem_ulock, 1); +}