Fix data race in Futex<EmulatedFutexAtomic>
[folly.git] / folly / test / AtomicLinkedListTest.cpp
index 0abe82c6cb93bbf2fee0a6fac56b7fdd109781f1..303261ff06e92cf023d5a5b2f8e57fc7db8e1bf1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,9 +17,8 @@
 #include <algorithm>
 #include <thread>
 
-#include <gtest/gtest.h>
-
 #include <folly/AtomicLinkedList.h>
+#include <folly/portability/GTest.h>
 
 class TestIntrusiveObject {
  public:
@@ -75,6 +74,23 @@ TEST(AtomicIntrusiveLinkedList, Basic) {
   TestIntrusiveObject::List movedList = std::move(list);
 }
 
+TEST(AtomicIntrusiveLinkedList, ReverseSweep) {
+  TestIntrusiveObject a(1), b(2), c(3);
+  TestIntrusiveObject::List list;
+  list.insertHead(&a);
+  list.insertHead(&b);
+  list.insertHead(&c);
+  size_t next_expected_id = 3;
+  list.reverseSweep([&](TestIntrusiveObject* obj) {
+    EXPECT_EQ(next_expected_id--, obj->id());
+  });
+  EXPECT_TRUE(list.empty());
+  // Test that we can still insert
+  list.insertHead(&a);
+  EXPECT_FALSE(list.empty());
+  list.reverseSweep([](TestIntrusiveObject*) {});
+}
+
 TEST(AtomicIntrusiveLinkedList, Move) {
   TestIntrusiveObject a(1), b(2);
 
@@ -153,7 +169,8 @@ TEST(AtomicIntrusiveLinkedList, Stress) {
 
 class TestObject {
  public:
-  TestObject(size_t id__, std::shared_ptr<void> ptr) : id_(id__), ptr_(ptr) {}
+  TestObject(size_t id__, const std::shared_ptr<void>& ptr)
+      : id_(id__), ptr_(ptr) {}
 
   size_t id() {
     return id_;