Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
condition_variable.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2019 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 
16 
17 
18 
19 */
20 
21 #include "tbb/tbb_config.h"
22 #include "tbb/compat/condition_variable"
23 #include "tbb/atomic.h"
24 #include "tbb_misc.h"
25 #include "dynamic_link.h"
26 #include "itt_notify.h"
27 
28 namespace tbb {
29 
30 namespace internal {
31 
32 //condition_variable
33 #if _WIN32||_WIN64
34 using tbb::interface5::internal::condition_variable_using_event;
35 
36 static atomic<do_once_state> condvar_api_state;
37 
38 void WINAPI init_condvar_using_event( condition_variable_using_event* cv_event )
39 {
40  // TODO: For Metro port, we can always use the API for condition variables, without dynamic_link etc.
41  cv_event->event = CreateEventEx(NULL, NULL, 0x1 /*CREATE_EVENT_MANUAL_RESET*/, EVENT_ALL_ACCESS );
42  InitializeCriticalSectionEx( &cv_event->mutex, 4000, 0 );
43  cv_event->n_waiters = 0;
44  cv_event->release_count = 0;
45  cv_event->epoch = 0;
46 }
47 
48 BOOL WINAPI sleep_condition_variable_cs_using_event( condition_variable_using_event* cv_event, LPCRITICAL_SECTION cs, DWORD dwMilliseconds )
49 {
50  EnterCriticalSection( &cv_event->mutex );
51  ++cv_event->n_waiters;
52  unsigned my_generation = cv_event->epoch;
53  LeaveCriticalSection( &cv_event->mutex );
54  LeaveCriticalSection( cs );
55  for (;;) {
56  // should come here at least once
57  DWORD rc = WaitForSingleObjectEx( cv_event->event, dwMilliseconds, FALSE );
58  EnterCriticalSection( &cv_event->mutex );
59  if( rc!=WAIT_OBJECT_0 ) {
60  --cv_event->n_waiters;
61  LeaveCriticalSection( &cv_event->mutex );
62  if( rc==WAIT_TIMEOUT ) {
63  SetLastError( WAIT_TIMEOUT );
64  EnterCriticalSection( cs );
65  }
66  return false;
67  }
68  __TBB_ASSERT( rc==WAIT_OBJECT_0, NULL );
69  if( cv_event->release_count>0 && cv_event->epoch!=my_generation )
70  break;
71  LeaveCriticalSection( &cv_event->mutex );
72  }
73 
74  // still in the critical section
75  --cv_event->n_waiters;
76  int count = --cv_event->release_count;
77  LeaveCriticalSection( &cv_event->mutex );
78 
79  if( count==0 ) {
80  __TBB_ASSERT( cv_event->event, "Premature destruction of condition variable?" );
81  ResetEvent( cv_event->event );
82  }
83  EnterCriticalSection( cs );
84  return true;
85 }
86 
87 void WINAPI wake_condition_variable_using_event( condition_variable_using_event* cv_event )
88 {
89  EnterCriticalSection( &cv_event->mutex );
90  if( cv_event->n_waiters>cv_event->release_count ) {
91  SetEvent( cv_event->event ); // Signal the manual-reset event.
92  ++cv_event->release_count;
93  ++cv_event->epoch;
94  }
95  LeaveCriticalSection( &cv_event->mutex );
96 }
97 
98 void WINAPI wake_all_condition_variable_using_event( condition_variable_using_event* cv_event )
99 {
100  EnterCriticalSection( &cv_event->mutex );
101  if( cv_event->n_waiters>0 ) {
102  SetEvent( cv_event->event );
103  cv_event->release_count = cv_event->n_waiters;
104  ++cv_event->epoch;
105  }
106  LeaveCriticalSection( &cv_event->mutex );
107 }
108 
109 void WINAPI destroy_condvar_using_event( condition_variable_using_event* cv_event )
110 {
111  HANDLE my_event = cv_event->event;
112  EnterCriticalSection( &cv_event->mutex );
113  // NULL is an invalid HANDLE value
114  cv_event->event = NULL;
115  if( cv_event->n_waiters>0 ) {
116  LeaveCriticalSection( &cv_event->mutex );
117  spin_wait_until_eq( cv_event->n_waiters, 0 );
118  // make sure the last thread completes its access to cv
119  EnterCriticalSection( &cv_event->mutex );
120  }
121  LeaveCriticalSection( &cv_event->mutex );
122  CloseHandle( my_event );
123 }
124 
125 void WINAPI destroy_condvar_noop( CONDITION_VARIABLE* /*cv*/ ) { /*no op*/ }
126 
127 static void (WINAPI *__TBB_init_condvar)( PCONDITION_VARIABLE ) = (void (WINAPI *)(PCONDITION_VARIABLE))&init_condvar_using_event;
128 static BOOL (WINAPI *__TBB_condvar_wait)( PCONDITION_VARIABLE, LPCRITICAL_SECTION, DWORD ) = (BOOL (WINAPI *)(PCONDITION_VARIABLE,LPCRITICAL_SECTION, DWORD))&sleep_condition_variable_cs_using_event;
129 static void (WINAPI *__TBB_condvar_notify_one)( PCONDITION_VARIABLE ) = (void (WINAPI *)(PCONDITION_VARIABLE))&wake_condition_variable_using_event;
130 static void (WINAPI *__TBB_condvar_notify_all)( PCONDITION_VARIABLE ) = (void (WINAPI *)(PCONDITION_VARIABLE))&wake_all_condition_variable_using_event;
131 static void (WINAPI *__TBB_destroy_condvar)( PCONDITION_VARIABLE ) = (void (WINAPI *)(PCONDITION_VARIABLE))&destroy_condvar_using_event;
132 
134 static const dynamic_link_descriptor CondVarLinkTable[] = {
135  DLD(InitializeConditionVariable, __TBB_init_condvar),
136  DLD(SleepConditionVariableCS, __TBB_condvar_wait),
137  DLD(WakeConditionVariable, __TBB_condvar_notify_one),
138  DLD(WakeAllConditionVariable, __TBB_condvar_notify_all)
139 };
140 
141 void init_condvar_module()
142 {
143  __TBB_ASSERT( (uintptr_t)__TBB_init_condvar==(uintptr_t)&init_condvar_using_event, NULL );
144 #if __TBB_WIN8UI_SUPPORT
145  // We expect condition variables to be always available for Windows* store applications,
146  // so there is no need to check presense and use alternative implementation.
147  __TBB_init_condvar = (void (WINAPI *)(PCONDITION_VARIABLE))&InitializeConditionVariable;
148  __TBB_condvar_wait = (BOOL(WINAPI *)(PCONDITION_VARIABLE, LPCRITICAL_SECTION, DWORD))&SleepConditionVariableCS;
149  __TBB_condvar_notify_one = (void (WINAPI *)(PCONDITION_VARIABLE))&WakeConditionVariable;
150  __TBB_condvar_notify_all = (void (WINAPI *)(PCONDITION_VARIABLE))&WakeAllConditionVariable;
151  __TBB_destroy_condvar = (void (WINAPI *)(PCONDITION_VARIABLE))&destroy_condvar_noop;
152 #else
153  if (dynamic_link("Kernel32.dll", CondVarLinkTable, 4))
154  __TBB_destroy_condvar = (void (WINAPI *)(PCONDITION_VARIABLE))&destroy_condvar_noop;
155 #endif
156 }
157 #endif /* _WIN32||_WIN64 */
158 
159 } // namespace internal
160 
161 #if _WIN32||_WIN64
162 
163 namespace interface5 {
164 namespace internal {
165 
166 using tbb::internal::condvar_api_state;
167 using tbb::internal::__TBB_init_condvar;
168 using tbb::internal::__TBB_condvar_wait;
169 using tbb::internal::__TBB_condvar_notify_one;
170 using tbb::internal::__TBB_condvar_notify_all;
171 using tbb::internal::__TBB_destroy_condvar;
172 using tbb::internal::init_condvar_module;
173 
174 void internal_initialize_condition_variable( condvar_impl_t& cv )
175 {
176  atomic_do_once( &init_condvar_module, condvar_api_state );
177  __TBB_init_condvar( &cv.cv_native );
178 }
179 
180 void internal_destroy_condition_variable( condvar_impl_t& cv )
181 {
182  __TBB_destroy_condvar( &cv.cv_native );
183 }
184 
185 void internal_condition_variable_notify_one( condvar_impl_t& cv )
186 {
187  __TBB_condvar_notify_one ( &cv.cv_native );
188 }
189 
190 void internal_condition_variable_notify_all( condvar_impl_t& cv )
191 {
192  __TBB_condvar_notify_all( &cv.cv_native );
193 }
194 
195 bool internal_condition_variable_wait( condvar_impl_t& cv, mutex* mtx, const tick_count::interval_t* i )
196 {
197  DWORD duration = i ? DWORD((i->seconds()*1000)) : INFINITE;
198  mtx->set_state( mutex::INITIALIZED );
199  BOOL res = __TBB_condvar_wait( &cv.cv_native, mtx->native_handle(), duration );
200  mtx->set_state( mutex::HELD );
201  return res?true:false;
202 }
203 
204 } // namespace internal
205 } // nameespace interface5
206 
207 #endif /* _WIN32||_WIN64 */
208 
209 } // namespace tbb
#define DLD(s, h)
The helper to construct dynamic_link_descriptor structure.
Definition: dynamic_link.h:60
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
Association between a handler name and location of pointer to it.
Definition: dynamic_link.h:64
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t count
OPEN_INTERNAL_NAMESPACE bool dynamic_link(const char *, const dynamic_link_descriptor *, size_t, dynamic_link_handle *handle, int)
The graph class.
void atomic_do_once(const F &initializer, atomic< do_once_state > &state)
One-time initialization function.
Definition: tbb_misc.h:210
void spin_wait_until_eq(const volatile T &location, const U value)
Spin UNTIL the value of the variable is equal to a given value.
Definition: tbb_machine.h:403

Copyright © 2005-2019 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.