?? scheduler.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_SCHEDULER_H#define DXR3PLAYER_SCHED_SCHEDULER_H//------------------------------------------------------------------------------#include "Schedulable.h"#include "util/DefaultAllocator.h"#include "util/FileDescriptor.h"#include "types.h"#include <vector>#include <set>#include <cassert>//------------------------------------------------------------------------------namespace sched {//------------------------------------------------------------------------------class Schedulable;class FDWaitCondition;//------------------------------------------------------------------------------/** * The scheduler. It is a singleton. Get the instance using Scheduler::get(). */class Scheduler{private: /** * The type for file descriptor-based wait conditions */ typedef std::set<FDWaitCondition*, std::less<FDWaitCondition*>, DefaultAllocator> fdWaitConditions_t; /** * The pair to store schedulables in. */ typedef std::pair<Schedulable*, bool> schedulableInfo_t; /** * Allocator for schedulables. */ typedef DefaultAllocatorTemplate<schedulableInfo_t> SchedulableAllocator; /** * The type for schedulables. */ typedef std::vector<schedulableInfo_t, SchedulableAllocator> schedulables_t;public: /** * Initialize the instance. */ static void initialize(size_t stackSize = 128 * 1024); /** * Get the instance. Can be called only after an initialize(). */ static Scheduler& get(); /** * Determine if the current thread is interrupted or not. */ static bool isInterrupted(); /** * Call continueBurst() on the current Schedulable * * @see Schedulable::continueBurst */ static void continueBurst(const char* ownerName); /** * Call yield() on the current Schedulable * * @see Schedulable::yield */ static void yield(const char* ownerName); /** * Call sleep() on the current Schedulable * * @see Schedulable::yield */ static void sleep(const char* ownerName, millis_t timeout = INVALID_MILLIS); /** * Call sleepInterruptible() on the current Schedulable * * @see Schedulable::yield */ static void sleepInterruptible(const char* ownerName, millis_t timeout = INVALID_MILLIS); /** * Call wait() on the current Schedulable * * @see Schedulable::wait */ static bool wait(WaitCondition& cond, millis_t timeout = INVALID_MILLIS); /** * Call waitInterruptible() on the current Schedulable * * @see Schedulable::wait */ static bool waitInterruptible(WaitCondition& cond, millis_t timeout = INVALID_MILLIS); private: /** * The only instance. */ static Scheduler* instance; /** * The Schedulable running currently. */ static Schedulable* currentSchedulable; /** * The top of the stack of the next Schedulable. */ char* nextStackTop; /** * The stack state of the scheduler. */ jmp_buf state; /** * The schedulables. */ schedulables_t schedulables; /** * The file descriptor-based wait conditions */ fdWaitConditions_t fdWaitConditions; /** * The start clock. */ long startClock; /** * Construct the scheduler. */ Scheduler(size_t stackSize);public: /** * Run the scheduler. */ void run(); /** * Print the status of the scheduler and the schedulables. */ void printStatus(); /** * Signal all schedulables to quit. */ void quit() const;private: /** * Add the given schedulable. * * @return the bottom of the next stack */ char* addSchedulable(Schedulable* schedulable, size_t stackSize); /** * Add the given file descriptor-based wait condition. */ void addFDWaitCondition(FDWaitCondition* cond); /** * Remove the given file descriptor-based wait condition. */ void removeFDWaitCondition(FDWaitCondition* cond); /** * Get the earliest time a schedulabe times out. * * @return the earliest time, or 0 if there is at least one * schedulable, that is runnable at once. */ millis_t getEarliestTime() const; /** * Get the file descriptors to watch. * * @return the largest file descriptor, or -1 if no file * descriptor should be watched. */ int getFileDescriptors(fd_set& readFDs, fd_set& writeFDs) const; /** * Set the file descriptor-based wait conditions for the file * descriptors that are available. */ void setFileDescriptors(const fd_set& readFDs, const fd_set& writeFDs) const; /** * Set the runnability of the schedulables. */ void setRunnability(); /** * Run the runnable schedulables. */ void runRunnables(); /** * Resume the scheduler. */ void resume(); /** * Run the schedulable. */ void runSchedulable(Schedulable* schedulable); friend class Schedulable; friend class FDWaitCondition;};//------------------------------------------------------------------------------// Inline definitions//------------------------------------------------------------------------------inline Scheduler& Scheduler::get(){ assert(instance!=0); return *instance;}//------------------------------------------------------------------------------inline bool Scheduler::isInterrupted(){ assert(currentSchedulable!=0); return currentSchedulable->isInterrupted();} //------------------------------------------------------------------------------inline void Scheduler::continueBurst(const char* ownerName){ assert(currentSchedulable!=0); currentSchedulable->continueBurst(ownerName);}//------------------------------------------------------------------------------inline void Scheduler::yield(const char* ownerName){ assert(currentSchedulable!=0); currentSchedulable->yield(ownerName);} //------------------------------------------------------------------------------inline void Scheduler::sleep(const char* ownerName, millis_t timeout){ assert(currentSchedulable!=0); currentSchedulable->sleep(ownerName, timeout);} //------------------------------------------------------------------------------inline void Scheduler::sleepInterruptible(const char* ownerName, millis_t timeout){ assert(currentSchedulable!=0); currentSchedulable->sleepInterruptible(ownerName, timeout);} //------------------------------------------------------------------------------inline bool Scheduler::wait(WaitCondition& cond, millis_t timeout){ assert(currentSchedulable!=0); return currentSchedulable->wait(cond, timeout);}//------------------------------------------------------------------------------inline bool Scheduler::waitInterruptible(WaitCondition& cond, millis_t timeout){ assert(currentSchedulable!=0); return currentSchedulable->waitInterruptible(cond, timeout);}//------------------------------------------------------------------------------inline void Scheduler::resume(){ longjmp(state, 0);}//------------------------------------------------------------------------------} /* namespace sched *///------------------------------------------------------------------------------#endif // DXR3PLAYER_SCHED_SCHEDULER_H// Local variables:// mode: c++// End:
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -