Browse Source

#linq加锁

zxs 4 weeks ago
parent
commit
e8af500efe
4 changed files with 67 additions and 6 deletions
  1. 2 2
      CMakeLists.txt
  2. BIN
      lib/windows/logger/logger.dll
  3. BIN
      lib/windows/logger/logger.lib
  4. 65 4
      robot/robotics/linq.hpp

+ 2 - 2
CMakeLists.txt

@@ -22,6 +22,6 @@ elseif(UNIX)
 endif()
 # 包含子项目。
 # add_subdirectory ("robot")
-# add_subdirectory ("logger")
+add_subdirectory ("logger")
 # add_subdirectory ("drivers")
-add_subdirectory ("generator")
+# add_subdirectory ("generator")

BIN
lib/windows/logger/logger.dll


BIN
lib/windows/logger/logger.lib


+ 65 - 4
robot/robotics/linq.hpp

@@ -13,6 +13,7 @@
 #include <functional>
 #include <algorithm>
 #include <vector>
+#include <mutex>
 #include <map>
 
 namespace robotics {
@@ -51,6 +52,7 @@ namespace robotics {
              * @return
              */
             linq& reset() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 source_ok_.assign(source_.size(), true);
                 return *this;
             }
@@ -59,6 +61,7 @@ namespace robotics {
              * @return
              */
             linq& reset_changed() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 changed_list_.clear();
                 return *this;
             }
@@ -67,6 +70,7 @@ namespace robotics {
              * @return
              */
             linq& submit() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 for (size_t i = 0; i < changed_list_.size(); ++i) {
                     for (auto const& it : changed_event) {
                         it(std::get<0>(changed_list_[i]), std::get<1>(changed_list_[i]), std::get<2>(changed_list_[i]), changed_list_.size() == (i + 1));
@@ -85,6 +89,7 @@ namespace robotics {
              */
             template<typename _Fn, typename _Obj>
             linq& changed_bind(_Fn&& fn, _Obj&& obj) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 changed_event.push_back(std::bind(std::forward<_Fn>(fn), std::forward<_Obj>(obj), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
                 return *this;
             }
@@ -96,6 +101,7 @@ namespace robotics {
              */
             template<typename _Fn>
             linq& changed_bind(_Fn&& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 changed_event.push_back(std::forward<_Fn>(fn));
                 return *this;
             }
@@ -107,6 +113,7 @@ namespace robotics {
              */
             template<typename _Fn>
             linq& where(_Fn&& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 typename std::vector<_Type>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -125,6 +132,7 @@ namespace robotics {
              */
             template<typename _Fn>
             linq& update(_Fn&& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 typename std::vector<_Type>::iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -151,6 +159,7 @@ namespace robotics {
              */
             template<typename _Fn>
             linq& remove(_Fn&& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 typename std::vector<_Type>::const_iterator it;
                 for (std::int64_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -181,6 +190,7 @@ namespace robotics {
              * @return
              */
             linq& remove() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 typename std::vector<_Type>::const_iterator it;
                 for (std::int64_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -222,6 +232,7 @@ namespace robotics {
              * @return
              */
             linq& append(_Type const& value) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 source_.push_back(value);
                 source_ok_.push_back(true);
                 if (!changed_event.empty()) {
@@ -237,6 +248,7 @@ namespace robotics {
              */
             template<typename _Fn>
             linq& order_by(_Fn&& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 std::vector<std::pair<_Type, bool>> alllist;
                 for (std::int64_t i = 0; i < source_.size(); ++i) {
                     alllist.push_back(std::pair(source_[i], source_ok_[i]));
@@ -258,6 +270,7 @@ namespace robotics {
              * @return
              */
             linq& limit(unsigned int page, unsigned int count) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 size_t begin_index = (page - 1) * count;
                 size_t current_index = 0;
                 size_t current_count = 0;
@@ -485,6 +498,7 @@ namespace robotics {
              * @return
              */
             size_t count() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 size_t result = 0;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -508,6 +522,7 @@ namespace robotics {
              */
             template<typename _Ret>
             _Ret avg() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret all = _Ret();
                 size_t count = 0;
                 typename std::vector<_Type>::const_iterator it;
@@ -530,6 +545,7 @@ namespace robotics {
              */
             template<typename _Ret, typename _Fn>
             _Ret avg(_Fn&& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret all = _Ret();
                 size_t count = 0;
                 typename std::vector<_Type>::const_iterator it;
@@ -550,6 +566,7 @@ namespace robotics {
              */
             template<typename _Ret>
             _Ret max() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret result = _Ret();
                 bool ok = true;
                 typename std::vector<_Type>::const_iterator it;
@@ -579,6 +596,7 @@ namespace robotics {
              */
             template<typename _Ret, typename _Fn>
             _Ret max(_Fn&& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret result = _Ret();
                 bool ok = true;
                 typename std::vector<_Type>::const_iterator it;
@@ -607,6 +625,7 @@ namespace robotics {
              */
             template<typename _Ret>
             _Ret min() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret result = _Ret();
                 bool ok = true;
                 typename std::vector<_Type>::const_iterator it;
@@ -636,6 +655,7 @@ namespace robotics {
              */
             template<typename _Ret, typename _Fn>
             _Ret min(_Fn&& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret result = _Ret();
                 bool ok = true;
                 typename std::vector<_Type>::const_iterator it;
@@ -664,6 +684,7 @@ namespace robotics {
              */
             template<typename _Ret>
             _Ret sum() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret result = _Ret();
                 typename std::vector<_Type>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
@@ -684,6 +705,7 @@ namespace robotics {
              */
             template<typename _Ret, typename _Fn>
             _Ret sum(_Fn&& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret result = _Ret();
                 typename std::vector<_Type>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
@@ -701,6 +723,7 @@ namespace robotics {
              * @return
              */
             bool contains(_Type const& value) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 typename std::vector<_Type>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -719,6 +742,7 @@ namespace robotics {
              * @return
              */
             bool contains(std::function<bool(typename std::vector<_Type>::const_iterator)>const& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 typename std::vector<_Type>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -740,6 +764,7 @@ namespace robotics {
              */
             template<typename _Ret, typename _Fn>
             std::vector<_Ret> select(_Fn&& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 std::vector<_Ret> result;
                 typename std::vector<_Type>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
@@ -760,6 +785,7 @@ namespace robotics {
              */
             template<typename _Key, typename _Fn>
             std::map<_Key, std::vector<_Type>> group_by(_Fn&& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 std::map<_Key, std::vector<_Type>> result;
                 typename std::vector<_Type>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
@@ -776,6 +802,7 @@ namespace robotics {
              * @return
              */
             std::vector<_Type> value() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 std::vector<_Type> result;
                 typename std::vector<_Type>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
@@ -805,6 +832,7 @@ namespace robotics {
              * @return
              */
             std::vector<std::shared_ptr<_Type>> source_ptr() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 std::vector<std::shared_ptr<_Type>> result;
                 for (auto& it : source_) {
                     result.push_back(std::make_shared<_Type>(it));
@@ -816,6 +844,7 @@ namespace robotics {
              * @return
              */
             std::vector<std::shared_ptr<_Type>> value_ptr() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 std::vector<std::shared_ptr<_Type>> result;
                 typename std::vector<_Type>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
@@ -838,6 +867,7 @@ namespace robotics {
              * @return
              */
             std::optional<_Type> first() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 typename std::vector<_Type>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -859,6 +889,7 @@ namespace robotics {
              */
             std::vector<std::function<void(_Type const&, _Type const&, linq_change_type, bool)>> changed_event;
         private:
+            std::mutex                                               mutex_;
             std::vector<_Type>&                                      source_;
             std::vector<bool>                                        source_ok_;
             std::vector<std::tuple<_Type, _Type, linq_change_type>>  changed_list_;
@@ -889,6 +920,7 @@ namespace robotics {
              * @return
              */
             linq_ptr& reset() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 source_ok_.assign(source_.size(), true);
                 return *this;
             }
@@ -897,6 +929,7 @@ namespace robotics {
              * @return
              */
             linq_ptr& reset_changed() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 changed_list_.clear();
                 return *this;
             }
@@ -905,6 +938,7 @@ namespace robotics {
              * @return
              */
             linq_ptr& submit() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 for (size_t i = 0; i < changed_list_.size(); ++i) {
                     for (auto const& it : changed_event) {
                         it(std::get<0>(changed_list_[i]), std::get<1>(changed_list_[i]), std::get<2>(changed_list_[i]), changed_list_.size() == (i + 1));
@@ -923,6 +957,7 @@ namespace robotics {
              */
             template<typename _Fn, typename _Obj>
             linq_ptr& changed_bind(_Fn&& fn, _Obj&& obj) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 changed_event.push_back(std::bind(std::forward<_Fn>(fn), std::forward<_Obj>(obj), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
                 return *this;
             }
@@ -934,6 +969,7 @@ namespace robotics {
              */
             template<typename _Fn>
             linq_ptr& changed_bind(_Fn&& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 changed_event.push_back(std::forward<_Fn>(fn));
                 return *this;
             }
@@ -943,6 +979,7 @@ namespace robotics {
              * @return
              */
             linq_ptr& where(std::function<bool(std::shared_ptr<const _Type> const&)> const& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -959,6 +996,7 @@ namespace robotics {
              * @return
              */
             linq_ptr& update(std::function<bool(std::shared_ptr<_Type> const&)> const& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 typename std::vector<std::shared_ptr<_Type>>::iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -983,6 +1021,7 @@ namespace robotics {
              * @return
              */
             linq_ptr& remove(std::function<bool(std::shared_ptr<const _Type> const&)> const& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
                 for (std::int64_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -1013,6 +1052,7 @@ namespace robotics {
              * @return
              */
             linq_ptr& remove() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
                 for (std::int64_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -1074,6 +1114,7 @@ namespace robotics {
              * @return
              */
             linq_ptr& append(std::shared_ptr<_Type> const& value) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 source_.push_back(value);
                 source_ok_.push_back(true);
                 if (!changed_event.empty()) {
@@ -1089,6 +1130,7 @@ namespace robotics {
              */
             template<typename _Fn>
             linq_ptr& order_by(_Fn&& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 std::vector<std::pair<std::shared_ptr<_Type>, bool>> alllist;
                 for (std::int64_t i = 0; i < source_.size(); ++i) {
                     alllist.push_back(std::pair(source_[i], source_ok_[i]));
@@ -1110,6 +1152,7 @@ namespace robotics {
              * @return
              */
             linq_ptr& limit(unsigned int page, unsigned int count) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 size_t begin_index = (page - 1) * count;
                 size_t current_index = 0;
                 size_t current_count = 0;
@@ -1337,6 +1380,7 @@ namespace robotics {
              * @return
              */
             size_t count() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 size_t result = 0;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -1360,6 +1404,7 @@ namespace robotics {
              */
             template<typename _Ret>
             _Ret avg() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret all = _Ret();
                 size_t count = 0;
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
@@ -1381,6 +1426,7 @@ namespace robotics {
              */
             template<typename _Ret>
             _Ret avg(std::function<_Ret(std::shared_ptr<const _Type> const&)> const& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret all = _Ret();
                 size_t count = 0;
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
@@ -1401,6 +1447,7 @@ namespace robotics {
              */
             template<typename _Ret>
             _Ret max() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret result = _Ret();
                 bool ok = true;
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
@@ -1429,6 +1476,7 @@ namespace robotics {
              */
             template<typename _Ret>
             _Ret max(std::function<_Ret(std::shared_ptr<const _Type> const&)> const& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret result = _Ret();
                 bool ok = true;
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
@@ -1457,6 +1505,7 @@ namespace robotics {
              */
             template<typename _Ret>
             _Ret min() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret result = _Ret();
                 bool ok = true;
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
@@ -1484,7 +1533,8 @@ namespace robotics {
              * @return
              */
             template<typename _Ret>
-            _Ret min_(std::function<_Ret(std::shared_ptr<const _Type> const&)> const& fn) {
+            _Ret min(std::function<_Ret(std::shared_ptr<const _Type> const&)> const& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret result = _Ret();
                 bool ok = true;
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
@@ -1513,6 +1563,7 @@ namespace robotics {
              */
             template<typename _Ret>
             _Ret sum() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret result = _Ret();
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
@@ -1532,6 +1583,7 @@ namespace robotics {
              */
             template<typename _Ret>
             _Ret sum(std::function<_Ret(std::shared_ptr<const _Type> const&)> const& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 _Ret result = _Ret();
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
@@ -1549,6 +1601,7 @@ namespace robotics {
              * @return
              */
             bool contains(std::shared_ptr<_Type> const& value) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -1567,6 +1620,7 @@ namespace robotics {
              * @return
              */
             bool contains(std::function<bool(std::shared_ptr<const _Type> const&)>const& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -1587,6 +1641,7 @@ namespace robotics {
              */
             template<typename _Ret>
             std::vector<_Ret> select(std::function<_Ret(std::shared_ptr<const _Type> const&)> const& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 std::vector<_Ret> result;
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
@@ -1606,6 +1661,7 @@ namespace robotics {
              */
             template<typename _Key>
             std::map<_Key, std::vector<std::shared_ptr<_Type>>> group_by(std::function<_Key(std::shared_ptr<const _Type> const&)> const& fn) {
+                std::lock_guard<std::mutex> locker(mutex_);
                 std::map<_Key, std::vector<std::shared_ptr<_Type>>> result;
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
@@ -1622,6 +1678,7 @@ namespace robotics {
              * @return
              */
             std::vector<std::shared_ptr<_Type>> value_ptr() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 std::vector<std::shared_ptr<_Type>> result;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -1636,6 +1693,7 @@ namespace robotics {
              * @return
              */
             std::shared_ptr<_Type> first() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 std::shared_ptr<_Type> result = nullptr;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -1670,6 +1728,7 @@ namespace robotics {
              * @return
              */
             std::vector<_Type> value() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 std::vector<_Type> result;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -1684,6 +1743,7 @@ namespace robotics {
              * @return
              */
             std::vector<_Type> source() {
+                std::lock_guard<std::mutex> locker(mutex_);
                 std::vector<_Type> result;
                 for (auto& it : source_) {
                     result.push_back(*it);
@@ -1701,9 +1761,10 @@ namespace robotics {
              */
             std::vector<std::function<void(_Type const&, _Type const&, linq_change_type, bool)>> changed_event;
         private:
-            std::vector<bool> source_ok_;
-            std::vector<std::shared_ptr<_Type>>& source_;
-            std::vector<std::tuple<_Type, _Type, linq_change_type>> changed_list_;
+            std::mutex                                               mutex_;
+            std::vector<bool>                                        source_ok_;
+            std::vector<std::shared_ptr<_Type>>&                     source_;
+            std::vector<std::tuple<_Type, _Type, linq_change_type>>  changed_list_;
         };
         /**
          * @brief from