Intel(R) Threading Building Blocks Doxygen Documentation version 4.2.3
linux_common.h
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#ifndef __TBB_machine_H
18#error Do not #include this internal file directly; use public TBB headers instead.
19#endif
20
21#include <sched.h>
22#define __TBB_Yield() sched_yield()
23
24#include <unistd.h>
25/* Futex definitions */
26#include <sys/syscall.h>
27
28#if defined(SYS_futex)
29/* This header file is included for Linux and some other systems that may support futexes.*/
30
31#define __TBB_USE_FUTEX 1
32
33#if defined(__has_include)
34#define __TBB_has_include __has_include
35#else
36#define __TBB_has_include(x) 0
37#endif
38
39/*
40If available, use typical headers where futex API is defined. While Linux and OpenBSD
41are known to provide such headers, other systems might have them as well.
42*/
43#if defined(__linux__) || __TBB_has_include(<linux/futex.h>)
44#include <linux/futex.h>
45#elif defined(__OpenBSD__) || __TBB_has_include(<sys/futex.h>)
46#include <sys/futex.h>
47#endif
48
49#include <limits.h>
50#include <errno.h>
51
52/*
53Some systems might not define the macros or use different names. In such case we expect
54the actual parameter values to match Linux: 0 for wait, 1 for wake.
55*/
56#if defined(FUTEX_WAIT_PRIVATE)
57#define __TBB_FUTEX_WAIT FUTEX_WAIT_PRIVATE
58#elif defined(FUTEX_WAIT)
59#define __TBB_FUTEX_WAIT FUTEX_WAIT
60#else
61#define __TBB_FUTEX_WAIT 0
62#endif
63
64#if defined(FUTEX_WAKE_PRIVATE)
65#define __TBB_FUTEX_WAKE FUTEX_WAKE_PRIVATE
66#elif defined(FUTEX_WAKE)
67#define __TBB_FUTEX_WAKE FUTEX_WAKE
68#else
69#define __TBB_FUTEX_WAKE 1
70#endif
71
72#ifndef __TBB_ASSERT
73#error machine specific headers must be included after tbb_stddef.h
74#endif
75
76namespace tbb {
77
78namespace internal {
79
80inline int futex_wait( void *futex, int comparand ) {
81 int r = syscall( SYS_futex,futex,__TBB_FUTEX_WAIT,comparand,NULL,NULL,0 );
82#if TBB_USE_ASSERT
83 int e = errno;
84 __TBB_ASSERT( r==0||r==EWOULDBLOCK||(r==-1&&(e==EAGAIN||e==EINTR)), "futex_wait failed." );
85#endif /* TBB_USE_ASSERT */
86 return r;
87}
88
89inline int futex_wakeup_one( void *futex ) {
90 int r = ::syscall( SYS_futex,futex,__TBB_FUTEX_WAKE,1,NULL,NULL,0 );
91 __TBB_ASSERT( r==0||r==1, "futex_wakeup_one: more than one thread woken up?" );
92 return r;
93}
94
95inline int futex_wakeup_all( void *futex ) {
96 int r = ::syscall( SYS_futex,futex,__TBB_FUTEX_WAKE,INT_MAX,NULL,NULL,0 );
97 __TBB_ASSERT( r>=0, "futex_wakeup_all: error in waking up threads" );
98 return r;
99}
100
101} /* namespace internal */
102
103} /* namespace tbb */
104
105#endif /* SYS_futex */
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
The graph class.

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.