Event.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: Event.cpp
37 // Author: Kai Welke
38 // Date: 13.01.2006
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
48 #include "Event.h"
49 
50 #include "Threading.h"
51 
52 #include <stdio.h>
53 
54 
55 
56 // ****************************************************************************
57 // Constructor / Destructor
58 // ****************************************************************************
59 
61 {
62 #ifdef WIN32
63  InitializeCriticalSection(&m_CSWindowsMutex);
64  m_SignalEvent = CreateEvent(NULL,false,false,NULL);
65 #else
66  pthread_mutexattr_init(&m_MutexAttr);
67  pthread_mutex_init(&m_PosixMutex ,&m_MutexAttr);
68  pthread_condattr_init(&m_ConditionalAttr);
69  pthread_cond_init(&m_PosixConditional,&m_ConditionalAttr);
70 
71  m_bSignaled = false;
72 #endif
73 }
74 
76 {
77 #ifdef WIN32
78  DeleteCriticalSection(&m_CSWindowsMutex);
79  CloseHandle(m_SignalEvent);
80 #else
81  pthread_mutex_destroy(&m_PosixMutex);
82  pthread_mutexattr_destroy(&m_MutexAttr);
83  pthread_cond_destroy(&m_PosixConditional);
84  pthread_condattr_destroy(&m_ConditionalAttr);
85 #endif
86 }
87 
88 
89 // ****************************************************************************
90 // Methods
91 // ****************************************************************************
93 {
94 #ifdef WIN32
95  ResetEvent(m_SignalEvent);
96 #else
97  pthread_mutex_lock(&m_PosixMutex);
98  m_bSignaled = false;
99  pthread_mutex_unlock(&m_PosixMutex);
100 
101  pthread_cond_signal(&m_PosixConditional);
102 #endif
103 }
104 
106 {
107 #ifdef WIN32
108  int nWaitMS;
109  if (nMS == Threading::WAIT_INFINITE)
110  nWaitMS = INFINITE;
111  else
112  nWaitMS = nMS;
113 
114  if (WaitForSingleObject(m_SignalEvent, nWaitMS) == WAIT_OBJECT_0)
116  else
118 #else
119  // lock mutex for signal
120  pthread_mutex_lock(&m_PosixMutex);
121 
122  // when we are not signalled, we wait for signal
123  if (!m_bSignaled)
124  {
125  if (nMS == Threading::WAIT_INFINITE) // wait infinite
126  {
127  // have to do a loop, because linux sometimes messes other signals to this one
128  while (!m_bSignaled)
129  {
130  pthread_cond_wait(&m_PosixConditional, &m_PosixMutex);
131  }
132 
133  // we consumed the signal
134  m_bSignaled = false;
135 
136  // mutex can be unlocked
137  pthread_mutex_unlock(&m_PosixMutex);
138 
139  // return with success
141  }
142  else // wait with timeout
143  {
144  // set timer structure
145  time_t current = time(NULL);
146  timespec uni_ts;
147  int nSeconds = nMS / 1000;
148  if (nSeconds == 0)
149  {
150  nSeconds = 1;
151  nMS = 0;
152  }
153  else
154  {
155  nMS -= nSeconds * 1000;
156  }
157 
158  uni_ts.tv_sec = current + nSeconds;
159  uni_ts.tv_nsec = nMS * 1000;
160 
161 
162  if (pthread_cond_timedwait(&m_PosixConditional, &m_PosixMutex, &uni_ts) == 0)
163  {
164  if (m_bSignaled)
165  {
166  m_bSignaled = false;
167  pthread_mutex_unlock(&m_PosixMutex);
169  }
170  else
171  {
172  pthread_mutex_unlock(&m_PosixMutex);
174  }
175  }
176  else
177  {
178  m_bSignaled = false;
179  pthread_mutex_unlock(&m_PosixMutex);
180 
181  // perhaps we didnt enter idle so yield
183 
185  }
186  }
187 
188 
189  }
190  else
191  {
192  // consume signal
193  m_bSignaled = false;
194 
195  // unlock signal mutex
196  pthread_mutex_unlock(&m_PosixMutex);
197 
198  // we didnt enter idle so yield
200  }
201 
203 #endif
204 }
205 
207 {
208 #ifdef WIN32
209  SetEvent(m_SignalEvent);
210 #else
211  pthread_mutex_lock(&m_PosixMutex);
212  m_bSignaled = true;
213  pthread_mutex_unlock(&m_PosixMutex);
214 
215  pthread_cond_signal(&m_PosixConditional);
216 #endif
217 }
void YieldThread()
Definition: Threading.cpp:67
const int WAIT_INFINITE
Definition: Threading.h:59
Threading::EEventStatus Wait(int nMS=Threading::WAIT_INFINITE)
Definition: Event.cpp:105
CEvent()
Definition: Event.cpp:60
~CEvent()
Definition: Event.cpp:75
void Signal()
Definition: Event.cpp:206
void Reset()
Definition: Event.cpp:92
pthread_mutexattr_t m_MutexAttr
Definition: Event.h:93
bool m_bSignaled
Definition: Event.h:97
pthread_mutex_t m_PosixMutex
Definition: Event.h:92
pthread_cond_t m_PosixConditional
Definition: Event.h:94
pthread_condattr_t m_ConditionalAttr
Definition: Event.h:95


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:27