NVIDIA Iray API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
condition.h
Go to the documentation of this file.
1 //*****************************************************************************
2 // Copyright 1986, 2014 NVIDIA Corporation. All rights reserved.
3 //*****************************************************************************
9 //*****************************************************************************
10 
11 #ifndef MI_BASE_CONDITION_H
12 #define MI_BASE_CONDITION_H
13 
14 #include <mi/base/config.h>
15 
16 #ifndef MI_PLATFORM_WINDOWS
17 #include <pthread.h>
18 #else
19 #include <mi/base/miwindows.h>
20 #endif
21 
22 namespace mi {
23 
24 namespace base {
25 
30 class Condition
32 {
33 public:
36  {
37 #ifndef MI_PLATFORM_WINDOWS
38  m_signaled = false;
39  pthread_mutex_init(&m_mutex,NULL);
40  pthread_cond_init(&m_condvar,NULL);
41 #else
42  m_handle = CreateEvent(NULL, false, false, NULL);
43 #endif
44  }
45 
48  {
49 #ifndef MI_PLATFORM_WINDOWS
50  pthread_mutex_destroy(&m_mutex);
51  pthread_cond_destroy(&m_condvar);
52 #else
53  CloseHandle(m_handle);
54 #endif
55  }
56 
60  void wait()
61  {
62 #ifndef MI_PLATFORM_WINDOWS
63  pthread_mutex_lock(&m_mutex);
64  while(!m_signaled)
65  pthread_cond_wait(&m_condvar,&m_mutex);
66  m_signaled = false;
67  pthread_mutex_unlock(&m_mutex);
68 #else
69  WaitForSingleObject(m_handle, INFINITE);
70 #endif
71  }
72 
81  void signal()
82  {
83 #ifndef MI_PLATFORM_WINDOWS
84  pthread_mutex_lock(&m_mutex);
85  m_signaled = true;
86  pthread_cond_signal(&m_condvar);
87  pthread_mutex_unlock(&m_mutex);
88 #else
89  SetEvent(m_handle);
90 #endif
91  }
92 
93 private:
94 #ifndef MI_PLATFORM_WINDOWS
95  pthread_mutex_t m_mutex;
98  pthread_cond_t m_condvar;
100  bool m_signaled;
101 #else
102  HANDLE m_handle;
104 #endif
105 };
106  // end group mi_base_threads
108 
109 } // namespace base
110 
111 } // namespace mi
112 
113 #endif // MI_BASE_CONDITION_H