JeVois  1.0
JeVois Smart Embedded Machine Vision Toolkit
SemaphoreImpl.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 // This file has been modified / imported from the Neuromorphic Robotics Toolkit (NRT). Original copyright is:
19 
20 /* author Randolph Voorhies
21  // ////////////////////////////////////////////////////////////////////////
22  // The iLab Neuromorphic Robotics Toolkit (NRT) //
23  // Copyright 2010-2012 by the University of Southern California (USC) //
24  // and the iLab at USC. //
25  // //
26  // iLab - University of Southern California //
27  // Hedco Neurociences Building, Room HNB-10 //
28  // Los Angeles, Ca 90089-2520 - USA //
29  // //
30  // See http://ilab.usc.edu for information about this project. //
31  // ////////////////////////////////////////////////////////////////////////
32  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
33  // //
34  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
35  // redistribute it and/or modify it under the terms of the GNU General //
36  // Public License as published by the Free Software Foundation, either //
37  // version 3 of the License, or (at your option) any later version. //
38  // //
39  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
40  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
41  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
42  // PURPOSE. See the GNU General Public License for more details. //
43  // //
44  // You should have received a copy of the GNU General Public License //
45  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
46  // <http://www.gnu.org/licenses/>. //
47  // ////////////////////////////////////////////////////////////////////////
48 */
49 
50 #pragma once
51 
52 // ##############################################################################################################
53 template <jevois::BlockingBehavior BB> inline
55  itsCount(n)
56 { }
57 
58 // ##############################################################################################################
59 template <jevois::BlockingBehavior BB> inline
61 {
62  std::lock_guard<std::mutex> guard(itsMutex);
63  itsCount += n;
64  itsCondVar.notify_all();
65 }
66 
67 // Doxygen is confused by template specializations
68 #ifndef JEVOIS_DOXYGEN
69 
70 // ##############################################################################################################
71 namespace jevois
72 {
73  // Specialization of decrement() when our behavior is to block
74  template <> void Semaphore<BlockingBehavior::Block>::decrement(size_t n);
75 
76  // Specialization of decrement() when our behavior is to throw
77  template <> void Semaphore<BlockingBehavior::Throw>::decrement(size_t n);
78 }
79 
80 // ##############################################################################################################
81 // Specialization of decrement() when our behavior is to block
82 template <> inline
84 {
85  std::unique_lock<std::mutex> unique_lock(itsMutex);
86 
87  while (itsCount < n) itsCondVar.wait(unique_lock);
88 
89  itsCount -= n;
90 }
91 
92 // ##############################################################################################################
93 // Specialization of decrement() when our behavior is to throw
94 template <> inline
96 {
97  std::unique_lock<std::mutex> unique_lock(itsMutex);
98 
99  if (itsCount < n) throw std::runtime_error("Semaphore decrement failed");
100 
101  itsCount -= n;
102 }
103 
104 #endif // __JEVOIS_DOXYGEN__
105 
106 // ##############################################################################################################
107 template <jevois::BlockingBehavior BB> inline
109 {
110  std::lock_guard<std::mutex> guard(itsMutex);
111  return itsCount;
112 }
113 
void increment(size_t n)
Release n resources to the semaphore.
Definition: SemaphoreImpl.H:60
void decrement(size_t n)
Remove n resources from the semaphore, blocking until they are available or throwing if they are not...
Semaphore(size_t n=0)
Create a semaphore with n initial resources.
Definition: SemaphoreImpl.H:54
size_t count() const
Get the current count.