Argus Camera Sample
Argus Camera Sample
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConditionVariable.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions
5  * are met:
6  * * Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of NVIDIA CORPORATION nor the names of its
12  * contributors may be used to endorse or promote products derived
13  * from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "ConditionVariable.h"
29 #include "Mutex.h"
30 #include "Error.h"
31 
32 namespace ArgusSamples
33 {
34 
36  : m_initialized(false)
37 {
38 }
39 
41 {
42  shutdown();
43 }
44 
46 {
47  if (!m_initialized)
48  {
49  if (pthread_cond_init(&m_cond, NULL) != 0)
50  ORIGINATE_ERROR("Failed to initialize condition variable");
51  m_initialized = true;
52  }
53 
54  return true;
55 }
56 
58 {
59  if (m_initialized)
60  {
61  if (pthread_cond_destroy(&m_cond) != 0)
62  ORIGINATE_ERROR("Failed to destroy condition variable");
63  m_initialized = false;
64  }
65 
66  return true;
67 }
68 
70 {
71  if (!m_initialized)
72  ORIGINATE_ERROR("Not initialized");
73 
74  if (pthread_cond_broadcast(&m_cond) != 0)
75  ORIGINATE_ERROR("pthread_cond_broadcast failed");
76  return true;
77 }
78 
80 {
81  if (!m_initialized)
82  ORIGINATE_ERROR("Not initialized");
83 
84  if (pthread_cond_signal(&m_cond) != 0)
85  ORIGINATE_ERROR("pthread_cond_signal failed");
86  return true;
87 }
88 
89 bool ConditionVariable::wait(const Mutex& mutex) const
90 {
91  if (!m_initialized)
92  ORIGINATE_ERROR("Not initialized");
93 
94  if (pthread_cond_wait(&m_cond, mutex.getPThreadMutex()) != 0)
95  ORIGINATE_ERROR("pthread_cond_signal failed");
96  return true;
97 }
98 
99 }; // namespace ArgusSamples