#ifndef __KERNEL_H__
#define __KERNEL_H__

#include "types.h"
#include "rtos_defs.h"
#include "thread.h"

extern "C" void SysTick_Handler(void);
extern "C" StackItem* OS_ContextSwitchHook(StackItem* sp);

class Kernel
{
	friend class TISRW;

	public:
		Kernel() {}

		static void					Run(uint32_t timerFrequencyInHz);
		static uint32_t				GetTickCount(void);
		static void					RegisterThread(BaseThread* const p, uint32_t priority);
		static void					Scheduler(void);
		static void 				ReadyThread(const uint32_t pr);
		static void 				BlockThread(const uint32_t pr);
		static BaseThread* 			Running(void);
		static void 				Tick(void);
		static void 				Sched(void);
		static void 				SchedISR(void);
		static volatile bool 		IsContextSwitchDone(void);
		static void 				PendSVC_Handler(void);
		static StackItem* 			ContextSwitchHook(StackItem* sp);

	private:
		static uint32_t 			getHighPriority(ProcessMap pm);
		static ProcessMap			getPrioTag(const uint32_t pr);
		static void 				setPrioTag(ProcessMap& pm, const ProcessMap prioTag);
		static void 				clrPrioTag(ProcessMap& pm, const ProcessMap prioTag);

	private:
		static ProcessMap 			_readyProcessMap;
		static BaseThread* 			_processTable[ThreadCount + 1];
		static volatile uint32_t	_curProcPriority;
		static volatile uint32_t 	_schedProcPriority;
		static volatile uint32_t	_sysTickCount;
		static volatile uint32_t 	_ISR_NestCount;
		static StackItem*			_sp;
};



#endif // __KERNEL_H__
