/*
  Copyright (c) 2008-2012  John Lee (j.y.lee@yeah.net)
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

  * Neither the name of the copyright holders nor the names of
    contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __LIGHTWEIGHT_OBJECT_ORENTED_KERNEL_H
#error "Include <look.h> instead of this file."
#else	// __LIGHTWEIGHT_OBJECT_ORENTED_KERNEL_H

#ifndef __LOOK_EDF_INTERFACE
#define __LOOK_EDF_INTERFACE

#pragma interface

#ifndef LOOK_SCHEDULING
#define LOOK_SCHEDULING	edf
#define LOOK_SCHEDULING_EDF
#endif	// LOOK_SCHEDULING

#include <co>

#define LOOK_SCHEDULING_PRIORITY

namespace look {
	namespace edf {
		using link::flag_t;

		class task_t : public co::task_t {
		public:
			__INLINE bool do_release(uintptr_t deadline);
			__INLINE static void rest();

		protected:
			__INLINE task_t(uintptr_t deadline);

		private:
			using co::task_t::wakeup;
			using co::task_t::do_wakeup;

		private:
			uintptr_t deadline;
		};

		class sched_t : public co::sched_t {
			friend class task_t;

		public:
			__INLINE sched_t();
			void create(task_t& task);

			__INLINE task_t& get_current_task();

		protected:					// virtual
			base::task_t* ready(sync_t& sync, base::task_t* task = 0);
			void block(sync_t& sync);

		private:
			bool do_release(task_t& task, uintptr_t deadline);
			using co::sched_t::yield;

		private:
			rest_site_t rest_site;
		};
	}
}
#else	// __LOOK_EDF_INTERFACE
#ifndef __LOOK_EDF_INLINE
#define __LOOK_EDF_INLINE

#include <co>

__INLINE look::edf::task_t::task_t(uintptr_t deadline)
{
	this->deadline = deadline;
}

__INLINE bool look::edf::task_t::do_release(uintptr_t deadline)
{
	return scheduler.do_release(*this, deadline);
}

__INLINE void look::edf::task_t::rest()
{
	scheduler.rest_site.rest();
}

__INLINE look::edf::sched_t::sched_t()
{
}

__INLINE look::edf::task_t& look::edf::sched_t::get_current_task()
{
	return static_cast<task_t&>(base::sched_t::get_current_task());
}
#endif	// __LOOK_EDF_INLINE
#endif	// __LOOK_EDF_INTERFACE
#endif	// __LIGHTWEIGHT_OBJECT_ORENTED_KERNEL_H
