semaphore.h
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.
*/
#ifndef __TBB_tbb_semaphore_H
#define __TBB_tbb_semaphore_H
#include <tbb/atomic.h>
#include "tbb/tbb_stddef.h"
#if _WIN32||_WIN64
#include "tbb/machine/windows_api.h"
#elif __APPLE__
#include <mach/semaphore.h>
#include <mach/task.h>
#include <mach/mach_init.h>
#include <mach/error.h>
#else
#include <semaphore.h>
#ifdef TBB_USE_DEBUG
#include <errno.h>
#endif
#endif /*_WIN32||_WIN64*/
namespace tbb {
namespace internal {
#if _WIN32||_WIN64
typedef LONG sem_count_t;
class semaphore : no_copy {
static const int max_semaphore_cnt = MAXLONG;
public:
semaphore(size_t start_cnt_ = 0) {init_semaphore(start_cnt_);}
~semaphore() {CloseHandle( sem );}
void P() {WaitForSingleObjectEx( sem, INFINITE, FALSE );}
void V() {ReleaseSemaphore( sem, 1, NULL );}
private:
HANDLE sem;
void init_semaphore(size_t start_cnt_) {
sem = CreateSemaphoreEx( NULL, LONG(start_cnt_), max_semaphore_cnt, NULL, 0, SEMAPHORE_ALL_ACCESS );
}
};
#elif __APPLE__
class semaphore : no_copy {
public:
semaphore(int start_cnt_ = 0) : sem(start_cnt_) { init_semaphore(start_cnt_); }
~semaphore() {
kern_return_t ret = semaphore_destroy( mach_task_self(), sem );
__TBB_ASSERT_EX( ret==err_none, NULL );
}
void P() {
int ret;
do {
ret = semaphore_wait( sem );
} while( ret==KERN_ABORTED );
__TBB_ASSERT( ret==KERN_SUCCESS, "semaphore_wait() failed" );
}
void V() { semaphore_signal( sem ); }
private:
semaphore_t sem;
void init_semaphore(int start_cnt_) {
kern_return_t ret = semaphore_create( mach_task_self(), &sem, SYNC_POLICY_FIFO, start_cnt_ );
__TBB_ASSERT_EX( ret==err_none, "failed to create a semaphore" );
}
};
#else /* Linux/Unix */
typedef uint32_t sem_count_t;
class semaphore : no_copy {
public:
semaphore(int start_cnt_ = 0 ) { init_semaphore( start_cnt_ ); }
~semaphore() {
int ret = sem_destroy( &sem );
__TBB_ASSERT_EX( !ret, NULL );
}
void P() {
while( sem_wait( &sem )!=0 )
__TBB_ASSERT( errno==EINTR, NULL );
}
void V() { sem_post( &sem ); }
private:
sem_t sem;
void init_semaphore(int start_cnt_) {
int ret = sem_init( &sem, /*shared among threads*/ 0, start_cnt_ );
__TBB_ASSERT_EX( !ret, NULL );
}
};
#endif /* _WIN32||_WIN64 */
#if _WIN32||_WIN64
#if !__TBB_USE_SRWLOCK
class binary_semaphore : no_copy {
public:
binary_semaphore() { my_sem = CreateEventEx( NULL, NULL, 0, EVENT_ALL_ACCESS ); }
~binary_semaphore() { CloseHandle( my_sem ); }
void P() { WaitForSingleObjectEx( my_sem, INFINITE, FALSE ); }
void V() { SetEvent( my_sem ); }
private:
HANDLE my_sem;
};
#else /* __TBB_USE_SRWLOCK */
union srwl_or_handle {
SRWLOCK lock;
HANDLE h;
};
class binary_semaphore : no_copy {
public:
binary_semaphore();
~binary_semaphore();
void P();
void V();
private:
srwl_or_handle my_sem;
};
#endif /* !__TBB_USE_SRWLOCK */
#elif __APPLE__
class binary_semaphore : no_copy {
public:
binary_semaphore() : my_sem(0) {
kern_return_t ret = semaphore_create( mach_task_self(), &my_sem, SYNC_POLICY_FIFO, 0 );
__TBB_ASSERT_EX( ret==err_none, "failed to create a semaphore" );
}
~binary_semaphore() {
kern_return_t ret = semaphore_destroy( mach_task_self(), my_sem );
__TBB_ASSERT_EX( ret==err_none, NULL );
}
void P() {
int ret;
do {
ret = semaphore_wait( my_sem );
} while( ret==KERN_ABORTED );
__TBB_ASSERT( ret==KERN_SUCCESS, "semaphore_wait() failed" );
}
void V() { semaphore_signal( my_sem ); }
private:
semaphore_t my_sem;
};
#else /* Linux/Unix */
#if __TBB_USE_FUTEX
class binary_semaphore : no_copy {
// The implementation is equivalent to the "Mutex, Take 3" one
// in the paper "Futexes Are Tricky" by Ulrich Drepper
public:
binary_semaphore() { my_sem = 1; }
~binary_semaphore() {}
void P() {
int s;
if( (s = my_sem.compare_and_swap( 1, 0 ))!=0 ) {
if( s!=2 )
s = my_sem.fetch_and_store( 2 );
while( s!=0 ) { // This loop deals with spurious wakeup
futex_wait( &my_sem, 2 );
s = my_sem.fetch_and_store( 2 );
}
}
}
void V() {
__TBB_ASSERT( my_sem>=1, "multiple V()'s in a row?" );
if( my_sem.fetch_and_store( 0 )==2 )
futex_wakeup_one( &my_sem );
}
private:
atomic<int> my_sem; // 0 - open; 1 - closed, no waits; 2 - closed, possible waits
};
#else
typedef uint32_t sem_count_t;
class binary_semaphore : no_copy {
public:
binary_semaphore() {
int ret = sem_init( &my_sem, /*shared among threads*/ 0, 0 );
__TBB_ASSERT_EX( !ret, NULL );
}
~binary_semaphore() {
int ret = sem_destroy( &my_sem );
__TBB_ASSERT_EX( !ret, NULL );
}
void P() {
while( sem_wait( &my_sem )!=0 )
__TBB_ASSERT( errno==EINTR, NULL );
}
void V() { sem_post( &my_sem ); }
private:
sem_t my_sem;
};
#endif /* __TBB_USE_FUTEX */
#endif /* _WIN32||_WIN64 */
} // namespace internal
} // namespace tbb
#endif /* __TBB_tbb_semaphore_H */
internal
Definition: _flow_graph_async_msg_impl.h:24
tbb::internal::binary_semaphore
binary_semaphore for concurrent monitor
Definition: semaphore.h:222
__TBB_ASSERT
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
tbb::internal::no_copy
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330
tbb::internal::binary_semaphore::my_sem
sem_t my_sem
Definition: semaphore.h:242
tbb::internal::semaphore::init_semaphore
void init_semaphore(int start_cnt_)
Definition: semaphore.h:113
tbb::internal::semaphore::sem
sem_t sem
Definition: semaphore.h:112
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
tbb::internal::semaphore::P
void P()
wait/acquire
Definition: semaphore.h:105
windows_api.h
semaphore.h
tbb::internal::sem_count_t
uint32_t sem_count_t
for performance reasons, we want specialized binary_semaphore
Definition: semaphore.h:92
lock
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 * lock
Definition: ittnotify_static.h:121
atomic.h
tbb::internal::semaphore::semaphore
semaphore(int start_cnt_=0)
ctor
Definition: semaphore.h:97
tbb::internal::semaphore
Edsger Dijkstra's counting semaphore.
Definition: semaphore.h:94
s
void const char const char int ITT_FORMAT __itt_group_sync s
Definition: ittnotify_static.h:91
tbb::internal::semaphore::~semaphore
~semaphore()
dtor
Definition: semaphore.h:100
tbb_stddef.h
tbb::internal::binary_semaphore::~binary_semaphore
~binary_semaphore()
dtor
Definition: semaphore.h:230
tbb::internal::binary_semaphore::P
void P()
wait/acquire
Definition: semaphore.h:235
tbb::internal::semaphore::V
void V()
post/release
Definition: semaphore.h:110
tbb::internal::binary_semaphore::binary_semaphore
binary_semaphore()
ctor
Definition: semaphore.h:225
__TBB_ASSERT_EX
#define __TBB_ASSERT_EX(predicate, comment)
"Extended" version is useful to suppress warnings if a variable is only used with an assert
Definition: tbb_stddef.h:167
h
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 ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function h
Definition: ittnotify_static.h:159
tbb::internal::binary_semaphore::V
void V()
post/release
Definition: semaphore.h:240

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.