CARMA C++
SemaphoreOperator.h
Go to the documentation of this file.
1 
2 
13 #ifndef CARMA_UTIL_SEMAPHORE_OPS_H
14 #define CARMA_UTIL_SEMAPHORE_OPS_H
15 
16 #include <sstream>
17 
18 #include <fcntl.h>
19 #include <sys/types.h>
20 #include <sys/ipc.h>
21 #include <sys/sem.h>
22 #include <errno.h>
23 
24 #define MAX_SEMS 250
25 
26 namespace carma
27 {
28  namespace util
29  {
30  class SemaphoreOperator
31  {
32  public:
33  // TODO, have a static definition of semnums associated
34  // with particular classes of Lock/Unlocks?
35  SemaphoreOperator( int semnum );
36 
37  // TODO, Create framework to remove semaphore when not in use
38  ~SemaphoreOperator() {};
39 
40  void Lock( struct timespec *timeout = NULL );
41  void Unlock( struct timespec *timeout = NULL );
42 
43  private:
44 
45  static const key_t _key;
46  int _semid;
47  int _semnum;
48 
49  struct sembuf _semop;
50 
51 #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
52  /* union semun is defined by including <sys/sem.h> */
53 #else
54  /* according to X/OPEN we have to define it ourselves */
55  union semun
56  {
57  int val; /* value for SETVAL */
58  struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
59  unsigned short *array; /* array for GETALL, SETALL */
60  /* Linux specific part: */
61  struct seminfo *__buf; /* buffer for IPC_INFO */
62  } _arg;
63 #endif // semun undefined
64  };
65  }
66 }
67 
68 
69 
70 #endif // CARMA_UTIL_SEMAPHORE_OPS_H