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