susumu.yata
null+****@clear*****
Fri Mar 1 19:00:23 JST 2013
susumu.yata 2013-03-01 19:00:23 +0900 (Fri, 01 Mar 2013) New Revision: afe2672b30f9a89c6f5665b558a0b8e7b4a658c8 https://github.com/groonga/grnxx/commit/afe2672b30f9a89c6f5665b558a0b8e7b4a658c8 Log: Rename switch_to_others() to yield() and use std::this_thread::yield(). Modified files: lib/mutex.cpp lib/thread.cpp lib/thread.hpp test/test_thread.cpp Modified: lib/mutex.cpp (+2 -2) =================================================================== --- lib/mutex.cpp 2013-03-01 18:26:05 +0900 (e3e8038) +++ lib/mutex.cpp 2013-03-01 19:00:23 +0900 (db3980a) @@ -33,7 +33,7 @@ void Mutex::lock_without_timeout() { if (try_lock()) { return; } - Thread::switch_to_others(); + Thread::yield(); } while (!try_lock()) { @@ -65,7 +65,7 @@ bool Mutex::lock_with_timeout(Duration timeout) { if (try_lock()) { return true; } - Thread::switch_to_others(); + Thread::yield(); } while (!has_deadline || (stopwatch.elapsed() < timeout)) { Modified: lib/thread.cpp (+4 -12) =================================================================== --- lib/thread.cpp 2013-03-01 18:26:05 +0900 (9b27d4b) +++ lib/thread.cpp 2013-03-01 19:00:23 +0900 (e7afb16) @@ -24,24 +24,16 @@ # include <windows.h> #endif // GRNXX_WINDOWS -#ifdef GRNXX_HAS_SCHED_YIELD -# include <sched.h> -#endif // GRNXX_HAS_SCHED_YIELD - #ifdef GRNXX_HAS_NANOSLEEP # include <time.h> #endif // GRNXX_HAS_NANOSLEEP +#include <thread> + namespace grnxx { -bool Thread::switch_to_others() { -#ifdef GRNXX_WINDOWS - return ::SwitchToThread() != 0; -#elif defined(GRNXX_HAS_SCHED_YIELD) - return ::sched_yield() == 0; -#else // defined(GRNXX_HAS_SCHED_YIELD) - sleep(Duration(0)); -#endif // defined(GRNXX_HAS_SCHED_YIELD) +void Thread::yield() { + std::this_thread::yield(); } void Thread::sleep(Duration duration) { Modified: lib/thread.hpp (+1 -1) =================================================================== --- lib/thread.hpp 2013-03-01 18:26:05 +0900 (b3a74ba) +++ lib/thread.hpp 2013-03-01 19:00:23 +0900 (f62032f) @@ -25,7 +25,7 @@ namespace grnxx { class Thread { public: - static bool switch_to_others(); + static void yield(); static void sleep(Duration duration); private: Modified: test/test_thread.cpp (+2 -2) =================================================================== --- test/test_thread.cpp 2013-03-01 18:26:05 +0900 (ef5d8c9) +++ test/test_thread.cpp 2013-03-01 19:00:23 +0900 (8e63359) @@ -30,10 +30,10 @@ int main() { grnxx::Stopwatch stopwatch(true); for (int i = 0; i < LOOP_COUNT; ++i) { - grnxx::Thread::switch_to_others(); + grnxx::Thread::yield(); } grnxx::Duration elapsed = stopwatch.elapsed(); - GRNXX_NOTICE() << "switch_to_others(): elapsed [ns]: " + GRNXX_NOTICE() << "yield(): elapsed [ns]: " << (1000.0 * elapsed.count() / LOOP_COUNT); stopwatch.reset(); -------------- next part -------------- HTML����������������������������...Download