VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gsemaphore.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gsemaphore.h
19 ///
20 
21 #ifndef G_SEMAPHORE__H
22 #define G_SEMAPHORE__H
23 
24 #include "gdef.h"
25 #include "gexception.h"
26 
27 namespace G
28 {
29  class Semaphore ;
30  class SemaphoreImp ;
31 }
32 
33 /// \class G::Semaphore
34 /// A semaphore class with a posix or sysv implementation chosen at build-time.
35 /// Eg.
36 /// \code
37 /// struct Memory { Semaphore::storage_type sem_storage ; int value ; } ;
38 /// Memory * mem = static_cast<Memory*>( mem_p ) ;
39 /// Semaphore * s = new (&mem->sem_storage) Semaphore ; // creator - placement new
40 /// Semaphore * s = Semaphore::at(&mem->sem_storage) ; // user
41 /// s->decrement() ; // lock - may block
42 /// mem->value = 999 ;
43 /// s->increment() ; // unlock
44 /// s->~Semaphore() ; // creator - placement delete
45 /// \endcode
46 ///
48 {
49 public:
50  G_EXCEPTION( Error , "semaphore error" ) ;
51  struct storage_type /// semaphore storage
52  { unsigned long filler[4] ; } ; // adjust the size wrt. sem_t as necessary
53 
54  explicit Semaphore( unsigned int initial_value = 1U ) ;
55  ///< Constructor for a new anonymous semaphore, typically located
56  ///< inside a shared memory segment using "placement new".
57  ///< The initial value should be one for a mutex.
58 
59  ~Semaphore() ;
60  ///< Destroys the semaphore. Results are undefined if there
61  ///< are waiters.
62 
63  static Semaphore * at( storage_type * ) ;
64  ///< Syntactic sugar to return an object pointer corresponding
65  ///< to the given storage pointer.
66 
67  void increment() ;
68  ///< Increment operator. Used for mutex unlocking.
69 
70  void decrement() ;
71  ///< Decrement-but-block-if-zero operator.
72  ///< Used for mutex locking.
73 
74  bool decrement( int timeout_s ) ;
75  ///< Overload with a timeout. Returns true if decremented.
76 
77 private:
78  Semaphore( const Semaphore & ) ;
79  void operator=( const Semaphore & ) ;
80 
81 private:
82  friend class G::SemaphoreImp ;
83  storage_type m_storage ;
84 } ;
85 
86 #endif
void decrement()
Decrement-but-block-if-zero operator.
static Semaphore * at(storage_type *)
Syntactic sugar to return an object pointer corresponding to the given storage pointer.
void increment()
Increment operator. Used for mutex unlocking.
A semaphore class with a posix or sysv implementation chosen at build-time.
Definition: gsemaphore.h:47
~Semaphore()
Destroys the semaphore.
semaphore storage
Definition: gsemaphore.h:51
Semaphore(unsigned int initial_value=1U)
Constructor for a new anonymous semaphore, typically located inside a shared memory segment using "pl...