Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
semaphore.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 "semaphore.h"
22 #if __TBB_USE_SRWLOCK
23 #include "dynamic_link.h" // Refers to src/tbb, not include/tbb
24 #include "tbb_misc.h"
25 #endif
26 
27 namespace tbb {
28 namespace internal {
29 
30 // TODO: For new win UI port, we can use SRWLock API without dynamic_link etc.
31 #if __TBB_USE_SRWLOCK
32 
33 static atomic<do_once_state> concmon_module_inited;
34 
35 void WINAPI init_binsem_using_event( SRWLOCK* h_ )
36 {
37  srwl_or_handle* shptr = (srwl_or_handle*) h_;
38  shptr->h = CreateEventEx( NULL, NULL, 0, EVENT_ALL_ACCESS|SEMAPHORE_ALL_ACCESS );
39 }
40 
41 void WINAPI acquire_binsem_using_event( SRWLOCK* h_ )
42 {
43  srwl_or_handle* shptr = (srwl_or_handle*) h_;
44  WaitForSingleObjectEx( shptr->h, INFINITE, FALSE );
45 }
46 
47 void WINAPI release_binsem_using_event( SRWLOCK* h_ )
48 {
49  srwl_or_handle* shptr = (srwl_or_handle*) h_;
50  SetEvent( shptr->h );
51 }
52 
53 static void (WINAPI *__TBB_init_binsem)( SRWLOCK* ) = (void (WINAPI *)(SRWLOCK*))&init_binsem_using_event;
54 static void (WINAPI *__TBB_acquire_binsem)( SRWLOCK* ) = (void (WINAPI *)(SRWLOCK*))&acquire_binsem_using_event;
55 static void (WINAPI *__TBB_release_binsem)( SRWLOCK* ) = (void (WINAPI *)(SRWLOCK*))&release_binsem_using_event;
56 
58 static const dynamic_link_descriptor SRWLLinkTable[] = {
59  DLD(InitializeSRWLock, __TBB_init_binsem),
60  DLD(AcquireSRWLockExclusive, __TBB_acquire_binsem),
61  DLD(ReleaseSRWLockExclusive, __TBB_release_binsem)
62 };
63 
64 inline void init_concmon_module()
65 {
66  __TBB_ASSERT( (uintptr_t)__TBB_init_binsem==(uintptr_t)&init_binsem_using_event, NULL );
67  if( dynamic_link( "Kernel32.dll", SRWLLinkTable, sizeof(SRWLLinkTable)/sizeof(dynamic_link_descriptor) ) ) {
68  __TBB_ASSERT( (uintptr_t)__TBB_init_binsem!=(uintptr_t)&init_binsem_using_event, NULL );
69  __TBB_ASSERT( (uintptr_t)__TBB_acquire_binsem!=(uintptr_t)&acquire_binsem_using_event, NULL );
70  __TBB_ASSERT( (uintptr_t)__TBB_release_binsem!=(uintptr_t)&release_binsem_using_event, NULL );
71  }
72 }
73 
75  atomic_do_once( &init_concmon_module, concmon_module_inited );
76 
77  __TBB_init_binsem( &my_sem.lock );
78  if( (uintptr_t)__TBB_init_binsem!=(uintptr_t)&init_binsem_using_event )
79  P();
80 }
81 
83  if( (uintptr_t)__TBB_init_binsem==(uintptr_t)&init_binsem_using_event )
84  CloseHandle( my_sem.h );
85 }
86 
87 void binary_semaphore::P() { __TBB_acquire_binsem( &my_sem.lock ); }
88 
89 void binary_semaphore::V() { __TBB_release_binsem( &my_sem.lock ); }
90 
91 #endif /* __TBB_USE_SRWLOCK */
92 
93 } // namespace internal
94 } // 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
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

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.