JeVois  1.3
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Semaphore.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  copyright GNU Public License (GPL v3)
22  // ////////////////////////////////////////////////////////////////////////
23  // The iLab Neuromorphic Robotics Toolkit (NRT) //
24  // Copyright 2010-2012 by the University of Southern California (USC) //
25  // and the iLab at USC. //
26  // //
27  // iLab - University of Southern California //
28  // Hedco Neurociences Building, Room HNB-10 //
29  // Los Angeles, Ca 90089-2520 - USA //
30  // //
31  // See http://ilab.usc.edu for information about this project. //
32  // ////////////////////////////////////////////////////////////////////////
33  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
34  // //
35  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
36  // redistribute it and/or modify it under the terms of the GNU General //
37  // Public License as published by the Free Software Foundation, either //
38  // version 3 of the License, or (at your option) any later version. //
39  // //
40  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
41  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
42  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
43  // PURPOSE. See the GNU General Public License for more details. //
44  // //
45  // You should have received a copy of the GNU General Public License //
46  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
47  // <http://www.gnu.org/licenses/>. //
48  // ////////////////////////////////////////////////////////////////////////
49 */
50 
51 #pragma once
52 
53 #include <thread>
54 #include <condition_variable>
56 
57 namespace jevois
58 {
59  //! A simple semaphore
60  /*! A semaphore is a thread-safe mechanism to share resources between multiple threads of execution.
61 
62  See this Wikipedia page for basic information: http://en.wikipedia.org/wiki/Semaphore_%28programming%29
63 
64  This code is derived from the Neuromorphic Robotics Toolkit (NRT).
65 
66  @tparam BB defines the blocking behavior and should be a value from jevois::BlockingBehavior
67 
68  \ingroup types */
69  template <BlockingBehavior BB>
70  class Semaphore
71  {
72  public:
73  //! Create a semaphore with n initial resources
74  Semaphore(size_t n = 0);
75 
76  //! Release n resources to the semaphore
77  void increment(size_t n);
78 
79  //! Remove n resources from the semaphore, blocking until they are available or throwing if they are not
80  void decrement(size_t n);
81 
82  //! Get the current count
83  size_t count() const;
84 
85  private:
86  size_t itsCount;
87  mutable std::mutex itsMutex;
88  std::condition_variable itsCondVar;
89  };
90 } // namespace jevois
91 
92 // Include implementation details of no interest to users:
A simple semaphore.
Definition: Semaphore.H:70
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.