Robotics Library  0.6.2
Semaphore.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009, Markus Rickert
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 // POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 #ifndef _RL_UTIL_SEMAPHORE_H_
28 #define _RL_UTIL_SEMAPHORE_H_
29 
30 #include <cstdlib>
31 
32 #ifdef WIN32
33 #include <windows.h>
34 #else // WIN32
35 #include <semaphore.h>
36 #endif // WIN32
37 
38 namespace rl
39 {
40  namespace util
41  {
42  class Semaphore
43  {
44  public:
45  explicit Semaphore(const ::std::size_t& n) :
46 #ifdef WIN32
47  semaphore(CreateSemaphore(NULL, n, n, NULL))
48 #else // WIN32
49  semaphore()
50 #endif // WIN32
51  {
52 #ifndef WIN32
53  sem_init(&(*this).semaphore, 0, n);
54 #endif // WIN32
55  }
56 
57  virtual ~Semaphore()
58  {
59 #ifdef WIN32
60  CloseHandle((*this).semaphore);
61 #else // WIN32
62  sem_destroy(&(*this).semaphore);
63 #endif // WIN32
64  }
65 
67  {
68  (*this).release();
69  return (*this);
70  }
71 
73  {
74  (*this).acquire();
75  return (*this);
76  }
77 
78  void acquire()
79  {
80 #ifdef WIN32
81  WaitForSingleObject((*this).semaphore, INFINITE);
82 #else // WIN32
83  sem_wait(&(*this).semaphore);
84 #endif // WIN32
85  }
86 
87  void release()
88  {
89 #ifdef WIN32
90  ReleaseSemaphore((*this).semaphore, 1, NULL);
91 #else // WIN32
92  sem_post(&(*this).semaphore);
93 #endif // WIN32
94  }
95 
96  bool tryAcquire()
97  {
98 #ifdef WIN32
99  return WaitForSingleObject((*this).semaphore, 0) == WAIT_OBJECT_0 ? false : true;
100 #else // WIN32
101  return sem_trywait(&(*this).semaphore) == 0 ? true : false;
102 #endif // WIN32
103  }
104 
105  protected:
106 
107  private:
108 #ifdef WIN32
109  HANDLE semaphore;
110 #else // WIN32
111  sem_t semaphore;
112 #endif // WIN32
113  };
114  }
115 }
116 
117 #endif // _RL_UTIL_SEMAPHORE_H_
rl::util::Semaphore::operator--
Semaphore & operator--()
Definition: Semaphore.h:72
rl::util::Semaphore::Semaphore
Semaphore(const ::std::size_t &n)
Definition: Semaphore.h:45
rl::util::Semaphore::release
void release()
Definition: Semaphore.h:87
rl::util::Semaphore::~Semaphore
virtual ~Semaphore()
Definition: Semaphore.h:57
rl::util::Semaphore::operator++
Semaphore & operator++()
Definition: Semaphore.h:66
rl::util::Semaphore::semaphore
sem_t semaphore
Definition: Semaphore.h:111
rl::util::Semaphore::acquire
void acquire()
Definition: Semaphore.h:78
rl::util::Semaphore
Definition: Semaphore.h:43
rl::util::Semaphore::tryAcquire
bool tryAcquire()
Definition: Semaphore.h:96
rl
Definition: Ati.cpp:35