Improve SingletonThreadLocal performance
[folly.git] / folly / Synchronized.h
index 2054aa06db0306692a17a1ec0cbd3e39e57d8076..7b016a1436bd5985232ef68ff35fc7df7645ee37 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2011-present Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -691,6 +691,15 @@ struct Synchronized : public SynchronizedBase<
     swap(datum_, rhs);
   }
 
+  /**
+   * Assign another datum and return the original value. Recommended
+   * because it keeps the mutex held only briefly.
+   */
+  T exchange(T&& rhs) {
+    swap(rhs);
+    return std::move(rhs);
+  }
+
   /**
    * Copies datum to a given target.
    */
@@ -757,7 +766,7 @@ using LockedPtrType = typename std::conditional<
     std::is_const<SynchronizedType>::value,
     typename SynchronizedType::ConstLockedPtr,
     typename SynchronizedType::LockedPtr>::type;
-} // detail
+} // namespace detail
 
 /**
  * A helper base class for implementing LockedPtr.