WPILib  2014.0
WPIRoboticsLibraryforFRC
 All Classes Functions Variables Pages
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 SYNCHRONIZED_H
8 #define SYNCHRONIZED_H
9 
10 #include <semLib.h>
11 
12 #include "Base.h"
13 
14 #define CRITICAL_REGION(s) { Synchronized _sync(s);
15 #define END_REGION }
16 
17 class Synchronized;
18 
32 {
33 public:
34  explicit ReentrantSemaphore() {
35  m_semaphore = semMCreate(SEM_Q_PRIORITY | SEM_DELETE_SAFE);
36  }
38  semDelete(m_semaphore);
39  }
40 
45  int take() {
46  return semTake(m_semaphore, WAIT_FOREVER);
47  }
48 
53  int give() {
54  return semGive(m_semaphore);
55  }
56 
57 private:
58  SEM_ID m_semaphore;
59 
60  friend class Synchronized;
61  DISALLOW_COPY_AND_ASSIGN(ReentrantSemaphore);
62 };
63 
83 {
84 public:
85  explicit Synchronized(SEM_ID);
87  virtual ~Synchronized();
88 private:
89  SEM_ID m_semaphore;
90 
91  DISALLOW_COPY_AND_ASSIGN(Synchronized);
92 };
93 
94 #endif
Definition: Synchronized.h:82
Synchronized(SEM_ID)
Definition: Synchronized.cpp:17
int give()
Definition: Synchronized.h:53
int take()
Definition: Synchronized.h:45
Definition: Synchronized.h:31
virtual ~Synchronized()
Definition: Synchronized.cpp:32