From 369761f0999b218383f468bfeebdf6b1a07d79ca Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Tue, 26 Jul 2016 16:17:39 -0700 Subject: [PATCH] Support PicoSpinLock on MSVC Summary: It was using inline assembly in order to get atomic single-bit operations, so add a variant for MSVC that uses intrinsics. MSVC is also weird in-that it doesn't have a 16-bit variant of these, so use an atomic OR and AND to achieve the required effect. Reviewed By: yfeldblum Differential Revision: D3623220 fbshipit-source-id: b4ff985ef2ed7787115f4d20de6f244123410dc8 --- folly/PicoSpinLock.h | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/folly/PicoSpinLock.h b/folly/PicoSpinLock.h index 32c5abfb..9f52a7a9 100644 --- a/folly/PicoSpinLock.h +++ b/folly/PicoSpinLock.h @@ -128,7 +128,20 @@ struct PicoSpinLock { bool try_lock() const { bool ret = false; -#if FOLLY_X64 +#ifdef _MSC_VER + switch (sizeof(IntType)) { + case 2: + // There is no _interlockedbittestandset16 for some reason :( + ret = _InterlockedOr16((volatile short*)&lock, 1 << Bit) & (1 << Bit); + break; + case 4: + ret = _interlockedbittestandset((volatile long*)&lock_, Bit); + break; + case 8: + ret = _interlockedbittestandset64((volatile long long*)&lock_, Bit); + break; + } +#elif FOLLY_X64 #define FB_DOBTS(size) \ asm volatile("lock; bts" #size " %1, (%2); setnc %0" \ : "=r" (ret) \ @@ -193,7 +206,20 @@ struct PicoSpinLock { * integer. */ void unlock() const { -#if FOLLY_X64 +#ifdef _MSC_VER + switch (sizeof(IntType)) { + case 2: + // There is no _interlockedbittestandreset16 for some reason :( + _InterlockedAnd16((volatile short*)&lock, ~(1 << Bit)); + break; + case 4: + _interlockedbittestandreset((volatile long*)&lock_, Bit); + break; + case 8: + _interlockedbittestandreset64((volatile long long*)&lock_, Bit); + break; + } +#elif FOLLY_X64 #define FB_DOBTR(size) \ asm volatile("lock; btr" #size " %0, (%1)" \ : \ -- 2.34.1