Leaked source code of windows server 2003
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.

162 lines
4.1 KiB

  1. #pragma once
  2. class CStressJobManager;
  3. class CBarrier
  4. {
  5. CEvent m_hBarrierEvent;
  6. LONG m_lBarrierSize;
  7. LONG m_lWaitingThreads;
  8. PRIVATIZE_COPY_CONSTRUCTORS(CBarrier)
  9. public:
  10. CBarrier() : m_lBarrierSize(0), m_lWaitingThreads(0) { }
  11. //
  12. // Start up, with the given barrier size and name (if any)
  13. //
  14. BOOL Initialize( DWORD lBarrierSize, PCWSTR pcwszBarrierName = NULL )
  15. {
  16. FN_PROLOG_WIN32
  17. m_lBarrierSize = lBarrierSize;
  18. m_lWaitingThreads = 0;
  19. IFW32FALSE_EXIT(m_hBarrierEvent.Win32CreateEvent(TRUE, FALSE, pcwszBarrierName));
  20. FN_EPILOG
  21. }
  22. //
  23. // This thread is to join the barrier. It's possible that this call will be
  24. // the one that "breaks" the barrier..
  25. //
  26. BOOL WaitForBarrier()
  27. {
  28. FN_PROLOG_WIN32
  29. LONG LatestValue = ::InterlockedIncrement(&m_lWaitingThreads);
  30. if ( LatestValue >= m_lBarrierSize )
  31. {
  32. IFW32FALSE_EXIT(::SetEvent(m_hBarrierEvent));
  33. }
  34. else
  35. {
  36. IFW32FALSE_EXIT(::WaitForSingleObject(m_hBarrierEvent, INFINITE) == WAIT_OBJECT_0);
  37. }
  38. FN_EPILOG
  39. }
  40. //
  41. // In case someone wants to wait on us without actually joining in to count
  42. // for breaking the barrier (why??)
  43. //
  44. BOOL WaitForBarrierNoJoin()
  45. {
  46. FN_PROLOG_WIN32
  47. IFW32FALSE_EXIT(::WaitForSingleObject(m_hBarrierEvent, INFINITE) == WAIT_OBJECT_0);
  48. FN_EPILOG
  49. }
  50. //
  51. // A thread has a reason to break the barrier early? Fine, let them.
  52. //
  53. BOOL EarlyRelease()
  54. {
  55. FN_PROLOG_WIN32
  56. IFW32FALSE_EXIT(::SetEvent(m_hBarrierEvent));
  57. FN_EPILOG
  58. }
  59. };
  60. class CStressJobEntry
  61. {
  62. DWORD InternalThreadProc();
  63. BOOL WaitForStartingGun();
  64. PRIVATIZE_COPY_CONSTRUCTORS(CStressJobEntry);
  65. public:
  66. CDequeLinkage m_dlLinkage;
  67. CThread m_hThread;
  68. bool m_fStop;
  69. ULONG m_ulRuns;
  70. ULONG m_ulFailures;
  71. DWORD m_dwSleepBetweenRuns;
  72. CStringBuffer m_buffTestName;
  73. CStringBuffer m_buffTestDirectory;
  74. CStressJobManager *m_pManager;
  75. //
  76. // Override these three to provide functionality
  77. //
  78. virtual BOOL RunTest( bool &rfTestPasses ) = 0;
  79. virtual BOOL SetupSelfForRun() = 0;
  80. virtual BOOL Cleanup();
  81. virtual BOOL LoadFromSettingsFile( PCWSTR pcwszSettingsFile );
  82. //
  83. // These are not to be overridden!
  84. //
  85. BOOL Stop( BOOL fWaitForCompletion = TRUE );
  86. BOOL WaitForCompletion();
  87. static DWORD ThreadProc( PVOID pv );
  88. CStressJobEntry( CStressJobManager *pManager );
  89. virtual ~CStressJobEntry();
  90. };
  91. typedef CDeque<CStressJobEntry, offsetof(CStressJobEntry, m_dlLinkage)> CStressEntryDeque;
  92. typedef CDequeIterator<CStressJobEntry, offsetof(CStressJobEntry, m_dlLinkage)> CStressEntryDequeIterator;
  93. class CStressJobManager
  94. {
  95. PRIVATIZE_COPY_CONSTRUCTORS(CStressJobManager);
  96. friend CStressJobEntry;
  97. CEvent m_hStartingGunEvent;
  98. ULONG m_ulThreadsCreated;
  99. ULONG m_ulThreadsReady;
  100. BOOL SignalAnotherJobReady();
  101. BOOL SignalThreadWorking();
  102. BOOL SignalThreadDone();
  103. BOOL WaitForStartEvent();
  104. public:
  105. CStressEntryDeque m_JobsListed;
  106. ULONG m_ulThreadsWorking;
  107. BOOL LoadFromDirectory( PCWSTR pcwszDirectoryName, PULONG pulJobsFound = NULL );
  108. BOOL CreateWorkerThreads( PULONG pulThreadsCreated = NULL );
  109. BOOL StopJobs( BOOL fWithWaitForComplete = TRUE );
  110. BOOL CleanupJobs();
  111. BOOL StartJobs();
  112. BOOL WaitForAllJobsComplete();
  113. //
  114. // This returns the directory name that the manager will use to find
  115. // data files/directories.
  116. //
  117. virtual PCWSTR GetGroupName() = 0;
  118. virtual PCWSTR GetIniFileName() = 0;
  119. CStressJobManager();
  120. ~CStressJobManager();
  121. protected:
  122. virtual BOOL CreateJobEntry( CStressJobEntry* &pJobEntry ) = 0;
  123. };