Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
9.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  5. //
  6. // File: runobj.hxx
  7. //
  8. // Contents: CRun class definition.
  9. //
  10. // Classes: CRun
  11. //
  12. // Functions: None.
  13. //
  14. // History: 08-Sep-95 EricB Created.
  15. // 01-Dec-95 MarkBl Split from util.hxx.
  16. //
  17. //----------------------------------------------------------------------------
  18. #ifndef __RUNOBJ_HXX__
  19. #define __RUNOBJ_HXX__
  20. //#include "time.hxx"
  21. #undef SetFlag
  22. #undef ClearFlag
  23. #undef IsFlagSet
  24. //
  25. // Run processing flags: values 0x00010000 through 0x80000000 for run state:
  26. //
  27. #define RUN_STATUS_RUNNING 0x00010000
  28. #define RUN_STATUS_FINISHED 0x00020000
  29. #define RUN_STATUS_ABORTED 0x00040000
  30. #define RUN_STATUS_TIMED_OUT 0x00080000
  31. #define RUN_STATUS_CLOSE_PENDING 0x00100000
  32. #define RUN_STATUS_CLOSED 0x00200000 // (This is set, but not used yet)
  33. #define RUN_STATUS_RESTART_ON_IDLE_RESUME 0x00400000
  34. //+----------------------------------------------------------------------------
  35. //
  36. // Class: CRun
  37. //
  38. // Synopsis: Lightweight job object. A moniker of sorts to the real thing.
  39. // Represents one run instance of a job.
  40. //
  41. // CODEWORK: (AnirudhS 11/7/96) The inheritance relationship should be:
  42. // class CRun : protected CDLink (instead of public CDLink)
  43. // class CRunList : private CRun (instead of having a private CRun member)
  44. // This would assure us that only CRunList could call CRun's CDLink methods.
  45. //
  46. //-----------------------------------------------------------------------------
  47. class CRun : public CDLink
  48. {
  49. public:
  50. CRun(LPFILETIME pft, LPFILETIME pftDeadline, FILETIME ftKill,
  51. DWORD MaxRunTime, DWORD rgFlags, WORD wIdleWait,
  52. BOOL fKeepAfterRunning);
  53. CRun(DWORD MaxRunTime, DWORD rgFlags, FILETIME ftDeadline,
  54. BOOL fKeepAfterRunning);
  55. CRun(DWORD MaxRunTime, DWORD rgFlags, WORD wIdleWait,
  56. FILETIME ftDeadline, BOOL fKeepAfterRunning);
  57. CRun(CRun * pRun);
  58. CRun(void);
  59. ~CRun();
  60. HRESULT Initialize(LPCTSTR ptszName) {
  61. return(this->SetName(ptszName));
  62. }
  63. void GetSysTime(LPSYSTEMTIME pst) {FileTimeToSystemTime(&m_ft, pst);}
  64. void GetTime(LPFILETIME pft) {*pft = m_ft;}
  65. LPFILETIME GetTime(void) {return &m_ft;}
  66. LPFILETIME GetDeadline(void) {return &m_ftDeadline;}
  67. void RelaxDeadline(FILETIME ftDeadline)
  68. { if (CompareFileTime(&ftDeadline, &m_ftDeadline) > 0)
  69. m_ftDeadline = ftDeadline; }
  70. WORD GetWait(void) {return m_wIdleWait;}
  71. void ReduceWaitTo(WORD wIdleWait) { m_wIdleWait = min(m_wIdleWait, wIdleWait);}
  72. FILETIME GetKillTime() { return m_ftKill; }
  73. void AdvanceKillTime(FILETIME ftKill)
  74. { if (CompareFileTime(&ftKill, &m_ftKill) < 0)
  75. m_ftKill = ftKill; }
  76. void AdjustKillTimeByMaxRunTime(FILETIME ftNow);
  77. void SetMaxRunTime(DWORD dwMaxRunTime) {m_dwMaxRunTime = dwMaxRunTime;}
  78. DWORD GetMaxRunTime(void) {return(m_dwMaxRunTime);}
  79. CRun * Next(void) { return (CRun *)CDLink::Next(); }
  80. CRun * Prev(void) { return (CRun *)CDLink::Prev(); }
  81. BOOL IsNull(void) {return(m_ft.dwLowDateTime == 0 &&
  82. m_ft.dwHighDateTime == 0);};
  83. HRESULT SetName(LPCTSTR ptszName);
  84. LPTSTR GetName(void) { return m_ptszJobName; }
  85. void SetHandle(HANDLE hJob) {
  86. m_hJob = hJob;
  87. this->SetFlag(RUN_STATUS_RUNNING);
  88. }
  89. HANDLE GetHandle(void) const { return(m_hJob); }
  90. #if !defined(_CHICAGO_)
  91. void SetProfileHandles(HANDLE hUser, HANDLE hProfile) {
  92. m_hUserToken = hUser;
  93. m_hProfile = hProfile;
  94. }
  95. HRESULT SetDesktop( LPCTSTR ptszDesktop );
  96. LPTSTR GetDesktop( void ) {
  97. return m_ptszDesktop;
  98. }
  99. HRESULT SetStation( LPCTSTR ptszStation );
  100. LPTSTR GetStation( void ) {
  101. return m_ptszStation;
  102. }
  103. #endif // !defined(_CHICAGO_)
  104. void SetFlag(DWORD rgFlag) { m_rgFlags |= rgFlag; }
  105. void ClearFlag(DWORD rgFlag) { m_rgFlags &= ~rgFlag; }
  106. DWORD GetFlags(void) { return(m_rgFlags); }
  107. BOOL IsFlagSet(DWORD rgFlag) {return (m_rgFlags & rgFlag);}
  108. BOOL IsIdleTriggered() { return m_fKeepInList; }
  109. void SetProcessId(DWORD dwProcessId) { m_dwProcessId = dwProcessId; }
  110. DWORD GetProcessId(void) { return(m_dwProcessId); }
  111. private:
  112. FILETIME m_ft; // When this instance is to run
  113. FILETIME m_ftDeadline; // Latest time when this instance may be started
  114. FILETIME m_ftKill; // Time by which this instance must terminate
  115. // (see the detailed comment on this field in
  116. // procssr.cxx)
  117. LPTSTR m_ptszJobName; // Job name.
  118. HANDLE m_hJob; // The process handle when run
  119. DWORD m_dwProcessId; // The process ID when run
  120. #if !defined(_CHICAGO_)
  121. HANDLE m_hUserToken; // User token
  122. HANDLE m_hProfile; // User profile
  123. LPTSTR m_ptszDesktop; //Windows Desktop
  124. LPTSTR m_ptszStation; //WindowsStation
  125. #endif // !defined(_CHICAGO_)
  126. DWORD m_rgFlags; // Job Flags
  127. DWORD m_dwMaxRunTime; // The remaining time to wait for the job to
  128. // terminate (see the detailed comment on this
  129. // field in procssr.cxx)
  130. WORD m_wIdleWait; // The idle wait to determine when to run
  131. // idle tasks.
  132. BOOL m_fKeepInList; // Whether to keep this run in the idle list
  133. // after starting it (set for jobs with idle
  134. // triggers)
  135. public:
  136. BOOL m_fStarted; // Whether the run was started since the last
  137. // idle period began
  138. };
  139. //+----------------------------------------------------------------------------
  140. //
  141. // Run object list class
  142. //
  143. //-----------------------------------------------------------------------------
  144. class CRunList
  145. {
  146. public:
  147. CRunList();
  148. ~CRunList();
  149. void FreeList(void);
  150. CRun * GetFirstJob(void) {return m_RunHead.Next();}
  151. BOOL IsEmpty() {return m_RunHead.Next() == &m_RunHead;}
  152. //
  153. // The following members allow the list to be used as a simple, unsorted,
  154. // (doubly) linked list.
  155. //
  156. void Add(CRun * pRun);
  157. HRESULT AddCopy(CRun * pRun);
  158. protected:
  159. CRun m_RunHead;
  160. };
  161. class CTimeRunList : private CRunList
  162. {
  163. public:
  164. void FreeList(void) {CRunList::FreeList();}
  165. CRun * GetFirstJob(void) {return CRunList::GetFirstJob();}
  166. BOOL IsEmpty() {return CRunList::IsEmpty();}
  167. //
  168. // The following members allow the list to be used as a sorted queue:
  169. //
  170. HRESULT AddSorted(FILETIME ftRun, FILETIME ftDeadline,
  171. FILETIME ftKillTime,
  172. LPTSTR ptszJobName, DWORD dwJobFlags,
  173. DWORD dwMaxRunTime, WORD wIdleWait,
  174. WORD * pCount, WORD cLimit);
  175. CRun * Pop(void);
  176. HRESULT PeekHeadTime(LPFILETIME pft);
  177. HRESULT MakeSysTimeArray(LPSYSTEMTIME *, WORD *);
  178. };
  179. class CIdleRunList : private CRunList
  180. {
  181. public:
  182. void FreeList(void);
  183. CRun * GetFirstJob(void) {return CRunList::GetFirstJob();}
  184. //
  185. // The following members allow the list to be used as a sorted queue,
  186. // sorted on idle wait times. The member at the head of the queue has
  187. // the least idle wait time.
  188. //
  189. void AddSortedByIdleWait(CRun * pRun);
  190. WORD GetFirstWait(void);
  191. void MarkNoneStarted(void);
  192. void FreeExpiredOrRegenerated();
  193. };
  194. //+----------------------------------------------------------------------------
  195. //
  196. // Method: CRunList::CRunList
  197. //
  198. // Synopsis: Constructor
  199. //
  200. //-----------------------------------------------------------------------------
  201. inline CRunList::CRunList(void)
  202. {
  203. //
  204. // Set the head node to point to itself. Note that the head node uses
  205. // the default ctor which sets the FILETIME member to {0, 0}. This null
  206. // FILETIME will be used as a marker for the head of the doubly linked
  207. // list.
  208. //
  209. m_RunHead.SetNext(&m_RunHead);
  210. m_RunHead.SetPrev(&m_RunHead);
  211. }
  212. //+----------------------------------------------------------------------------
  213. //
  214. // Method: CRunList::CRunList
  215. //
  216. // Synopsis: Destructor
  217. //
  218. //-----------------------------------------------------------------------------
  219. inline CRunList::~CRunList(void)
  220. {
  221. FreeList();
  222. }
  223. //+----------------------------------------------------------------------------
  224. //
  225. // Member: CRunList::Add
  226. //
  227. // Synopsis: Add an element to the unsorted list.
  228. //
  229. //-----------------------------------------------------------------------------
  230. inline void
  231. CRunList::Add(CRun * pAdd)
  232. {
  233. pAdd->LinkAfter(&m_RunHead);
  234. }
  235. #endif // __RUNOBJ_HXX__