Now you can download a copy of these docs so you can use them offline! Download now
OSAL/Synchronized.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
5 /*----------------------------------------------------------------------------*/
6 
7 #ifndef NT_SYNCHRONIZED_H
8 #define NT_SYNCHRONIZED_H
9 
10 #define NT_CRITICAL_REGION(s) { NTSynchronized _sync(s);
11 #define NT_END_REGION }
12 
13 #if (defined __vxworks || defined WIN32)
14 
15 #ifdef __vxworks
16 #include <vxWorks.h>
17 #endif
18 #include <semLib.h>
19 
21 {
22 public:
23  explicit NTReentrantSemaphore(){
24  m_semaphore = semMCreate(SEM_Q_PRIORITY | SEM_INVERSION_SAFE | SEM_DELETE_SAFE);
25  };
27  semDelete(m_semaphore);
28  };
29  void take(){
30  semTake(m_semaphore, WAIT_FOREVER);
31  };
32  void give(){
33  semGive(m_semaphore);
34  };
35 private:
36  SEM_ID m_semaphore;
37 };
38 
39 #else
40 
41 #include <pthread.h>
42 
44 {
45 public:
46  explicit NTReentrantSemaphore(){
47  pthread_mutexattr_init(&mta);
48  pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);
49  pthread_mutex_init(&m_semaphore, &mta);
50  };
52  pthread_mutex_unlock(&m_semaphore);
53  pthread_mutex_destroy(&m_semaphore);
54  };
55  void take(){
56  pthread_mutex_lock(&m_semaphore);
57  };
58  void give(){
59  pthread_mutex_unlock(&m_semaphore);
60  };
61 private:
62  pthread_mutexattr_t mta;
63  pthread_mutex_t m_semaphore;
64 };
65 #endif // __vxworks
66 
78 {
79 public:
81  //TODO remove vxworks SEM_ID support
82 #if (defined __vxworks || defined WIN32)
83  explicit NTSynchronized(SEM_ID);
84 #endif
85  virtual ~NTSynchronized();
86 private:
87 #if (defined __vxworks || defined WIN32)
88  bool usingSem;
89  NTReentrantSemaphore* m_sem;
90  SEM_ID m_semaphore;
91 #else
92  NTReentrantSemaphore& m_semaphore;
93 #endif
94 };
95 
96 
97 
98 #endif
virtual ~NTSynchronized()
NTSynchronized(NTReentrantSemaphore &)

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6