28 lines
		
	
	
		
			587 B
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			28 lines
		
	
	
		
			587 B
		
	
	
	
		
			C++
		
	
	
	
	
	
 | 
						|
 | 
						|
#pragma once
 | 
						|
 | 
						|
#if !defined(MIJIN_ASYNC_COROUTINE_SLEEP_HPP_INCLUDED)
 | 
						|
#define MIJIN_ASYNC_COROUTINE_SLEEP_HPP_INCLUDED 1
 | 
						|
 | 
						|
#include <chrono>
 | 
						|
#include "./coroutine.hpp"
 | 
						|
 | 
						|
namespace mijin
 | 
						|
{
 | 
						|
template<typename Rep, typename Period>
 | 
						|
Task<> c_sleep(std::chrono::duration<Rep, Period> duration)
 | 
						|
{
 | 
						|
    auto now = std::chrono::steady_clock::now();
 | 
						|
    const auto end = now + duration;
 | 
						|
    while (now < end)
 | 
						|
    {
 | 
						|
        co_await c_suspend();
 | 
						|
        now = std::chrono::steady_clock::now();
 | 
						|
    }
 | 
						|
    co_return;
 | 
						|
}
 | 
						|
} // namespace mijin
 | 
						|
 | 
						|
#endif // !defined(MIJIN_ASYNC_COROUTINE_SLEEP_HPP_INCLUDED)
 |