From 9d4e81294c0486ff21f16e473addda2a306e1aad Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Mon, 18 Jul 2016 09:48:58 -0700 Subject: [PATCH] Add an Atomic portability header Summary: Because there are situations where we need to do an atomic write to a plain pointer, rather than an atomic value. Unfortunately, there is no support in the standard library for this, so different compilers implement the primitives differently. This specifically implements an `int64_t` overload for `__sync_fetch_and_add` for use in the atomic hash map test. Reviewed By: yfeldblum Differential Revision: D3571830 fbshipit-source-id: c27d8d2a5238bbc9aba6a9e48e4b3412a199288f --- folly/Makefile.am | 1 + folly/portability/Atomic.h | 31 +++++++++++++++++++++++++++++++ folly/test/AtomicHashMapTest.cpp | 1 + 3 files changed, 33 insertions(+) create mode 100755 folly/portability/Atomic.h diff --git a/folly/Makefile.am b/folly/Makefile.am index a987eb74..e71ec9a0 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -252,6 +252,7 @@ nobase_follyinclude_HEADERS = \ PicoSpinLock.h \ Portability.h \ portability/Asm.h \ + portability/Atomic.h \ portability/Builtins.h \ portability/Config.h \ portability/Constexpr.h \ diff --git a/folly/portability/Atomic.h b/folly/portability/Atomic.h new file mode 100755 index 00000000..811c24cc --- /dev/null +++ b/folly/portability/Atomic.h @@ -0,0 +1,31 @@ +/* + * Copyright 2016 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#ifdef _WIN32 + +#include + +#include +// The intrinsics we need are in Windows.h :( +#include + +FOLLY_ALWAYS_INLINE int64_t __sync_fetch_and_add(int64_t* ptr, int64_t value) { + return InterlockedExchangeAdd64(ptr, value); +} + +#endif diff --git a/folly/test/AtomicHashMapTest.cpp b/folly/test/AtomicHashMapTest.cpp index 08162273..f884b4ed 100644 --- a/folly/test/AtomicHashMapTest.cpp +++ b/folly/test/AtomicHashMapTest.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include using std::vector; -- 2.34.1