CARMA C++
SemaphoreOperator.h
Go to the documentation of this file.
1 
2 
13 #ifndef CARMA_BIMA_SEMAPHORE_OPS_H
14 #define CARMA_BIMA_SEMAPHORE_OPS_H
15 
16 #include <map>
17 #include <sstream>
18 
19 #include <fcntl.h>
20 #include <sys/types.h>
21 #include <sys/ipc.h>
22 #include <sys/sem.h>
23 #include <errno.h>
24 
25 #define MAX_SEMS 250
26 
27 namespace carma
28 {
29  namespace antenna
30  {
31  namespace bima
32  {
33  class SemaphoreOperator
34  {
35  public:
36  SemaphoreOperator( bool server = false );
37  ~SemaphoreOperator();
38 
39  void semOpIdToSemIdAndNum( int semOpId,
40  int &semid,
41  unsigned short &semnum );
42 
43  void update( const std::string & name );
44  void updateStale();
45  void waitOn( const std::string & name );
46  void checkAndUpdate( const std::string & name );
47  void addNameAndId( const std::string & name, unsigned short id );
48  unsigned short getSemNum( const std::string & name );
49 
50  private:
51  std::map<std::string, unsigned short> _nameToSemOpId;
52 
53  std::map<std::string, int> _staleList;
54  int _staleCheck;
55 
56  // Unique sem keys follow
57  static const key_t _key[4];
58  // 1000 semnums default (4 x 250 semnums on 2.4.25 )
59  int _semid[4];
60 
61 #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
62  /* union semun is defined by including <sys/sem.h> */
63 #else
64  /* according to X/OPEN we have to define it ourselves */
65  union semun
66  {
67  int val; /* value for SETVAL */
68  struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
69  unsigned short *array; /* array for GETALL, SETALL */
70  /* Linux specific part: */
71  struct seminfo *__buf; /* buffer for IPC_INFO */
72  } _arg;
73 #endif // semun undefined
74  };
75  }
76  }
77 }
78 
79 
80 
81 #endif // CARMA_BIMA_SEMAPHORE_OPS_H