Add folly::assume_unreachable
authorChristopher Dykes <cdykes@fb.com>
Tue, 26 Jul 2016 22:50:47 +0000 (15:50 -0700)
committerFacebook Github Bot 5 <facebook-github-bot-5-bot@fb.com>
Tue, 26 Jul 2016 22:53:45 +0000 (15:53 -0700)
Summary: So that the `[[noreturn]]` attribute can be applied.

Reviewed By: yfeldblum

Differential Revision: D3614122

fbshipit-source-id: 4b95cb553e85c85c277c00b8165671dcc75afac8

folly/Assume.h
folly/test/AtomicHashMapTest.cpp

index 8f8074333c2db293b4227372fb8976a0037b9130..c4d8fabb609ca36b297595411d86d330e502cca1 100644 (file)
@@ -16,6 +16,8 @@
 
 #pragma once
 
+#include <cstdlib>
+
 #include <folly/Portability.h>
 #include <glog/logging.h>
 
@@ -45,4 +47,18 @@ FOLLY_ALWAYS_INLINE void assume(bool cond) {
 #endif
 }
 
+[[noreturn]] FOLLY_ALWAYS_INLINE void assume_unreachable() {
+  assume(false);
+  // Do a bit more to get the compiler to understand
+  // that this function really will never return.
+#if defined(__GNUC__)
+  __builtin_unreachable();
+#elif defined(_MSC_VER)
+  __assume(0);
+#else
+  // Well, it's better than nothing.
+  std::abort();
+#endif
+}
+
 }  // namespace folly
index e62e8386eeb605c5139f675135171ec5702c53b6..28f576789082598d21acca2df75ec351dc5c4898 100644 (file)
@@ -681,7 +681,7 @@ void* atomicHashArrayInsertRaceThread(void* /* j */) {
     }
   }
   pthread_exit((void *) numInserted);
-  folly::assume(false);
+  folly::assume_unreachable();
 }
 TEST(Ahm, atomic_hash_array_insert_race) {
   AHA* arr = atomicHashArrayInsertRaceArray.get();