From: Christopher Dykes Date: Tue, 26 Jul 2016 23:17:39 +0000 (-0700) Subject: Support PicoSpinLock on MSVC X-Git-Tag: v2016.07.29.00~8 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=369761f0999b218383f468bfeebdf6b1a07d79ca;p=folly.git 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 --- 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)" \ : \