condition_variable.cpp
Go to the documentation of this file.
/*
Copyright (c) 2005-2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "tbb/tbb_config.h"
#include "tbb/compat/condition_variable"
#include "tbb/atomic.h"
#include "tbb_misc.h"
#include "dynamic_link.h"
#include "itt_notify.h"
namespace tbb {
namespace internal {
//condition_variable
#if _WIN32||_WIN64
using tbb::interface5::internal::condition_variable_using_event;
static atomic<do_once_state> condvar_api_state;
void WINAPI init_condvar_using_event( condition_variable_using_event* cv_event )
{
// TODO: For Metro port, we can always use the API for condition variables, without dynamic_link etc.
cv_event->event = CreateEventEx(NULL, NULL, 0x1 /*CREATE_EVENT_MANUAL_RESET*/, EVENT_ALL_ACCESS );
InitializeCriticalSectionEx( &cv_event->mutex, 4000, 0 );
cv_event->n_waiters = 0;
cv_event->release_count = 0;
cv_event->epoch = 0;
}
BOOL WINAPI sleep_condition_variable_cs_using_event( condition_variable_using_event* cv_event, LPCRITICAL_SECTION cs, DWORD dwMilliseconds )
{
EnterCriticalSection( &cv_event->mutex );
++cv_event->n_waiters;
unsigned my_generation = cv_event->epoch;
LeaveCriticalSection( &cv_event->mutex );
LeaveCriticalSection( cs );
for (;;) {
// should come here at least once
DWORD rc = WaitForSingleObjectEx( cv_event->event, dwMilliseconds, FALSE );
EnterCriticalSection( &cv_event->mutex );
if( rc!=WAIT_OBJECT_0 ) {
--cv_event->n_waiters;
LeaveCriticalSection( &cv_event->mutex );
if( rc==WAIT_TIMEOUT ) {
SetLastError( WAIT_TIMEOUT );
EnterCriticalSection( cs );
}
return false;
}
__TBB_ASSERT( rc==WAIT_OBJECT_0, NULL );
if( cv_event->release_count>0 && cv_event->epoch!=my_generation )
break;
LeaveCriticalSection( &cv_event->mutex );
}
// still in the critical section
--cv_event->n_waiters;
int count = --cv_event->release_count;
LeaveCriticalSection( &cv_event->mutex );
if( count==0 ) {
__TBB_ASSERT( cv_event->event, "Premature destruction of condition variable?" );
ResetEvent( cv_event->event );
}
EnterCriticalSection( cs );
return true;
}
void WINAPI wake_condition_variable_using_event( condition_variable_using_event* cv_event )
{
EnterCriticalSection( &cv_event->mutex );
if( cv_event->n_waiters>cv_event->release_count ) {
SetEvent( cv_event->event ); // Signal the manual-reset event.
++cv_event->release_count;
++cv_event->epoch;
}
LeaveCriticalSection( &cv_event->mutex );
}
void WINAPI wake_all_condition_variable_using_event( condition_variable_using_event* cv_event )
{
EnterCriticalSection( &cv_event->mutex );
if( cv_event->n_waiters>0 ) {
SetEvent( cv_event->event );
cv_event->release_count = cv_event->n_waiters;
++cv_event->epoch;
}
LeaveCriticalSection( &cv_event->mutex );
}
void WINAPI destroy_condvar_using_event( condition_variable_using_event* cv_event )
{
HANDLE my_event = cv_event->event;
EnterCriticalSection( &cv_event->mutex );
// NULL is an invalid HANDLE value
cv_event->event = NULL;
if( cv_event->n_waiters>0 ) {
LeaveCriticalSection( &cv_event->mutex );
spin_wait_until_eq( cv_event->n_waiters, 0 );
// make sure the last thread completes its access to cv
EnterCriticalSection( &cv_event->mutex );
}
LeaveCriticalSection( &cv_event->mutex );
CloseHandle( my_event );
}
void WINAPI destroy_condvar_noop( CONDITION_VARIABLE* /*cv*/ ) { /*no op*/ }
static void (WINAPI *__TBB_init_condvar)( PCONDITION_VARIABLE ) = (void (WINAPI *)(PCONDITION_VARIABLE))&init_condvar_using_event;
static BOOL (WINAPI *__TBB_condvar_wait)( PCONDITION_VARIABLE, LPCRITICAL_SECTION, DWORD ) = (BOOL (WINAPI *)(PCONDITION_VARIABLE,LPCRITICAL_SECTION, DWORD))&sleep_condition_variable_cs_using_event;
static void (WINAPI *__TBB_condvar_notify_one)( PCONDITION_VARIABLE ) = (void (WINAPI *)(PCONDITION_VARIABLE))&wake_condition_variable_using_event;
static void (WINAPI *__TBB_condvar_notify_all)( PCONDITION_VARIABLE ) = (void (WINAPI *)(PCONDITION_VARIABLE))&wake_all_condition_variable_using_event;
static void (WINAPI *__TBB_destroy_condvar)( PCONDITION_VARIABLE ) = (void (WINAPI *)(PCONDITION_VARIABLE))&destroy_condvar_using_event;
static const dynamic_link_descriptor CondVarLinkTable[] = {
DLD(InitializeConditionVariable, __TBB_init_condvar),
DLD(SleepConditionVariableCS, __TBB_condvar_wait),
DLD(WakeConditionVariable, __TBB_condvar_notify_one),
DLD(WakeAllConditionVariable, __TBB_condvar_notify_all)
};
void init_condvar_module()
{
__TBB_ASSERT( (uintptr_t)__TBB_init_condvar==(uintptr_t)&init_condvar_using_event, NULL );
#if __TBB_WIN8UI_SUPPORT
// We expect condition variables to be always available for Windows* store applications,
// so there is no need to check presence and use alternative implementation.
__TBB_init_condvar = (void (WINAPI *)(PCONDITION_VARIABLE))&InitializeConditionVariable;
__TBB_condvar_wait = (BOOL(WINAPI *)(PCONDITION_VARIABLE, LPCRITICAL_SECTION, DWORD))&SleepConditionVariableCS;
__TBB_condvar_notify_one = (void (WINAPI *)(PCONDITION_VARIABLE))&WakeConditionVariable;
__TBB_condvar_notify_all = (void (WINAPI *)(PCONDITION_VARIABLE))&WakeAllConditionVariable;
__TBB_destroy_condvar = (void (WINAPI *)(PCONDITION_VARIABLE))&destroy_condvar_noop;
#else
if (dynamic_link("Kernel32.dll", CondVarLinkTable, 4))
__TBB_destroy_condvar = (void (WINAPI *)(PCONDITION_VARIABLE))&destroy_condvar_noop;
#endif
}
#endif /* _WIN32||_WIN64 */
} // namespace internal
#if _WIN32||_WIN64
namespace interface5 {
namespace internal {
using tbb::internal::condvar_api_state;
using tbb::internal::__TBB_init_condvar;
using tbb::internal::__TBB_condvar_wait;
using tbb::internal::__TBB_condvar_notify_one;
using tbb::internal::__TBB_condvar_notify_all;
using tbb::internal::__TBB_destroy_condvar;
using tbb::internal::init_condvar_module;
void internal_initialize_condition_variable( condvar_impl_t& cv )
{
atomic_do_once( &init_condvar_module, condvar_api_state );
__TBB_init_condvar( &cv.cv_native );
}
void internal_destroy_condition_variable( condvar_impl_t& cv )
{
__TBB_destroy_condvar( &cv.cv_native );
}
void internal_condition_variable_notify_one( condvar_impl_t& cv )
{
__TBB_condvar_notify_one ( &cv.cv_native );
}
void internal_condition_variable_notify_all( condvar_impl_t& cv )
{
__TBB_condvar_notify_all( &cv.cv_native );
}
bool internal_condition_variable_wait( condvar_impl_t& cv, mutex* mtx, const tick_count::interval_t* i )
{
DWORD duration = i ? DWORD((i->seconds()*1000)) : INFINITE;
mtx->set_state( mutex::INITIALIZED );
BOOL res = __TBB_condvar_wait( &cv.cv_native, mtx->native_handle(), duration );
mtx->set_state( mutex::HELD );
return res?true:false;
}
} // namespace internal
} // nameespace interface5
#endif /* _WIN32||_WIN64 */
} // namespace tbb
internal
Definition: _flow_graph_async_msg_impl.h:24
void
void
Definition: ittnotify_static.h:91
__TBB_ASSERT
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
count
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
Definition: ittnotify_static.h:145
dynamic_link.h
dynamic_link
OPEN_INTERNAL_NAMESPACE bool dynamic_link(const char *, const dynamic_link_descriptor *, size_t, dynamic_link_handle *handle, int)
Definition: dynamic_link.cpp:564
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
itt_notify.h
DLD
#define DLD(s, h)
The helper to construct dynamic_link_descriptor structure.
Definition: dynamic_link.h:56
tbb_config.h
atomic.h
tbb_misc.h
tbb::internal::atomic_do_once
void atomic_do_once(const F &initializer, atomic< do_once_state > &state)
One-time initialization function.
Definition: tbb_misc.h:213
dynamic_link_descriptor
Association between a handler name and location of pointer to it.
Definition: dynamic_link.h:60
tbb::internal::spin_wait_until_eq
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:402

Copyright © 2005-2020 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.