Argus Camera Sample
Argus Camera Sample
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Mutex.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA CORPORATION nor the names of its
13  * contributors may be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef MUTEX_H
30 #define MUTEX_H
31 
32 #include <pthread.h>
33 
34 #include "Error.h"
35 
36 namespace ArgusSamples
37 {
38 
39 class ConditionVariable;
40 
41 /**
42  * Mutex
43  */
44 class Mutex
45 {
46 public:
47  Mutex();
48  ~Mutex();
49 
50  /**
51  * Create the underlying mutex. This method must be called before any other methods.
52  */
53  bool initialize();
54 
55  /**
56  * Destroy the underlying mutex. After this call, this object can no longer be used
57  * (until and unless a future call to @c initialize()). Calling this method if the
58  * object is not initialized generates no error, but silently returns.
59  */
60  bool shutdown();
61 
62  /**
63  * Lock the mutex. This method is declared @c const for convenience.
64  */
65  bool lock() const;
66 
67  /**
68  * Unlock the mutex. This method is declared @c const for convenience.
69  */
70  bool unlock() const;
71 
72 private:
74  /**
75  * pthread mutex, this is 'mutable' so that 'const' functions can be used.
76  */
77  mutable pthread_mutex_t m_mutex;
78 
79  /**
80  * Hide copy constructor and assignment operator
81  */
82  Mutex(Mutex &other);
83  const Mutex& operator = (const Mutex&);
84 
85  friend class ConditionVariable;
86 
87  pthread_mutex_t* getPThreadMutex() const
88  {
89  return &m_mutex;
90  }
91 };
92 
93 /**
94  * An RAII-style class for acquiring a Mutex.
95  * The mutex is acquired in the constructor and released in the destructor.
96  * This class is NOT to be subclassed.
97  */
99 {
100 public:
101  explicit ScopedMutex(Mutex& mutex)
102  : m_mutex(&mutex)
103  , m_isLocked(false)
104  {
105  m_isLocked = m_mutex->lock();
106  }
107 
109  {
110  if (m_isLocked)
111  m_mutex->unlock();
112  }
113 
114  bool expectLocked() const
115  {
116  if (!m_isLocked)
117  ORIGINATE_ERROR("Expected mutex to be locked");
118  return true;
119  }
120 
121 private:
124 
125  /**
126  * Hide default/copy constructor and assignment operator
127  */
128  ScopedMutex();
129  ScopedMutex(ScopedMutex &other);
131 };
132 
133 } // namespace ArgusSamples
134 
135 #endif // MUTEX_H