📄 schedulable.h
字号:
//// Copyright (c) 2003 by Istv醤 V醨adi//// This file is part of dxr3Player, a DVD player written specifically // for the DXR3 (aka Hollywood+) decoder card.// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA#ifndef DXR3PLAYER_SCHED_SCHEDULABLE_H#define DXR3PLAYER_SCHED_SCHEDULABLE_H//------------------------------------------------------------------------------#include "util/Log.h"#include "types.h"#include <cstdlib>#include <csetjmp>//------------------------------------------------------------------------------namespace sched {//------------------------------------------------------------------------------class WaitCondition;//------------------------------------------------------------------------------/** * Abstract base class for schedulables. */class Schedulable{public: /** * The default stack size. */ static const size_t defaultStackSize = 64 * 1024;private: /** * The name of the schedulable. */ const char* name; /** * The stack state. */ jmp_buf state; /** * The current wait condition. */ WaitCondition* waitCondition; /** * The wait timeout. */ millis_t waitTimeout; /** * Indicate if the schedulable is in an interruptible wait. */ bool waitIsInterruptible; /** * Indicate if an interrupt is pending. */ bool interruptPending; /** * Indicate if we should quit. It is set only if interruptPending * is set too. */ bool toQuit; /** * Maxilam burst size. */ size_t maxBurstSize; /** * Current burst size. */ size_t burstSize;public: /** * Construct the schedulable with the given stack size */ Schedulable(const char* name, size_t stackSize = defaultStackSize, size_t maxBurstSize = 0); /** * Virtual destructor. */ virtual ~Schedulable(); /** * Get the name. */ const char* getName() const; /** * Interrupt the schedulable. */ void interrupt(); /** * Check if we are interrupted. */ bool isInterrupted() const; /** * Quit as soon as possibile. */ void quit(); /** * Indicate if we should quit. */ bool shouldQuit() const; /** * The actual operation of the schedulable. */ virtual void run() = 0; /** * Print any relevant status information. The default * implementation does nothing. */ virtual void printStatus() const;protected: /** * Clear the interrupt. */ void clearInterrupt();private: /** * Continue the burst. This increments the burst size and if that * reaches the maximum, it will yield the processor. */ void continueBurst(const char* ownerName); /** * Yield the processor. The scheduler gets back the control, but * this schedulable will be scheduled again in the next cycle. */ void yield(const char* ownerName); /** * Sleep until the given timeout. */ void sleep(const char* ownerName, millis_t timeout = INVALID_MILLIS); /** * Sleep until the given timeout. The sleeping can be interrupted */ void sleepInterruptible(const char* ownerName, millis_t timeout = INVALID_MILLIS); /** * Wait on the given condition without being interruptible. * * @return true if the condition was fulfilled, false if a timeout * occured. An interrupt may be pending in any case. */ bool wait(WaitCondition& cond, millis_t timeout = INVALID_MILLIS); /** * Wait on the given condition. The waiting can be interrupted. * * @return true if the condition was fulfilled, false if a timeout * occured or the waiting was interrupted. An interrupt may still be * pending if the return value is true. */ bool waitInterruptible(WaitCondition& cond, millis_t timeout = INVALID_MILLIS); /** * Initialize the state of the schedulable. */ void initState(); /** * Determine if the schedulabe can be run. It is runnable, if the * condition is fulfilled or an interrupt is pending and we are in * an interruptible wait. */ bool isRunnable() const; /** * Determine if the schedulabe can be run. It is runnable, if the * condition is fulfilled or the timeout has been reached or an * interrupt is pending and we are in an interruptible wait. If * there is no waiting condition, the schedulable is not runnable. */ bool isRunnable(millis_t t) const; /** * Wrapper around the run() function that resumes the scheduler when * run() returns. */ void doRun(); /** * Suspend this schedulable, i.e. give control back to the * scheduler. */ void suspend(); /** * Resume this schedulable, i.e. give control to the last * suspend() call. */ void resume(); /** * The common wait function. It must be called with an initialized * condition. */ bool wait(WaitCondition& cond, millis_t timeout, bool interruptible, bool fulfilled = false); friend class Scheduler;};//------------------------------------------------------------------------------// Inline definitions//------------------------------------------------------------------------------inline Schedulable::~Schedulable(){}//------------------------------------------------------------------------------inline const char* Schedulable::getName() const{ return name;}//------------------------------------------------------------------------------inline void Schedulable::interrupt(){ interruptPending = true;}//------------------------------------------------------------------------------inline bool Schedulable::isInterrupted() const{ return interruptPending;}//------------------------------------------------------------------------------inline void Schedulable::clearInterrupt(){ interruptPending = false;}//------------------------------------------------------------------------------inline void Schedulable::quit(){ Log::debug("Schedulable::quit\n"); toQuit = true; interrupt();}//------------------------------------------------------------------------------inline bool Schedulable::shouldQuit() const{ return toQuit;}//------------------------------------------------------------------------------inline void Schedulable::continueBurst(const char* ownerName){ if (maxBurstSize==0) return; ++burstSize; if (burstSize>=maxBurstSize) { yield(ownerName); }}//------------------------------------------------------------------------------inline bool Schedulable::wait(WaitCondition& cond, millis_t timeout){ return wait(cond, timeout, false, false);}//------------------------------------------------------------------------------inline bool Schedulable::waitInterruptible(WaitCondition& cond, millis_t timeout){ return wait(cond, timeout, true, false);}//------------------------------------------------------------------------------} /* namespace sched *///------------------------------------------------------------------------------#endif // DXR3PLAYER_SCHED_SCHEDULABLE_H// Local variables:// mode: c++// End:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -