susumu.yata
null+****@clear*****
Fri Mar 1 19:18:33 JST 2013
susumu.yata 2013-03-01 19:18:33 +0900 (Fri, 01 Mar 2013) New Revision: d4f90b4a9937849fe45bb477f6dc2f38849a5640 https://github.com/groonga/grnxx/commit/d4f90b4a9937849fe45bb477f6dc2f38849a5640 Log: Rename sleep() to sleep_for() and add sleep_until(). Modified files: lib/internal_clock.cpp lib/io/file-posix.cpp lib/io/file-windows.cpp lib/io/pool-impl.cpp lib/mutex.cpp lib/thread.cpp lib/thread.hpp test/test_internal_clock.cpp test/test_stopwatch.cpp test/test_thread.cpp Modified: lib/internal_clock.cpp (+1 -1) =================================================================== --- lib/internal_clock.cpp 2013-03-01 19:00:23 +0900 (930769b) +++ lib/internal_clock.cpp 2013-03-01 19:18:33 +0900 (e23050f) @@ -44,7 +44,7 @@ Time *internal_time = nullptr; void internal_clock_routine() { // TODO: Fix this endless loop. while (true) { - Thread::sleep(UPDATE_INTERVAL); + Thread::sleep_for(UPDATE_INTERVAL); *internal_time = SystemClock::now(); } } Modified: lib/io/file-posix.cpp (+2 -2) =================================================================== --- lib/io/file-posix.cpp 2013-03-01 19:00:23 +0900 (244f6a5) +++ lib/io/file-posix.cpp 2013-03-01 19:18:33 +0900 (4310004) @@ -106,7 +106,7 @@ void FileImpl::lock(FileLockMode mode) { GRNXX_THROW(); } while (!try_lock(mode)) { - Thread::sleep(FILE_LOCK_SLEEP_DURATION); + Thread::sleep_for(FILE_LOCK_SLEEP_DURATION); } } @@ -123,7 +123,7 @@ bool FileImpl::lock(FileLockMode mode, Duration timeout) { if (try_lock(mode)) { return true; } - Thread::sleep(FILE_LOCK_SLEEP_DURATION); + Thread::sleep_for(FILE_LOCK_SLEEP_DURATION); } return false; } Modified: lib/io/file-windows.cpp (+2 -2) =================================================================== --- lib/io/file-windows.cpp 2013-03-01 19:00:23 +0900 (bd47ad5) +++ lib/io/file-windows.cpp 2013-03-01 19:18:33 +0900 (2bcb4ea) @@ -101,7 +101,7 @@ void FileImpl::lock(FileLockMode mode) { GRNXX_THROW(); } while (!try_lock(mode)) { - Thread::sleep(FILE_LOCK_SLEEP_DURATION); + Thread::sleep_for(FILE_LOCK_SLEEP_DURATION); } } @@ -118,7 +118,7 @@ bool FileImpl::lock(FileLockMode mode, Duration timeout) { if (try_lock(mode)) { return true; } - Thread::sleep(FILE_LOCK_SLEEP_DURATION); + Thread::sleep_for(FILE_LOCK_SLEEP_DURATION); } return false; } Modified: lib/io/pool-impl.cpp (+1 -1) =================================================================== --- lib/io/pool-impl.cpp 2013-03-01 19:00:23 +0900 (0661141) +++ lib/io/pool-impl.cpp 2013-03-01 19:18:33 +0900 (6707b8f) @@ -343,7 +343,7 @@ void PoolImpl::open_regular_pool(PoolFlags flags, const char *path, if (files_[0]->size() != 0) { break; } - Thread::sleep(Duration::milliseconds(10)); + Thread::sleep_for(Duration::milliseconds(10)); } } if (files_[0]->lock(FILE_LOCK_SHARED, Duration::seconds(10))) { Modified: lib/mutex.cpp (+2 -2) =================================================================== --- lib/mutex.cpp 2013-03-01 19:00:23 +0900 (db3980a) +++ lib/mutex.cpp 2013-03-01 19:18:33 +0900 (3c79343) @@ -37,7 +37,7 @@ void Mutex::lock_without_timeout() { } while (!try_lock()) { - Thread::sleep(MUTEX_SLEEP_DURATION); + Thread::sleep_for(MUTEX_SLEEP_DURATION); } } @@ -72,7 +72,7 @@ bool Mutex::lock_with_timeout(Duration timeout) { if (try_lock()) { return true; } - Thread::sleep(MUTEX_SLEEP_DURATION); + Thread::sleep_for(MUTEX_SLEEP_DURATION); } return false; Modified: lib/thread.cpp (+10 -1) =================================================================== --- lib/thread.cpp 2013-03-01 19:00:23 +0900 (e7afb16) +++ lib/thread.cpp 2013-03-01 19:18:33 +0900 (1e48e52) @@ -30,13 +30,15 @@ #include <thread> +#include "system_clock.hpp" + namespace grnxx { void Thread::yield() { std::this_thread::yield(); } -void Thread::sleep(Duration duration) { +void Thread::sleep_for(Duration duration) { #ifdef GRNXX_WINDOWS if (duration.count() < 0) { ::Sleep(0); @@ -77,4 +79,11 @@ void Thread::sleep(Duration duration) { #endif // defined(GRNXX_HAS_NANOSLEEP) } +void Thread::sleep_until(Time time) { + const Time now = SystemClock::now(); + if (time > now) { + sleep_for(time - now); + } +} + } // namespace grnxx Modified: lib/thread.hpp (+4 -2) =================================================================== --- lib/thread.hpp 2013-03-01 19:00:23 +0900 (f62032f) +++ lib/thread.hpp 2013-03-01 19:18:33 +0900 (6b06650) @@ -19,14 +19,16 @@ #define GRNXX_THREAD_HPP #include "basic.hpp" -#include "duration.hpp" +#include "time.hpp" namespace grnxx { class Thread { public: static void yield(); - static void sleep(Duration duration); + + static void sleep_for(Duration duration); + static void sleep_until(Time time); private: Thread(const Thread &); Modified: test/test_internal_clock.cpp (+2 -2) =================================================================== --- test/test_internal_clock.cpp 2013-03-01 19:00:23 +0900 (9460960) +++ test/test_internal_clock.cpp 2013-03-01 19:18:33 +0900 (f249a5e) @@ -39,14 +39,14 @@ int main() { GRNXX_NOTICE() << "grnxx::InternalClock::now().local_time(): " << time.local_time(); - grnxx::Thread::sleep(grnxx::Duration::milliseconds(500)); + grnxx::Thread::sleep_for(grnxx::Duration::milliseconds(500)); time = grnxx::InternalClock::now(); GRNXX_NOTICE() << "grnxx::InternalClock::now(): " << time; GRNXX_NOTICE() << "grnxx::InternalClock::now().local_time(): " << time.local_time(); - grnxx::Thread::sleep(grnxx::Duration::milliseconds(500)); + grnxx::Thread::sleep_for(grnxx::Duration::milliseconds(500)); time = grnxx::InternalClock::now(); GRNXX_NOTICE() << "grnxx::InternalClock::now(): " << time; Modified: test/test_stopwatch.cpp (+6 -6) =================================================================== --- test/test_stopwatch.cpp 2013-03-01 19:00:23 +0900 (170f145) +++ test/test_stopwatch.cpp 2013-03-01 19:18:33 +0900 (5401711) @@ -30,28 +30,28 @@ int main() { assert(stopwatch.elapsed() == grnxx::Duration(0)); stopwatch.start(); - grnxx::Thread::sleep(grnxx::Duration::milliseconds(1)); + grnxx::Thread::sleep_for(grnxx::Duration::milliseconds(1)); grnxx::Duration elapsed = stopwatch.elapsed(); assert(elapsed > grnxx::Duration(0)); - grnxx::Thread::sleep(grnxx::Duration::milliseconds(1)); + grnxx::Thread::sleep_for(grnxx::Duration::milliseconds(1)); assert(stopwatch.elapsed() > elapsed); stopwatch.stop(); elapsed = stopwatch.elapsed(); - grnxx::Thread::sleep(grnxx::Duration::milliseconds(1)); + grnxx::Thread::sleep_for(grnxx::Duration::milliseconds(1)); assert(stopwatch.elapsed() == elapsed); stopwatch.start(); - grnxx::Thread::sleep(grnxx::Duration::milliseconds(1)); + grnxx::Thread::sleep_for(grnxx::Duration::milliseconds(1)); assert(stopwatch.elapsed() > elapsed); GRNXX_NOTICE() << "stopwatch.elapsed() = " << stopwatch.elapsed(); elapsed = stopwatch.elapsed(); stopwatch.reset(); - grnxx::Thread::sleep(grnxx::Duration::milliseconds(1)); + grnxx::Thread::sleep_for(grnxx::Duration::milliseconds(1)); assert(stopwatch.elapsed() < elapsed); assert(stopwatch.elapsed() > grnxx::Duration(0)); @@ -59,7 +59,7 @@ int main() { stopwatch.reset(); assert(stopwatch.elapsed() == grnxx::Duration(0)); - grnxx::Thread::sleep(grnxx::Duration::milliseconds(1)); + grnxx::Thread::sleep_for(grnxx::Duration::milliseconds(1)); assert(stopwatch.elapsed() == grnxx::Duration(0)); return 0; Modified: test/test_thread.cpp (+12 -4) =================================================================== --- test/test_thread.cpp 2013-03-01 19:00:23 +0900 (8e63359) +++ test/test_thread.cpp 2013-03-01 19:18:33 +0900 (6fb9be8) @@ -20,6 +20,7 @@ #include "logger.hpp" #include "thread.hpp" #include "stopwatch.hpp" +#include "system_clock.hpp" int main() { grnxx::Logger::set_flags(grnxx::LOGGER_WITH_ALL | @@ -38,16 +39,23 @@ int main() { stopwatch.reset(); for (int i = 0; i < LOOP_COUNT; ++i) { - grnxx::Thread::sleep(grnxx::Duration(0)); + grnxx::Thread::sleep_for(grnxx::Duration(0)); } elapsed = stopwatch.elapsed(); - GRNXX_NOTICE() << "sleep(0): elapsed [ns]: " + GRNXX_NOTICE() << "sleep_for(0): elapsed [ns]: " << (1000.0 * elapsed.count() / LOOP_COUNT); stopwatch.reset(); - grnxx::Thread::sleep(grnxx::Duration::milliseconds(10)); + grnxx::Thread::sleep_for(grnxx::Duration::milliseconds(10)); elapsed = stopwatch.elapsed(); - GRNXX_NOTICE() << "sleep(10ms): elapsed [ns] = " + GRNXX_NOTICE() << "sleep_for(10ms): elapsed [ns] = " + << (1000.0 * elapsed.count()); + + stopwatch.reset(); + grnxx::Thread::sleep_until(grnxx::SystemClock::now() + + grnxx::Duration::milliseconds(10)); + elapsed = stopwatch.elapsed(); + GRNXX_NOTICE() << "sleep_until(now + 10ms): elapsed [ns] = " << (1000.0 * elapsed.count()); return 0; -------------- next part -------------- HTML����������������������������...다운로드