1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/********************************************************************************
* *
* S p i n l o c k C l a s s *
* *
*********************************************************************************
* Copyright (C) 2004,2022 by Jeroen van der Zijp. All Rights Reserved. *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/> *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "FXSpinLock.h"
/*
Notes:
- Spin lock variable.
- Extra fluff data is not useless; it is intentionally there to pad the
spin-variable to a cache line, thus reducing false-sharing in case other
"hot" data is nearby.
- Ticket spinlock flavor does not support more than 256 simultaneous threads.
*/
using namespace FX;
/*******************************************************************************/
namespace FX {
// Initialize spinlock
FXSpinLock::FXSpinLock(){
#if defined(WIN32)
data[0]=data[1]=data[2]=data[3]=0;
#elif (defined(__GNUC__) || defined(__INTEL_COMPILER)) && (defined(__i386__) || defined(__x86_64__))
data[0]=data[1]=data[2]=data[3]=0;
#elif defined(__APPLE__)
// If this fails on your machine, determine what value
// of sizeof(pthread_mutex_t) is supposed to be on your
// machine and mail it to: jeroen@fox-toolkit.net!!
//FXTRACE((150,"sizeof(OSSpinLock)=%d\n",sizeof(OSSpinLock)));
FXASSERT_STATIC(sizeof(data)>=sizeof(OSSpinLock));
data[0]=data[1]=data[2]=data[3]=0;
*((OSSpinLock*)data)=OS_SPINLOCK_INIT;
#else
// If this fails on your machine, determine what value
// of sizeof(pthread_spinlock_t) is supposed to be on your
// machine and mail it to: jeroen@fox-toolkit.net!!
//FXTRACE((150,"sizeof(pthread_spinlock_t)=%d\n",sizeof(pthread_spinlock_t)));
FXASSERT_STATIC(sizeof(data)>=sizeof(pthread_spinlock_t));
data[0]=data[1]=data[2]=data[3]=0;
pthread_spin_init((pthread_spinlock_t*)(void*)data,PTHREAD_PROCESS_PRIVATE);
#endif
}
// Lock the spinlock
void FXSpinLock::lock(){
#if defined(WIN32)
while(_InterlockedExchange((LONG*)data,1L)){
YieldProcessor();
}
#elif (defined(__GNUC__) || defined(__INTEL_COMPILER)) && (defined(__i386__) || defined(__x86_64__))
__asm__ __volatile__ ("movw $0x0100, %%ax \n\t"
"lock\n\t"
"xaddw %%ax, %0 \n\t"
"1: \n\t"
"cmpb %%ah, %%al\n\t"
"je 2f\n\t"
"rep; nop\n\t"
"movb %0, %%al\n\t"
"jmp 1b\n\t"
"2:" : "+m" (data) : : "memory", "ax", "cc");
#elif defined(__APPLE__)
OSSpinLockLock((OSSpinLock*)data);
#else
pthread_spin_lock((pthread_spinlock_t*)(void*)data);
#endif
}
// Try lock the spinlock
FXbool FXSpinLock::trylock(){
#if defined(WIN32)
return !_InterlockedExchange((LONG*)data,1L);
#elif (defined(__GNUC__) || defined(__INTEL_COMPILER)) && (defined(__i386__) || defined(__x86_64__))
FXbool ret;
__asm__ __volatile__ ("movw %1,%%ax\n\t"
"cmpb %%ah, %%al\n\t"
"jne 1f\n\t"
"movw %%ax,%%cx\n\t"
"addw $0x0100,%%cx\n\t"
"lock\n\t"
"cmpxchgw %%cx,%1\n\t"
"1:\n\t"
"sete %b0\n\t" :"=a" (ret), "+m" (data) : : "ecx", "memory", "cc");
return ret;
#elif defined(__APPLE__)
return OSSpinLockTry((OSSpinLock*)data);
#else
return pthread_spin_trylock((pthread_spinlock_t*)(void*)data)==0;
#endif
}
// Unlock spinlock
void FXSpinLock::unlock(){
#if defined(WIN32)
_InterlockedExchange((LONG*)data,0L);
#elif (defined(__GNUC__) || defined(__INTEL_COMPILER)) && (defined(__i386__) || defined(__x86_64__))
__asm__ __volatile__ ("lock\n\t"
"incb %0\n\t" : "+m" (data) : : "memory", "cc");
#elif defined(__APPLE__)
OSSpinLockUnlock((OSSpinLock*)data);
#else
pthread_spin_unlock((pthread_spinlock_t*)(void*)data);
#endif
}
// Test if locked
FXbool FXSpinLock::locked(){
#if defined(WIN32)
return (data[0]!=0);
#elif (defined(__GNUC__) || defined(__INTEL_COMPILER)) && (defined(__i386__) || defined(__x86_64__))
FXbool ret;
__asm__ __volatile__ ("movw %1,%%ax\n\t"
"cmpb %%ah, %%al\n\t"
"setne %%al\n\t" :"=a" (ret), "+m" (data) : : "memory", "cc");
return ret;
#elif defined(__APPLE__)
if(OSSpinLockTry((OSSpinLock*)data)){
OSSpinLockUnlock((OSSpinLock*)data);
return false;
}
return true;
#else
if(pthread_spin_trylock((pthread_spinlock_t*)(void*)data)==0){
pthread_spin_unlock((pthread_spinlock_t*)(void*)data);
return false;
}
return true;
#endif
}
// Delete spinlock
FXSpinLock::~FXSpinLock(){
#if defined(WIN32)
// NOP //
#elif (defined(__GNUC__) || defined(__INTEL_COMPILER)) && (defined(__i386__) || defined(__x86_64__))
// NOP //
#elif defined(__APPLE__)
// NOP //
#else
pthread_spin_destroy((pthread_spinlock_t*)(void*)data);
#endif
}
}
|