#include "co_context/detail/worker_meta.h"
namespace co_context::detail {
	thread_local thread_meta this_thread; // NOLINT(*global-variables)

	void worker_meta::init()
	{
		this_thread.worker = this;
	}

	void worker_meta::deinit()
	{
		this_thread.worker = nullptr;
	}

	void worker_meta::forward_lazy() noexcept
	{
		++requests_to_reap; // NOTE may required no reap or required multi-reap.
		++requests_to_submit;
	}
	void worker_meta::forward_task(std::coroutine_handle<> handle) noexcept
	{
		auto& cur = reap_cur;
		reap_swap[cur.tail()] = handle;
		cur.push();
	}

	std::coroutine_handle<> worker_meta::schedule() noexcept
	{
		auto& cur = this->reap_cur;
		std::coroutine_handle<> chosen_coro = reap_swap[cur.head()];
		//cur.pop();
		return chosen_coro;
	}
	void worker_meta::work_once()
	{
		const auto coro = this->schedule();
		coro.resume();
	}

	void worker_meta::poll_submission() noexcept
	{
		// submit sqes
		if (requests_to_submit) [[likely]] {
			bool will_wait = !has_task_ready();
			requests_to_submit = 0;
		}
	}

	uint32_t worker_meta::poll_completion() noexcept
	{
		return 0;
	}

}