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