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

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.