/*
  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_MLQ_INTERFACE
#define __LOOK_MLQ_INTERFACE

#pragma interface

#ifndef LOOK_SCHEDULING
#define LOOK_SCHEDULING mlq
#define LOOK_SCHEDULING_MLQ
#endif	// LOOK_SCHEDULING

#define LOOK_SCHEDULING_PRIORITY

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

		class task_t : public link::task_t {
		public:
			__INLINE uint_fast8_t get_current_priority() const;
			__INLINE uint_fast8_t get_original_priority() const;
			__INLINE bool set_priority(uint_fast8_t priority);

		protected:
			__INLINE task_t(uint_fast8_t priority);

#ifndef __DOXYGEN__
		protected:
			struct {
				uint8_t current;
				uint8_t original;
			} priority;
			uint8_t mutex_count;
#endif	// __DOXYGEN__
		};

		class mutex_t : public sync_t {
		public:
			__INLINE mutex_t();
			__INLINE uintptr_t get_count() const;

#ifdef __ARM_ARCH_6M__
			__INLINE bool trylock();
#else	// __ARM_ARCH_6M__
			bool trylock();
#endif	// __ARM_ARCH_6M__

			__INLINE bool lock(uintptr_t timeout = 0);
			__INLINE bool unlock();

#ifndef __DOXYGEN__
		private:

#ifdef __ARM_ARCH_6M__
			bool do_trylock();
#endif
			bool do_lock();
			bool do_unlock();

		protected:
			task_t* owner;
			uintptr_t count;
#endif	// __DOXYGEN__

		};

		class cond_t : public sync_t {
		public:
			__INLINE cond_t();
			int wait(mutex_t& mutex, uintptr_t timeout = 0);
			__INLINE bool signal();
			__INLINE void broadcast();

			__INLINE bool do_signal();
			void do_broadcast();
		};

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

			__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:
			bool do_yield(uintptr_t sched);

		private:
			uintptr_t internal_data[33];
		};
	}
}
#else	// __LOOK_MLQ_INTERFACE
#ifndef __LOOK_MLQ_INLINE
#define __LOOK_MLQ_INLINE
__INLINE look::mlq::task_t::task_t(uint_fast8_t priority)
{
	uintptr_t* p = reinterpret_cast<uintptr_t*>(&this->priority.current);
	*p = priority | (priority << 8);
}

__INLINE uint_fast8_t look::mlq::task_t::get_current_priority() const
{
	return priority.current;
}

__INLINE uint_fast8_t look::mlq::task_t::get_original_priority() const
{
	return priority.original;
}

__INLINE bool look::mlq::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::mlq::mutex_t::mutex_t()
{
	owner = 0;
	count = 0;
}

__INLINE uintptr_t look::mlq::mutex_t::get_count() const
{
	return count;
}

#if defined(__ARM_ARCH_6M__)
__INLINE bool look::mlq::mutex_t::trylock()
{
	register auto _this asm ("a1") = this;
	return LOOK_SVC(0, bool, &mutex_t::do_trylock, "0" (_this));
}
#endif
__INLINE bool look::mlq::mutex_t::lock(uintptr_t timeout)
{
	register auto _this asm ("a1") = this;
	register auto _timeout asm ("a2") = timeout;
	return LOOK_SVC(1, bool, &mutex_t::do_lock, "0" (_this), "r" (_timeout));
}

__INLINE bool look::mlq::mutex_t::unlock()
{
	register auto _this asm ("a1") = this;
	return LOOK_SVC(0, bool, &mutex_t::do_unlock, "0" (_this));
}

__INLINE look::mlq::cond_t::cond_t()
{
}

__INLINE bool look::mlq::cond_t::signal()
{
	return wakeup(1) != 0;
}

__INLINE void look::mlq::cond_t::broadcast()
{
	register auto _this asm ("a1") = this;
	LOOK_SVC(0, bool, &cond_t::do_broadcast, "0" (_this));
}

__INLINE bool look::mlq::cond_t::do_signal()
{
	return do_wakeup(1) != 0;
}

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

__INLINE bool look::mlq::sched_t::yield()
{
	register auto _this asm ("a1") = this;
	register uintptr_t _data asm ("a2") = 255;
	return LOOK_SVC(0, bool, &sched_t::do_yield, "0" (_this), "r" (_data));
}

__INLINE look::mlq::task_t& look::mlq::sched_t::get_current_task()
{
	return static_cast<task_t&>(base::sched_t::get_current_task());
}
#endif	// __LOOK_MLQ_INLINE
#endif	// __LOOK_MLQ_INTERFACE
#endif	// __LIGHTWEIGHT_OBJECT_ORENTED_KERNEL_H
