Add an Atomic portability header
authorChristopher Dykes <cdykes@fb.com>
Mon, 18 Jul 2016 16:48:58 +0000 (09:48 -0700)
committerFacebook Github Bot 3 <facebook-github-bot-3-bot@fb.com>
Mon, 18 Jul 2016 16:53:32 +0000 (09:53 -0700)
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
folly/portability/Atomic.h [new file with mode: 0755]
folly/test/AtomicHashMapTest.cpp

index a987eb744c668653ffd9bef24149dce49ac715c0..e71ec9a01012fa0a138bf42ca0e13486cc205752 100644 (file)
@@ -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 (executable)
index 0000000..811c24c
--- /dev/null
@@ -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 <stdint.h>
+
+#include <folly/Portability.h>
+// The intrinsics we need are in Windows.h :(
+#include <folly/portability/Windows.h>
+
+FOLLY_ALWAYS_INLINE int64_t __sync_fetch_and_add(int64_t* ptr, int64_t value) {
+  return InterlockedExchangeAdd64(ptr, value);
+}
+
+#endif
index 08162273a3ffa850555dc2c08b14e13d59885623..f884b4ed30542bbc97aa3eaf2504e3e48661a39f 100644 (file)
@@ -23,6 +23,7 @@
 #include <memory>
 #include <folly/Benchmark.h>
 #include <folly/Conv.h>
+#include <folly/portability/Atomic.h>
 #include <folly/portability/SysTime.h>
 
 using std::vector;