Intel(R) Threading Building Blocks Doxygen Documentation version 4.2.3
Loading...
Searching...
No Matches
semaphore.cpp
Go to the documentation of this file.
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
23namespace tbb {
24namespace internal {
25
26// TODO: For new win UI port, we can use SRWLock API without dynamic_link etc.
27#if __TBB_USE_SRWLOCK
28
29static atomic<do_once_state> concmon_module_inited;
30
31void WINAPI init_binsem_using_event( SRWLOCK* h_ )
32{
33 srwl_or_handle* shptr = (srwl_or_handle*) h_;
34 shptr->h = CreateEventEx( NULL, NULL, 0, EVENT_ALL_ACCESS|SEMAPHORE_ALL_ACCESS );
35}
36
37void WINAPI acquire_binsem_using_event( SRWLOCK* h_ )
38{
39 srwl_or_handle* shptr = (srwl_or_handle*) h_;
40 WaitForSingleObjectEx( shptr->h, INFINITE, FALSE );
41}
42
43void WINAPI release_binsem_using_event( SRWLOCK* h_ )
44{
45 srwl_or_handle* shptr = (srwl_or_handle*) h_;
46 SetEvent( shptr->h );
47}
48
49static void (WINAPI *__TBB_init_binsem)( SRWLOCK* ) = (void (WINAPI *)(SRWLOCK*))&init_binsem_using_event;
50static void (WINAPI *__TBB_acquire_binsem)( SRWLOCK* ) = (void (WINAPI *)(SRWLOCK*))&acquire_binsem_using_event;
51static void (WINAPI *__TBB_release_binsem)( SRWLOCK* ) = (void (WINAPI *)(SRWLOCK*))&release_binsem_using_event;
52
54static const dynamic_link_descriptor SRWLLinkTable[] = {
55 DLD(InitializeSRWLock, __TBB_init_binsem),
56 DLD(AcquireSRWLockExclusive, __TBB_acquire_binsem),
57 DLD(ReleaseSRWLockExclusive, __TBB_release_binsem)
58};
59
60inline void init_concmon_module()
61{
62 __TBB_ASSERT( (uintptr_t)__TBB_init_binsem==(uintptr_t)&init_binsem_using_event, NULL );
63 if( dynamic_link( "Kernel32.dll", SRWLLinkTable, sizeof(SRWLLinkTable)/sizeof(dynamic_link_descriptor) ) ) {
64 __TBB_ASSERT( (uintptr_t)__TBB_init_binsem!=(uintptr_t)&init_binsem_using_event, NULL );
65 __TBB_ASSERT( (uintptr_t)__TBB_acquire_binsem!=(uintptr_t)&acquire_binsem_using_event, NULL );
66 __TBB_ASSERT( (uintptr_t)__TBB_release_binsem!=(uintptr_t)&release_binsem_using_event, NULL );
67 }
68}
69
71 atomic_do_once( &init_concmon_module, concmon_module_inited );
72
73 __TBB_init_binsem( &my_sem.lock );
74 if( (uintptr_t)__TBB_init_binsem!=(uintptr_t)&init_binsem_using_event )
75 P();
76}
77
79 if( (uintptr_t)__TBB_init_binsem==(uintptr_t)&init_binsem_using_event )
80 CloseHandle( my_sem.h );
81}
82
83void binary_semaphore::P() { __TBB_acquire_binsem( &my_sem.lock ); }
84
85void binary_semaphore::V() { __TBB_release_binsem( &my_sem.lock ); }
86
87#endif /* __TBB_USE_SRWLOCK */
88
89} // namespace internal
90} // namespace tbb
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
#define DLD(s, h)
The helper to construct dynamic_link_descriptor structure.
Definition: dynamic_link.h:56
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:213
Association between a handler name and location of pointer to it.
Definition: dynamic_link.h:60

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.