susumu.yata
null+****@clear*****
Tue Apr 30 22:28:13 JST 2013
susumu.yata 2013-04-30 22:28:13 +0900 (Tue, 30 Apr 2013) New Revision: d788d74740cad3d22b3919be6b13415df51bd37f https://github.com/groonga/grnxx/commit/d788d74740cad3d22b3919be6b13415df51bd37f Message: Remove grnxx::InternalClock. Removed files: lib/grnxx/time/internal_clock.cpp lib/grnxx/time/internal_clock.hpp Modified files: lib/grnxx/time/Makefile.am test/test_time.cpp Modified: lib/grnxx/time/Makefile.am (+0 -2) =================================================================== --- lib/grnxx/time/Makefile.am 2013-04-30 22:10:43 +0900 (2511296) +++ lib/grnxx/time/Makefile.am 2013-04-30 22:28:13 +0900 (edeb3ac) @@ -5,7 +5,6 @@ libgrnxx_time_la_LDFLAGS = @AM_LTLDFLAGS@ libgrnxx_time_la_SOURCES = \ broken_down_time.cpp \ duration.cpp \ - internal_clock.cpp \ periodic_clock.cpp \ stopwatch.cpp \ system_clock.cpp \ @@ -15,7 +14,6 @@ libgrnxx_time_includedir = ${includedir}/grnxx/time libgrnxx_time_include_HEADERS = \ broken_down_time.hpp \ duration.hpp \ - internal_clock.hpp \ periodic_clock.hpp \ stopwatch.hpp \ system_clock.hpp \ Deleted: lib/grnxx/time/internal_clock.cpp (+0 -96) 100644 =================================================================== --- lib/grnxx/time/internal_clock.cpp 2013-04-30 22:10:43 +0900 (719b2c0) +++ /dev/null @@ -1,96 +0,0 @@ -/* - Copyright (C) 2013 Brazil, Inc. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ -#include "grnxx/time/internal_clock.hpp" - -#include "../config.h" - -#ifdef HAVE_PTHREAD_ATFORK -# include <pthread.h> -#endif // HAVE_PTHREAD_ATFORK - -#include <thread> - -#include "grnxx/error.hpp" -#include "grnxx/lock.hpp" -#include "grnxx/logger.hpp" -#include "grnxx/thread.hpp" -#include "grnxx/time/system_clock.hpp" - -namespace grnxx { -namespace { - -// Accuracy of the internal clock. Note that a short sleep may lead to a -// busy-wait loop, which exhausts CPU resources. -constexpr Duration UPDATE_INTERVAL = Duration::milliseconds(100); - -Time *internal_time = nullptr; - -// Update the internal clock periodically. -void internal_clock_routine() { - // TODO: Fix this endless loop. - while (true) { - Thread::sleep_for(UPDATE_INTERVAL); - *internal_time = SystemClock::now(); - } -} - -// Start a thread to update the internal clock. -void start_internal_clock() { - try { - std::thread thread(internal_clock_routine); - thread.detach(); - } catch (...) { - // Failed to start thread. - *internal_time = Time::min(); - return; - } - -#ifdef HAVE_PTHREAD_ATFORK - // Start a new thread, if fork() is invoked, on the child process. - int error = ::pthread_atfork(nullptr, nullptr, start_internal_clock); - if (error != 0) { - // The current process works well even if this failed. - GRNXX_WARNING() << "failed to set a fork handler: '::pthread_atfork' " - << Error(error); - } -#endif // HAVE_PTHREAD_ATFORK - - *internal_time = SystemClock::now(); -} - -} // namespace - -Time InternalClock::now_ = Time::min(); - -Time InternalClock::start() { - if (!internal_time) { - static Mutex mutex(MUTEX_UNLOCKED); - Lock lock(&mutex); - if (!internal_time) { - internal_time = &now_; - start_internal_clock(); - if (now_ != Time::min()) { - return now_; - } - } - } - // Use the system clock if the internal clock is not available. - return SystemClock::now(); -} - -} // namespace grnxx Deleted: lib/grnxx/time/internal_clock.hpp (+0 -43) 100644 =================================================================== --- lib/grnxx/time/internal_clock.hpp 2013-04-30 22:10:43 +0900 (c8dd74b) +++ /dev/null @@ -1,43 +0,0 @@ -/* - Copyright (C) 2013 Brazil, Inc. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ -#ifndef GRNXX_TIME_INTERNAL_CLOCK_HPP -#define GRNXX_TIME_INTERNAL_CLOCK_HPP - -#include "grnxx/basic.hpp" -#include "grnxx/time/time.hpp" - -namespace grnxx { - -class InternalClock { - public: - static Time now() { - if (now_ == Time::min()) { - return start(); - } - return now_; - } - - private: - static Time now_; - - static Time start(); -}; - -} // namespace grnxx - -#endif // GRNXX_TIME_INTERNAL_CLOCK_HPP Modified: test/test_time.cpp (+0 -38) =================================================================== --- test/test_time.cpp 2013-04-30 22:10:43 +0900 (12cefa7) +++ test/test_time.cpp 2013-04-30 22:28:13 +0900 (1406966) @@ -20,7 +20,6 @@ #include "grnxx/logger.hpp" #include "grnxx/thread.hpp" #include "grnxx/time/broken_down_time.hpp" -#include "grnxx/time/internal_clock.hpp" #include "grnxx/time/periodic_clock.hpp" #include "grnxx/time/stopwatch.hpp" #include "grnxx/time/system_clock.hpp" @@ -77,42 +76,6 @@ void test_system_clock() { << (1000.0 * elapsed.count() / LOOP_COUNT); } -void test_internal_clock() { - grnxx::Time time = grnxx::InternalClock::now(); - GRNXX_NOTICE() << "grnxx::InternalClock::now(): " << time; - GRNXX_NOTICE() << "grnxx::InternalClock::now().local_time(): " - << time.local_time(); - - time = grnxx::InternalClock::now(); - GRNXX_NOTICE() << "grnxx::InternalClock::now(): " << time; - GRNXX_NOTICE() << "grnxx::InternalClock::now().local_time(): " - << time.local_time(); - - grnxx::Thread::sleep_for(grnxx::Duration::milliseconds(310)); - - time = grnxx::InternalClock::now(); - GRNXX_NOTICE() << "grnxx::InternalClock::now(): " << time; - GRNXX_NOTICE() << "grnxx::InternalClock::now().local_time(): " - << time.local_time(); - - grnxx::Thread::sleep_for(grnxx::Duration::milliseconds(310)); - - time = grnxx::InternalClock::now(); - GRNXX_NOTICE() << "grnxx::InternalClock::now(): " << time; - GRNXX_NOTICE() << "grnxx::InternalClock::now().local_time(): " - << time.local_time(); - - enum { LOOP_COUNT = 1 << 20 }; - - grnxx::Stopwatch stopwatch(true); - for (int i = 0; i < LOOP_COUNT; ++i) { - grnxx::InternalClock::now(); - } - grnxx::Duration elapsed = stopwatch.elapsed(); - GRNXX_NOTICE() << "grnxx::InternalClock::now: average elapsed [ns] = " - << (1000.0 * elapsed.count() / LOOP_COUNT); -} - void test_periodic_clock() { grnxx::PeriodicClock clock; @@ -197,7 +160,6 @@ int main() { test_time(); test_broken_down_time(); test_system_clock(); - test_internal_clock(); test_periodic_clock(); test_stopwatch(); -------------- next part -------------- HTML����������������������������...Download