home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / h / vd2 / system / VDScheduler.h < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  3.9 KB  |  126 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    System library component
  3. //    Copyright (C) 1998-2004 Avery Lee, All Rights Reserved.
  4. //
  5. //    Beginning with 1.6.0, the VirtualDub system library is licensed
  6. //    differently than the remainder of VirtualDub.  This particular file is
  7. //    thus licensed as follows (the "zlib" license):
  8. //
  9. //    This software is provided 'as-is', without any express or implied
  10. //    warranty.  In no event will the authors be held liable for any
  11. //    damages arising from the use of this software.
  12. //
  13. //    Permission is granted to anyone to use this software for any purpose,
  14. //    including commercial applications, and to alter it and redistribute it
  15. //    freely, subject to the following restrictions:
  16. //
  17. //    1.    The origin of this software must not be misrepresented; you must
  18. //        not claim that you wrote the original software. If you use this
  19. //        software in a product, an acknowledgment in the product
  20. //        documentation would be appreciated but is not required.
  21. //    2.    Altered source versions must be plainly marked as such, and must
  22. //        not be misrepresented as being the original software.
  23. //    3.    This notice may not be removed or altered from any source
  24. //        distribution.
  25.  
  26. #ifndef f_VD2_SYSTEM_VDSCHEDULER_H
  27. #define f_VD2_SYSTEM_VDSCHEDULER_H
  28.  
  29. #include <vd2/system/vdstl.h>
  30. #include <vd2/system/thread.h>
  31. #include <vd2/system/error.h>
  32.  
  33. class VDSchedulerNode;
  34. class VDSchedulerSuspendNode;
  35. class VDSignal;
  36. class IVDAsyncErrorCallback;
  37.  
  38. class VDScheduler {
  39. public:
  40.     VDScheduler();
  41.     ~VDScheduler();
  42.  
  43.     void setSignal(VDSignal *);
  44.     VDSignal *getSignal() { return pWakeupSignal; }
  45.     void setSchedulerNode(VDSchedulerNode *pSchedulerNode);
  46.  
  47.     IVDAsyncErrorCallback *getErrorCallback() const { return mpErrorCB; }
  48.     void setErrorCallback(IVDAsyncErrorCallback *pCB) { mpErrorCB = pCB; }
  49.  
  50.     bool isShuttingDown() const { return mbExitThreads; }
  51.  
  52.     void BeginShutdown();                            ///< Start signaling scheduling threads to exit.
  53.  
  54.     bool Run();
  55.     bool IdleWait();                                ///< Wait because no nodes are ready. Returns false if a thread should exit immediately.
  56.     void Ping();                                    ///< Restart a scheduler thread.  This is required when a scheduler thread leaves.
  57.     void Lock();
  58.     void Unlock();
  59.     void Reschedule(VDSchedulerNode *);                ///< Move node to Ready if Waiting.
  60.     void RescheduleFast(VDSchedulerNode *);            ///< Same as Reschedule(), but assumes the scheduler is already locked.
  61.     void Add(VDSchedulerNode *pNode);                ///< Add node to scheduler.
  62.     void Remove(VDSchedulerNode *pNode);            ///< Remove node from scheduler.
  63.     void DumpStatus();
  64.  
  65. protected:
  66.     void Repost(VDSchedulerNode *, bool);
  67.  
  68.     VDCriticalSection csScheduler;
  69.     IVDAsyncErrorCallback    *mpErrorCB;
  70.     VDSignal *pWakeupSignal;
  71.     volatile bool    mbExitThreads;
  72.     VDSchedulerNode *pParentSchedulerNode;
  73.  
  74.     typedef vdlist<VDSchedulerNode> tNodeList;
  75.     tNodeList listWaiting, listReady;
  76.  
  77.     typedef vdlist<VDSchedulerSuspendNode> tSuspendList;
  78.     tSuspendList listSuspends;
  79. };
  80.  
  81. class VDSchedulerNode : public vdlist<VDSchedulerNode>::node {
  82. friend class VDScheduler;
  83. public:
  84.     int nPriority;
  85.  
  86.     VDSchedulerNode() : nPriority(0) {}
  87.  
  88.     virtual bool Service()=0;
  89.  
  90.     virtual void DumpStatus();
  91.  
  92.     void Reschedule() { pScheduler->Reschedule(this); }
  93.     void RemoveFromScheduler() { pScheduler->Remove(this); }
  94.  
  95. protected:
  96.     VDScheduler *pScheduler;
  97.     volatile bool bRunning;
  98.     volatile bool bReschedule;
  99.     volatile bool bReady;
  100.     volatile bool bCondemned;
  101. };
  102.  
  103. class VDSchedulerSuspendNode : public vdlist<VDSchedulerSuspendNode>::node {
  104. public:
  105.     VDSchedulerSuspendNode(VDSchedulerNode *pNode) : mpNode(pNode) {}
  106.  
  107.     VDSchedulerNode *mpNode;
  108.     VDSignal mSignal;
  109. };
  110.  
  111. class VDSchedulerThread : public VDThread {
  112. public:
  113.     VDSchedulerThread();
  114.     ~VDSchedulerThread();
  115.  
  116.     bool Start(VDScheduler *pScheduler);
  117.  
  118. protected:
  119.     void ThreadRun();
  120.  
  121.     VDScheduler *mpScheduler;
  122.     uint32 mAffinity;
  123. };
  124.  
  125. #endif
  126.