#include <array>
#include <coroutine>
#include <mutex>
#include "thread_meta.h"
#include "spsc_cursor.h"
namespace co_context::detail {

	struct worker_meta final {
		// number of I/O tasks running inside io_uring
		int32_t requests_to_reap = 0;
		// if there is at least one entry to submit to io_uring
		uint32_t requests_to_submit = 0;

		std::array<std::coroutine_handle<>, swap_capacity> reap_swap;
		spsc_cursor<cur_t, swap_capacity, unsafe> reap_cur;

		void init();
		void deinit();

		void forward_lazy() noexcept;
		void forward_task(std::coroutine_handle<> handle) noexcept;

		[[nodiscard]]
		cur_t number_to_schedule() const noexcept {
			const auto& cur = this->reap_cur;
			return cur.size();
		}
		// if there is at least one task newly spawned or forwarded
		[[nodiscard]]
		bool has_task_ready() const noexcept {
			return !reap_cur.is_empty();
		}
		// Get a coroutine to run. Guarantee to be non-null.
		[[nodiscard]]
		std::coroutine_handle<> schedule() noexcept;
		void work_once();

		/**
		 * @brief poll the submission swap zone
		 */
		void poll_submission() noexcept;

		/**
		 * @brief poll the uring completion queue
		 *
		 * @return number of cqes handled by worker
		 */
		uint32_t poll_completion() noexcept;
	};
}