From c6cdd4a9883ebf879ba7e94cfcdb3a8275c0cdd1 Mon Sep 17 00:00:00 2001 From: Vincent Lee Date: Mon, 15 Sep 2014 18:04:36 +0800 Subject: [PATCH] fix deque erase, the end() iterator cannot be used as erase position. --- include/boost/fiber/condition.hpp | 37 ++++++++++++++++++++----------- src/recursive_timed_mutex.cpp | 18 ++++++++++----- src/timed_mutex.cpp | 18 ++++++++++----- 3 files changed, 48 insertions(+), 25 deletions(-) diff --git a/include/boost/fiber/condition.hpp b/include/boost/fiber/condition.hpp index 3af513d5..180a1765 100644 --- a/include/boost/fiber/condition.hpp +++ b/include/boost/fiber/condition.hpp @@ -132,9 +132,12 @@ public: catch (...) { unique_lock< detail::spinlock > lk( splk_); - // remove fiber from waiting-list - waiting_.erase( - std::find( waiting_.begin(), waiting_.end(), n) ); + std::deque< detail::fiber_base * >::iterator wit = std::find( waiting_.begin(), waiting_.end(), n); + if (wit != waiting_.end()) + { + // remove fiber from waiting-list + waiting_.erase( wit ); + } throw; } } @@ -169,9 +172,12 @@ public: // this fiber was not notified before timeout // lock spinlock again unique_lock< detail::spinlock > lk( splk_); - // remove fiber from waiting-list - waiting_.erase( - std::find( waiting_.begin(), waiting_.end(), n) ); + std::deque< detail::fiber_base * >::iterator wit = std::find( waiting_.begin(), waiting_.end(), n); + if (wit != waiting_.end()) + { + // remove fiber from waiting-list + waiting_.erase( wit ); + } status = cv_status::timeout; } @@ -209,12 +215,14 @@ public: // timeout happend before notified // lock spinlock unique_lock< detail::spinlock > lk( splk_); - // remove fiber from waiting-list - waiting_.erase( - std::find( waiting_.begin(), waiting_.end(), n) ); + std::deque< detail::fiber_base * >::iterator wit = std::find( waiting_.begin(), waiting_.end(), n); + if (wit != waiting_.end()) + { + // remove fiber from waiting-list + waiting_.erase(wit); + } status = cv_status::timeout; - break; } // run scheduler @@ -228,9 +236,12 @@ public: catch (...) { unique_lock< detail::spinlock > lk( splk_); - // remove fiber from waiting-list - waiting_.erase( - std::find( waiting_.begin(), waiting_.end(), n) ); + std::deque< detail::fiber_base * >::iterator wit = std::find( waiting_.begin(), waiting_.end(), n); + if (wit != waiting_.end()) + { + // remove fiber from waiting-list + waiting_.erase( wit ); + } throw; } diff --git a/src/recursive_timed_mutex.cpp b/src/recursive_timed_mutex.cpp index cdc8f78a..9ff6e868 100644 --- a/src/recursive_timed_mutex.cpp +++ b/src/recursive_timed_mutex.cpp @@ -136,9 +136,12 @@ recursive_timed_mutex::try_lock_until( chrono::high_resolution_clock::time_point if ( ! fm_wait_until( timeout_time, lk) ) { lk.lock(); - // remove fiber from waiting-list - waiting_.erase( - std::find( waiting_.begin(), waiting_.end(), n) ); + std::deque< detail::fiber_base * >::iterator wit = std::find( waiting_.begin(), waiting_.end(), n); + if (wit != waiting_.end()) + { + // remove fiber from waiting-list + waiting_.erase( wit ); + } lk.unlock(); return false; } @@ -170,9 +173,12 @@ recursive_timed_mutex::try_lock_until( chrono::high_resolution_clock::time_point if ( chrono::high_resolution_clock::now() > timeout_time) { lk.lock(); - // remove fiber from waiting-list - waiting_.erase( - std::find( waiting_.begin(), waiting_.end(), n) ); + std::deque< detail::fiber_base * >::iterator wit = std::find( waiting_.begin(), waiting_.end(), n); + if (wit != waiting_.end()) + { + // remove fiber from waiting-list + waiting_.erase( wit ); + } lk.unlock(); return false; } diff --git a/src/timed_mutex.cpp b/src/timed_mutex.cpp index 51eea444..21d651a9 100644 --- a/src/timed_mutex.cpp +++ b/src/timed_mutex.cpp @@ -125,9 +125,12 @@ timed_mutex::try_lock_until( chrono::high_resolution_clock::time_point const& ti if ( ! fm_wait_until( timeout_time, lk) ) { lk.lock(); - // remove fiber from waiting-list - waiting_.erase( - std::find( waiting_.begin(), waiting_.end(), n) ); + std::deque< detail::fiber_base * >::iterator wit = std::find( waiting_.begin(), waiting_.end(), n); + if (wit != waiting_.end()) + { + // remove fiber from waiting-list + waiting_.erase( wit ); + } lk.unlock(); return false; } @@ -159,9 +162,12 @@ timed_mutex::try_lock_until( chrono::high_resolution_clock::time_point const& ti if ( chrono::high_resolution_clock::now() > timeout_time) { lk.lock(); - // remove fiber from waiting-list - waiting_.erase( - std::find( waiting_.begin(), waiting_.end(), n) ); + std::deque< detail::fiber_base * >::iterator wit = std::find( waiting_.begin(), waiting_.end(), n); + if (wit != waiting_.end()) + { + // remove fiber from waiting-list + waiting_.erase( wit ); + } lk.unlock(); return false; }