/*
  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_FP_INTERFACE
#define __LOOK_FP_INTERFACE

#pragma interface

#ifndef LOOK_SCHEDULING
#define LOOK_SCHEDULING fp
#define LOOK_SCHEDULING_FP
#endif	// LOOK_SCHEDULING

#define LOOK_SCHEDULING_PRIORITY

namespace look {
	namespace fp {
		class flag_t : public base::flag_t {
		public:
			__INLINE flag_t(uintptr_t flags = 0);
			__INLINE void set_bits(uintptr_t pattern);
			void do_set_bits(uintptr_t pattern);
		};

		class task_t : public base::task_t {
		public:
			__INLINE bool set_priority(uint_fast8_t priority);

		protected:
			__INLINE task_t(uint_fast8_t priority);

		protected:
			uint8_t priority;
		};

		class sched_t : public base::sched_t {
		public:
			__INLINE sched_t();
			void create(task_t& task);
			bool do_set_priority(task_t& task, uint_fast8_t priority);

			__INLINE task_t& get_current_task();

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

		protected:
			uintptr_t internal_data[33];
		};
	}
}
#else	// __LOOK_FP_INTERFACE
#ifndef __LOOK_FP_INLINE
#define __LOOK_FP_INLINE
__INLINE look::fp::flag_t::flag_t(uintptr_t flags)
: base::flag_t(flags)
{
}

__INLINE void look::fp::flag_t::set_bits(uintptr_t pattern)
{
	register auto _this asm ("a1") = this;
	register auto _pattern asm ("a2") = pattern;
	LOOK_SVC(0, uintptr_t, &flag_t::do_set_bits, "0" (_this), "r" (_pattern));
}

__INLINE look::fp::task_t::task_t(uint_fast8_t priority)
{
	this->priority = priority;
}

__INLINE bool look::fp::task_t::set_priority(uint_fast8_t priority)
{
	register auto _scheduler asm ("a1") = &scheduler;
	register auto _task asm ("a2") = this;
	register auto _prio asm ("a3") = priority;
	return LOOK_SVC(0, bool, &sched_t::do_set_priority, "0" (_scheduler), "r" (_task), "r" (_prio));
}

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

__INLINE look::fp::task_t& look::fp::sched_t::get_current_task()
{
	return static_cast<task_t&>(base::sched_t::get_current_task());
}
#endif	// __LOOK_FP_INLINE
#endif	// __LOOK_FP_INTERFACE
#endif	// __LIGHTWEIGHT_OBJECT_ORENTED_KERNEL_H
