You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

126 line
3.7 KiB

  1. #pragma once
  2. #include <mutex>
  3. #include <memory>
  4. #include <chrono>
  5. namespace cppcore
  6. {
  7. template<
  8. typename T_value,
  9. typename T_lock = std::mutex>
  10. struct locked
  11. {
  12. public:
  13. template<typename T_locked_value>
  14. struct locked_ref;
  15. using value_type = T_value;
  16. using lock_type = T_lock;
  17. using guard_type = std::unique_lock<lock_type>;
  18. using locked_ref_type = locked_ref<value_type>;
  19. using locked_ref_ptr_u = std::unique_ptr<locked_ref_type>;
  20. using locked_const_ref_type = locked_ref<const value_type>;
  21. using locked_const_ref_ptr_u = std::unique_ptr<const locked_ref_type>;
  22. template<typename T_locked_value>
  23. struct locked_ref
  24. {
  25. public:
  26. using value_type = T_locked_value;
  27. using reference_type = value_type&;
  28. using pointer_type = value_type*;
  29. private:
  30. guard_type _guard;
  31. public:
  32. value_type& value;
  33. public:
  34. /**
  35. * @brief Constructor.
  36. */
  37. inline locked_ref(
  38. guard_type&& p_guard,
  39. value_type& p_value);
  40. inline pointer_type operator->();
  41. inline reference_type operator*();
  42. };
  43. private:
  44. mutable lock_type _lock;
  45. value_type _value;
  46. public:
  47. /**
  48. * @brief Constructor.
  49. */
  50. template<typename... X_args>
  51. inline locked(X_args&&... p_args);
  52. public:
  53. /**
  54. * @brief Lock the resource stored in this object and return the locked reference.
  55. */
  56. inline locked_ref_type lock();
  57. /**
  58. * @brief Try to lock the resource stored in this object and return the locked reference.
  59. */
  60. inline locked_ref_ptr_u try_lock();
  61. /**
  62. * @brief Try to lock the resource stored in this object and return the locked reference.
  63. */
  64. template<typename X_value, typename X_ratio>
  65. inline locked_ref_ptr_u try_lock_for(
  66. const std::chrono::duration<X_value, X_ratio>& timeout);
  67. /**
  68. * @brief Try to lock the resource stored in this object and return the locked reference.
  69. */
  70. template<typename X_clock, typename X_duration>
  71. inline locked_ref_ptr_u try_lock_until(
  72. const std::chrono::time_point<X_clock, X_duration>& timeout);
  73. public:
  74. /**
  75. * @brief Lock the resource stored in this object and return the locked reference.
  76. */
  77. inline locked_const_ref_type lock() const;
  78. /**
  79. * @brief Try to lock the resource stored in this object and return the locked reference.
  80. */
  81. inline locked_const_ref_ptr_u try_lock() const;
  82. /**
  83. * @brief Try to lock the resource stored in this object and return the locked reference.
  84. */
  85. template<typename X_value, typename X_ratio>
  86. inline locked_const_ref_ptr_u try_lock_for(
  87. const std::chrono::duration<X_value, X_ratio>& timeout) const;
  88. /**
  89. * @brief Try to lock the resource stored in this object and return the locked reference.
  90. */
  91. template<typename X_clock, typename X_duration>
  92. inline locked_const_ref_ptr_u try_lock_until(
  93. const std::chrono::time_point<X_clock, X_duration>& timeout) const;
  94. };
  95. /**
  96. * @brief Construct a locked object with the passed arguments.
  97. */
  98. template<
  99. typename T_value,
  100. typename... X_args>
  101. inline locked<T_value> make_locked(X_args&&... p_args);
  102. }
  103. #include "locked.inl"