bool spin_mutex::lock() { unsigned long thread_id = ::GetCurrentThreadId(); while (spin_loop(spin_count_, thread_id) == false) { std::this_thread::yield(); } return true; }
bool nonrecursive_lock::try_lock() noexcept { thread_id_t thread_id = get_thread_id(); if (spin_loop(NO_LOCKED_TAG, thread_id, 0) == true) { return true; } return false; }
bool spin_mutex::try_lock() { unsigned long thread_id = ::GetCurrentThreadId(); if (spin_loop(0, thread_id) == true) { return true; } return false; }
bool nonrecursive_lock::lock() noexcept { thread_id_t thread_id = get_thread_id(); std::uint32_t spin_count = spin_count_; while (spin_loop(NO_LOCKED_TAG, thread_id, spin_count) == false) { std::this_thread::yield(); } return true; }
bool nonrecursive_lock::unlock() noexcept { // unlock은 실패하지 못하게 했다. 무한 루프를 돈다. thread_id_t thread_id = get_thread_id(); std::uint32_t spin_count = spin_count_; while (spin_loop(thread_id, NO_LOCKED_TAG, spin_count) == false) { std::this_thread::yield(); } return true; }
static void test_clock_rates(void) { uint64_t diff; unsigned int rtc_start, rtc_end = 0; /* Set clock to 32 MHz. */ clk_sys_set_mode(CLK_SYS_CRYSTAL_OSC, CLK_SYS_DIV_1); diff = spin_loop(1000, &rtc_start, &rtc_end); clk_sys_set_mode(CLK_SYS_CRYSTAL_OSC, CLK_SYS_DIV_1); QM_PRINTF("Fast Clk loop: %d TSC ticks; RTC diff=%d : %d - %d\n", (unsigned int)(diff & 0xffffffff), rtc_end - rtc_start, rtc_end, rtc_start); /* Set clock to 4 MHz. */ clk_sys_set_mode(CLK_SYS_CRYSTAL_OSC, CLK_SYS_DIV_8); diff = spin_loop(1000, &rtc_start, &rtc_end); clk_sys_set_mode(CLK_SYS_CRYSTAL_OSC, CLK_SYS_DIV_1); /* Output is limited to 32 bits here. */ QM_PRINTF("Slow Clk loop: %d TSC ticks; RTC diff=%d : %d - %d\n", (unsigned int)(diff & 0xffffffff), rtc_end - rtc_start, rtc_end, rtc_start); }
bool spin_mutex::try_lock_for(const std::chrono::milliseconds& timeout_ms) { std::chrono::system_clock::time_point start = std::chrono::system_clock::now(); unsigned long thread_id = ::GetCurrentThreadId(); do { if (spin_loop(spin_count_, thread_id) == true) { return true; } std::this_thread::yield(); } while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start) < timeout_ms); return false; }
bool nonrecursive_lock::try_lock_for(const std::chrono::milliseconds& timeout_ms) noexcept { std::chrono::system_clock::time_point start = std::chrono::system_clock::now(); thread_id_t thread_id = get_thread_id(); do { if (spin_loop(NO_LOCKED_TAG, thread_id, spin_count_) == true) { return true; } std::this_thread::yield(); } while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start) < timeout_ms); return false; }