Synchronized.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #pragma once
  2. #include <mutex>
  3. #include <c10/util/C++17.h>
  4. namespace c10 {
  5. /**
  6. * A very simple Synchronization class for error-free use of data
  7. * in a multi-threaded context. See folly/docs/Synchronized.md for
  8. * the inspiration of this class.
  9. *
  10. * Full URL:
  11. * https://github.com/facebook/folly/blob/main/folly/docs/Synchronized.md
  12. *
  13. * This class implements a small subset of the generic functionality
  14. * implemented by folly:Synchronized<T>. Specifically, only withLock<T>
  15. * is implemeted here since it's the smallest possible API that is
  16. * able to cover a large surface area of functionality offered by
  17. * folly::Synchronized<T>.
  18. */
  19. template <typename T>
  20. class Synchronized final {
  21. mutable std::mutex mutex_;
  22. T data_;
  23. public:
  24. Synchronized() = default;
  25. Synchronized(T const& data) : data_(data) {}
  26. Synchronized(T&& data) : data_(data) {}
  27. // Don't permit copy construction, move, assignment, or
  28. // move assignment, since the underlying std::mutex
  29. // isn't necessarily copyable/moveable.
  30. Synchronized(Synchronized const&) = delete;
  31. Synchronized(Synchronized&&) = delete;
  32. Synchronized operator=(Synchronized const&) = delete;
  33. Synchronized operator=(Synchronized&&) = delete;
  34. /**
  35. * To use, call withLock<T> with a callback that accepts T either
  36. * by copy or by reference. Use the protected variable in the
  37. * provided callback safely.
  38. */
  39. template <typename CB>
  40. typename c10::invoke_result_t<CB, T&> withLock(CB cb) {
  41. std::lock_guard<std::mutex> guard(this->mutex_);
  42. return cb(this->data_);
  43. }
  44. /**
  45. * To use, call withLock<T> with a callback that accepts T either
  46. * by copy or by const reference. Use the protected variable in
  47. * the provided callback safely.
  48. */
  49. template <typename CB>
  50. typename c10::invoke_result_t<CB, T const&> withLock(CB cb) const {
  51. std::lock_guard<std::mutex> guard(this->mutex_);
  52. return cb(this->data_);
  53. }
  54. };
  55. } // end namespace c10