BRE12
linux_common.h
1 /*
2  Copyright 2005-2016 Intel Corporation. All Rights Reserved.
3 
4  This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5  you can redistribute it and/or modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation. Threading Building Blocks is
7  distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9  See the GNU General Public License for more details. You should have received a copy of
10  the GNU General Public License along with Threading Building Blocks; if not, write to the
11  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12 
13  As a special exception, you may use this file as part of a free software library without
14  restriction. Specifically, if other files instantiate templates or use macros or inline
15  functions from this file, or you compile this file and link it with other files to produce
16  an executable, this file does not by itself cause the resulting executable to be covered
17  by the GNU General Public License. This exception does not however invalidate any other
18  reasons why the executable file might be covered by the GNU General Public License.
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 
34 #define __TBB_USE_FUTEX 1
35 #include <limits.h>
36 #include <errno.h>
37 // Unfortunately, some versions of Linux do not have a header that defines FUTEX_WAIT and FUTEX_WAKE.
38 
39 #ifdef FUTEX_WAIT
40 #define __TBB_FUTEX_WAIT FUTEX_WAIT
41 #else
42 #define __TBB_FUTEX_WAIT 0
43 #endif
44 
45 #ifdef FUTEX_WAKE
46 #define __TBB_FUTEX_WAKE FUTEX_WAKE
47 #else
48 #define __TBB_FUTEX_WAKE 1
49 #endif
50 
51 #ifndef __TBB_ASSERT
52 #error machine specific headers must be included after tbb_stddef.h
53 #endif
54 
55 namespace tbb {
56 
57 namespace internal {
58 
59 inline int futex_wait( void *futex, int comparand ) {
60  int r = syscall( SYS_futex,futex,__TBB_FUTEX_WAIT,comparand,NULL,NULL,0 );
61 #if TBB_USE_ASSERT
62  int e = errno;
63  __TBB_ASSERT( r==0||r==EWOULDBLOCK||(r==-1&&(e==EAGAIN||e==EINTR)), "futex_wait failed." );
64 #endif /* TBB_USE_ASSERT */
65  return r;
66 }
67 
68 inline int futex_wakeup_one( void *futex ) {
69  int r = ::syscall( SYS_futex,futex,__TBB_FUTEX_WAKE,1,NULL,NULL,0 );
70  __TBB_ASSERT( r==0||r==1, "futex_wakeup_one: more than one thread woken up?" );
71  return r;
72 }
73 
74 inline int futex_wakeup_all( void *futex ) {
75  int r = ::syscall( SYS_futex,futex,__TBB_FUTEX_WAKE,INT_MAX,NULL,NULL,0 );
76  __TBB_ASSERT( r>=0, "futex_wakeup_all: error in waking up threads" );
77  return r;
78 }
79 
80 } /* namespace internal */
81 
82 } /* namespace tbb */
83 
84 #endif /* SYS_futex */
Definition: _flow_graph_async_msg_impl.h:32
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44