| 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 | #if !defined(__TBB_machine_H) || defined(__TBB_machine_gcc_itsx_H) |
| 30 | #error Do not #include this internal file directly; use public TBB headers instead. |
| 31 | #endif |
| 32 | |
| 33 | #define __TBB_machine_gcc_itsx_H |
| 34 | |
| 35 | #define __TBB_OP_XACQUIRE 0xF2 |
| 36 | #define __TBB_OP_XRELEASE 0xF3 |
| 37 | #define __TBB_OP_LOCK 0xF0 |
| 38 | |
| 39 | #define __TBB_STRINGIZE_INTERNAL(arg) #arg |
| 40 | #define __TBB_STRINGIZE(arg) __TBB_STRINGIZE_INTERNAL(arg) |
| 41 | |
| 42 | #ifdef __TBB_x86_64 |
| 43 | #define __TBB_r_out "=r" |
| 44 | #else |
| 45 | #define __TBB_r_out "=q" |
| 46 | #endif |
| 47 | |
| 48 | inline static uint8_t __TBB_machine_try_lock_elided( volatile uint8_t* lk ) |
| 49 | { |
| 50 | uint8_t value = 1; |
| 51 | __asm__ volatile (".byte " __TBB_STRINGIZE(__TBB_OP_XACQUIRE)"; lock; xchgb %0, %1;" |
| 52 | : __TBB_r_out(value), "=m" (*lk) : "0" (value), "m" (*lk) : "memory" ); |
| 53 | return uint8_t(value^1); |
| 54 | } |
| 55 | |
| 56 | inline static void __TBB_machine_try_lock_elided_cancel() |
| 57 | { |
| 58 | // 'pause' instruction aborts HLE/RTM transactions |
| 59 | __asm__ volatile ("pause\n" : : : "memory" ); |
| 60 | } |
| 61 | |
| 62 | inline static void __TBB_machine_unlock_elided( volatile uint8_t* lk ) |
| 63 | { |
| 64 | __asm__ volatile (".byte " __TBB_STRINGIZE(__TBB_OP_XRELEASE)"; movb $0, %0" |
| 65 | : "=m" (*lk) : "m" (*lk) : "memory" ); |
| 66 | } |
| 67 | |