1#ifndef SEMAPHORE_H 2#define SEMAPHORE_H 3 4#include <sys/types.h> 5#include <sys/ipc.h> 6#include <sys/sem.h> 7 8#ifndef SEMUN_IN_SEM_H 9union semun 10{ 11 int val; /* value for SETVAL */ 12 struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */ 13 unsigned short int *array; /* array for GETALL, SETALL */ 14 struct seminfo *__buf; /* buffer for IPC_INFO */ 15}; 16#endif 17 18class Semaphore 19{ 20public: 21 22 // numSems is the number of semaphores to be in the set 23 // semKey is the ID number for the semaphore set 24 // val is the initial value for the semaphores, no values will be assigned 25 // if the default (0) is specified. 26 Semaphore(int semKey, int numSems = 1, int val = 0); 27 28 // clear the semaphores and return an error code. 29 int clear_sem(); 30 31 // create the semaphores 32 // count is the initial value assigned to each semaphore 33 int create(int count); 34 35 // get the handle to a semaphore set previously created 36 int get_semid(); 37 38 int decrement_and_wait(int nr_sem); 39 int get_mutex(); 40 int put_mutex(); 41 42private: 43 union semun m_arg; 44 int m_semid; 45 int m_semflg; 46 bool m_semopen; 47 int m_semKey; 48 int m_numSems; 49}; 50 51#endif 52 53