X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2Fwrappers.h;h=15ddd826c2f3500668632b8d1cea932e6a95fef1;hp=ee8fa89da88d4d85489ae1bb209807408907eeab;hb=3498a6fc0fd13e07cacdea210cb99126d5052fbc;hpb=4ddd2ee63965e5eb0f723da634d157447f3e4ceb diff --git a/sources/wrappers.h b/sources/wrappers.h index ee8fa89..15ddd82 100644 --- a/sources/wrappers.h +++ b/sources/wrappers.h @@ -5,7 +5,7 @@ * * Author: S. Piccardi * - * $Id: wrappers.h,v 1.3 2002/08/18 23:24:44 piccardi Exp $ + * $Id: wrappers.h,v 1.4 2002/11/20 23:34:02 piccardi Exp $ * ***************************************************************/ #include /* IPC semaphore declarations */ @@ -32,45 +32,8 @@ union semun { }; #endif /* - * Define the sem_lock and sem_ulock 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, /* semaphore operation (-1 to use resource) */ - 0}; /* semaphore flag (in this case 0) */ -struct sembuf sem_ulock={ /* to unlock semaphore */ - 0, /* semaphore number (only one so 0) */ - 1, /* semaphore operation (-1 to release resource) */ - 0}; /* semaphore flag (in this case 0) */ -/* - * Function MutexLock: - * to lock a mutex/semaphore - * - * Input: a semaphore id # - */ -inline void MutexLock(int sem_id) -{ - if (semop(sem_id,&sem_lock,1)) { - perror("Cannot lock the semaphore"); - exit(1); - } -} -/* - * Function MutexUnlock - * to unlock a mutex/semaphore + * Function MutexCreate: create a mutex/semaphore * - * Input: a semaphore id # - */ -inline void MutexUnlock(int sem_id) -{ - if (semop(sem_id,&sem_ulock,1)) { - perror("Cannot unlock the semaphore"); - exit(1); - } -} -/* - * Function MutexCreate: * 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 @@ -78,55 +41,72 @@ inline void MutexUnlock(int sem_id) * Second call initialize the semaphore to 1 (unlocked) * * Input: an IPC key value (to create an unique semaphore) - * Return: the semaphore id# + * Return: the semaphore id# or -1 on error */ -const union semun semunion={1}; /* semaphore union structure */ inline int MutexCreate(key_t ipc_key) { - int sem_id; - if( (sem_id=semget(ipc_key,1,IPC_CREAT|0666))<0 ){ /* get sem ID */ - perror("cannot create semaphore"); /* a sem_id <0 is an error */ - printf("semid=%d",sem_id); - exit(1); + 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; } - if ( (semctl(sem_id,0,SETVAL,semunion)) < 0 ) { - perror("cannot init semaphore"); /* <0 is an error */ - printf("on semid=%d",sem_id); - exit(1); + ret = semctl(sem_id, 0, SETVAL, semunion); /* init semaphore */ + if (ret == -1) { + return ret; } return sem_id; } /* - * Find Mutex - * get the semaphore/mutex Id given the IPC key value + * Function MutexFind: get the semaphore/mutex Id given the IPC key value * * Input: an IPC key value */ inline int MutexFind(key_t ipc_key) { - int sem_id; - if( (sem_id=semget(ipc_key,1,0))<0 ){ /* find sem .ID */ - perror("cannot find semaphore"); - exit(1); - } - return sem_id; + return semget(ipc_key,1,0); } /* - * Function MutexRead: - * Read the current value of the mutex/semaphore + * Function MutexRead: read the current value of the mutex/semaphore * * Input: a semaphore id # * Return: the semaphore value */ inline int MutexRead(int sem_id) { - int value; - if ( (value=semctl(sem_id,0,GETVAL,semunion)) < 0 ) { - perror("cannot read semaphore"); /* a <0 is an error */ - printf("on semid=%d\n",sem_id); - exit(1); - } - return value; + 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_UNO}; /* flag (in this case 0) */ +/* + * Function MutexLock: to lock a mutex/semaphore + * + * Input: a semaphore id # + * Output: semop return code (0 OK, -1 KO) + */ +inline 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) + */ +inline int MutexUnlock(int sem_id) +{ + return semop(sem_id, &sem_ulock, 1); } /* * Function ShmCreate: