susumu.yata
null+****@clear*****
Wed May 15 10:35:54 JST 2013
susumu.yata 2013-05-15 10:35:54 +0900 (Wed, 15 May 2013) New Revision: cedc397d9e33b1cf6090c48c5b54876bbdc872b6 https://github.com/groonga/grnxx/commit/cedc397d9e33b1cf6090c48c5b54876bbdc872b6 Message: Fix a bug that PeriodicClock causes a critical error. Modified files: lib/grnxx/time/periodic_clock.cpp Modified: lib/grnxx/time/periodic_clock.cpp (+16 -24) =================================================================== --- lib/grnxx/time/periodic_clock.cpp 2013-05-14 19:11:23 +0900 (8484a79) +++ lib/grnxx/time/periodic_clock.cpp 2013-05-15 10:35:54 +0900 (50fde30) @@ -17,8 +17,9 @@ */ #include "grnxx/time/periodic_clock.hpp" -#include "grnxx/intrinsic.hpp" #include "grnxx/thread.hpp" +#include "grnxx/lock.hpp" +#include "grnxx/mutex.hpp" namespace grnxx { namespace { @@ -29,40 +30,31 @@ constexpr Duration UPDATE_INTERVAL = Duration::milliseconds(100); volatile uint32_t ref_count = 0; grnxx::Thread *thread = nullptr; +Mutex mutex(MUTEX_UNLOCKED); } // namespace Time PeriodicClock::now_ = Time::min(); PeriodicClock::PeriodicClock() { - for ( ; ; ) { - const uint32_t count = ref_count; - if (atomic_compare_and_swap(count, count + 1, &ref_count)) { - if (count == 0) { - // Start the internal thread. - thread = grnxx::Thread::create(routine); - if (thread) { - now_ = SystemClock::now(); - } - } - break; + // Start the internal thread iff this is the first object. + Lock lock(&mutex); + if (++ref_count == 1) { + thread = grnxx::Thread::create(routine); + if (thread) { + now_ = SystemClock::now(); } } } PeriodicClock::~PeriodicClock() { - for ( ; ; ) { - const uint32_t count = ref_count; - if (atomic_compare_and_swap(count, count - 1, &ref_count)) { - if (count == 1) { - // Stop the running thread. - thread->join(); - delete thread; - thread = nullptr; - now_ = Time::min(); - } - break; - } + // Stop the running thread iff this is the last object. + Lock lock(&mutex); + if (--ref_count == 0) { + thread->join(); + delete thread; + thread = nullptr; + now_ = Time::min(); } } -------------- next part -------------- HTML����������������������������...Download