|
| 1 | +/* Copyright (C) 2006-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc. |
| 2 | +
|
| 3 | + This program is free software; you can redistribute it and/or modify |
| 4 | + it under the terms of the GNU General Public License as published by |
| 5 | + the Free Software Foundation; version 2 of the License. |
| 6 | +
|
| 7 | + This program is distributed in the hope that it will be useful, |
| 8 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + GNU General Public License for more details. |
| 11 | +
|
| 12 | + You should have received a copy of the GNU General Public License |
| 13 | + along with this program; if not, write to the Free Software |
| 14 | + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ |
| 15 | + |
| 16 | +#ifndef _atomic_h_cleanup_ |
| 17 | +#define _atomic_h_cleanup_ "atomic/generic-msvc.h" |
| 18 | + |
| 19 | +/* |
| 20 | + We don't implement anything specific for MY_ATOMIC_MODE_DUMMY, always use |
| 21 | + intrinsics. |
| 22 | + 8 and 16-bit atomics are not implemented, but it can be done if necessary. |
| 23 | +*/ |
| 24 | +#undef MY_ATOMIC_HAS_8_16 |
| 25 | + |
| 26 | +/* |
| 27 | + x86 compilers (both VS2003 or VS2005) never use instrinsics, but generate |
| 28 | + function calls to kernel32 instead, even in the optimized build. |
| 29 | + We force intrinsics as described in MSDN documentation for |
| 30 | + _InterlockedCompareExchange. |
| 31 | +*/ |
| 32 | +#ifdef _M_IX86 |
| 33 | + |
| 34 | +#if (_MSC_VER >= 1500) |
| 35 | +#include <intrin.h> |
| 36 | +#else |
| 37 | +C_MODE_START |
| 38 | +/*Visual Studio 2003 and earlier do not have prototypes for atomic intrinsics*/ |
| 39 | +LONG _InterlockedExchange (LONG volatile *Target,LONG Value); |
| 40 | +LONG _InterlockedCompareExchange (LONG volatile *Target, LONG Value, LONG Comp); |
| 41 | +LONG _InterlockedExchangeAdd (LONG volatile *Addend, LONG Value); |
| 42 | +C_MODE_END |
| 43 | + |
| 44 | +#pragma intrinsic(_InterlockedExchangeAdd) |
| 45 | +#pragma intrinsic(_InterlockedCompareExchange) |
| 46 | +#pragma intrinsic(_InterlockedExchange) |
| 47 | +#endif |
| 48 | + |
| 49 | +#define InterlockedExchange _InterlockedExchange |
| 50 | +#define InterlockedExchangeAdd _InterlockedExchangeAdd |
| 51 | +#define InterlockedCompareExchange _InterlockedCompareExchange |
| 52 | +/* |
| 53 | + No need to do something special for InterlockedCompareExchangePointer |
| 54 | + as it is a #define to InterlockedCompareExchange. The same applies to |
| 55 | + InterlockedExchangePointer. |
| 56 | +*/ |
| 57 | +#endif /*_M_IX86*/ |
| 58 | + |
| 59 | +#define MY_ATOMIC_MODE "msvc-intrinsics" |
| 60 | +#define IL_EXCHG_ADD32(X,Y) InterlockedExchangeAdd((volatile LONG *)(X),(Y)) |
| 61 | +#define IL_COMP_EXCHG32(X,Y,Z) InterlockedCompareExchange((volatile LONG *)(X),(Y),(Z)) |
| 62 | +#define IL_COMP_EXCHGptr InterlockedCompareExchangePointer |
| 63 | +#define IL_EXCHG32(X,Y) InterlockedExchange((volatile LONG *)(X),(Y)) |
| 64 | +#define IL_EXCHGptr InterlockedExchangePointer |
| 65 | +#define make_atomic_add_body(S) \ |
| 66 | + v= IL_EXCHG_ADD ## S (a, v) |
| 67 | +#define make_atomic_cas_body(S) \ |
| 68 | + int ## S initial_cmp= *cmp; \ |
| 69 | + int ## S initial_a= IL_COMP_EXCHG ## S (a, set, initial_cmp); \ |
| 70 | + if (!(ret= (initial_a == initial_cmp))) *cmp= initial_a; |
| 71 | +#define make_atomic_swap_body(S) \ |
| 72 | + v= IL_EXCHG ## S (a, v) |
| 73 | +#define make_atomic_load_body(S) \ |
| 74 | + ret= 0; /* avoid compiler warning */ \ |
| 75 | + ret= IL_COMP_EXCHG ## S (a, ret, ret); |
| 76 | + |
| 77 | +/* |
| 78 | + my_yield_processor (equivalent of x86 PAUSE instruction) should be used |
| 79 | + to improve performance on hyperthreaded CPUs. Intel recommends to use it in |
| 80 | + spin loops also on non-HT machines to reduce power consumption (see e.g |
| 81 | + http://softwarecommunity.intel.com/articles/eng/2004.htm) |
| 82 | +
|
| 83 | + Running benchmarks for spinlocks implemented with InterlockedCompareExchange |
| 84 | + and YieldProcessor shows that much better performance is achieved by calling |
| 85 | + YieldProcessor in a loop - that is, yielding longer. On Intel boxes setting |
| 86 | + loop count in the range 200-300 brought best results. |
| 87 | + */ |
| 88 | +#ifndef YIELD_LOOPS |
| 89 | +#define YIELD_LOOPS 200 |
| 90 | +#endif |
| 91 | + |
| 92 | +static __inline int my_yield_processor() |
| 93 | +{ |
| 94 | + int i; |
| 95 | + for(i=0; i<YIELD_LOOPS; i++) |
| 96 | + { |
| 97 | +#if (_MSC_VER <= 1310) |
| 98 | + /* On older compilers YieldProcessor is not available, use inline assembly*/ |
| 99 | + __asm { rep nop } |
| 100 | +#else |
| 101 | + YieldProcessor(); |
| 102 | +#endif |
| 103 | + } |
| 104 | + return 1; |
| 105 | +} |
| 106 | + |
| 107 | +#define LF_BACKOFF my_yield_processor() |
| 108 | +#else /* cleanup */ |
| 109 | + |
| 110 | +#undef IL_EXCHG_ADD32 |
| 111 | +#undef IL_COMP_EXCHG32 |
| 112 | +#undef IL_COMP_EXCHGptr |
| 113 | +#undef IL_EXCHG32 |
| 114 | +#undef IL_EXCHGptr |
| 115 | + |
| 116 | +#endif |
0 commit comments