condition_variable_unittest.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // Multi-threaded tests of ConditionVariable class.
  5. #include <time.h>
  6. #include <algorithm>
  7. #include <vector>
  8. #include "butil/logging.h"
  9. #include "butil/memory/scoped_ptr.h"
  10. #include "butil/synchronization/condition_variable.h"
  11. #include "butil/synchronization/lock.h"
  12. #include "butil/synchronization/spin_wait.h"
  13. #include "butil/threading/platform_thread.h"
  14. #include "butil/threading/thread_collision_warner.h"
  15. #include "butil/time/time.h"
  16. #include <gtest/gtest.h>
  17. #include <gtest/gtest.h>
  18. namespace butil {
  19. namespace {
  20. //------------------------------------------------------------------------------
  21. // Define our test class, with several common variables.
  22. //------------------------------------------------------------------------------
  23. class ConditionVariableTest : public testing::Test {
  24. public:
  25. const TimeDelta kZeroMs;
  26. const TimeDelta kTenMs;
  27. const TimeDelta kThirtyMs;
  28. const TimeDelta kFortyFiveMs;
  29. const TimeDelta kSixtyMs;
  30. const TimeDelta kOneHundredMs;
  31. ConditionVariableTest()
  32. : kZeroMs(TimeDelta::FromMilliseconds(0)),
  33. kTenMs(TimeDelta::FromMilliseconds(10)),
  34. kThirtyMs(TimeDelta::FromMilliseconds(30)),
  35. kFortyFiveMs(TimeDelta::FromMilliseconds(45)),
  36. kSixtyMs(TimeDelta::FromMilliseconds(60)),
  37. kOneHundredMs(TimeDelta::FromMilliseconds(100)) {
  38. }
  39. };
  40. //------------------------------------------------------------------------------
  41. // Define a class that will control activities an several multi-threaded tests.
  42. // The general structure of multi-threaded tests is that a test case will
  43. // construct an instance of a WorkQueue. The WorkQueue will spin up some
  44. // threads and control them throughout their lifetime, as well as maintaining
  45. // a central repository of the work thread's activity. Finally, the WorkQueue
  46. // will command the the worker threads to terminate. At that point, the test
  47. // cases will validate that the WorkQueue has records showing that the desired
  48. // activities were performed.
  49. //------------------------------------------------------------------------------
  50. // Callers are responsible for synchronizing access to the following class.
  51. // The WorkQueue::lock_, as accessed via WorkQueue::lock(), should be used for
  52. // all synchronized access.
  53. class WorkQueue : public PlatformThread::Delegate {
  54. public:
  55. explicit WorkQueue(int thread_count);
  56. virtual ~WorkQueue();
  57. // PlatformThread::Delegate interface.
  58. virtual void ThreadMain() OVERRIDE;
  59. //----------------------------------------------------------------------------
  60. // Worker threads only call the following methods.
  61. // They should use the lock to get exclusive access.
  62. int GetThreadId(); // Get an ID assigned to a thread..
  63. bool EveryIdWasAllocated() const; // Indicates that all IDs were handed out.
  64. TimeDelta GetAnAssignment(int thread_id); // Get a work task duration.
  65. void WorkIsCompleted(int thread_id);
  66. int task_count() const;
  67. bool allow_help_requests() const; // Workers can signal more workers.
  68. bool shutdown() const; // Check if shutdown has been requested.
  69. void thread_shutting_down();
  70. //----------------------------------------------------------------------------
  71. // Worker threads can call them but not needed to acquire a lock.
  72. Lock* lock();
  73. ConditionVariable* work_is_available();
  74. ConditionVariable* all_threads_have_ids();
  75. ConditionVariable* no_more_tasks();
  76. //----------------------------------------------------------------------------
  77. // The rest of the methods are for use by the controlling master thread (the
  78. // test case code).
  79. void ResetHistory();
  80. int GetMinCompletionsByWorkerThread() const;
  81. int GetMaxCompletionsByWorkerThread() const;
  82. int GetNumThreadsTakingAssignments() const;
  83. int GetNumThreadsCompletingTasks() const;
  84. int GetNumberOfCompletedTasks() const;
  85. void SetWorkTime(TimeDelta delay);
  86. void SetTaskCount(int count);
  87. void SetAllowHelp(bool allow);
  88. // The following must be called without locking, and will spin wait until the
  89. // threads are all in a wait state.
  90. void SpinUntilAllThreadsAreWaiting();
  91. void SpinUntilTaskCountLessThan(int task_count);
  92. // Caller must acquire lock before calling.
  93. void SetShutdown();
  94. // Compares the |shutdown_task_count_| to the |thread_count| and returns true
  95. // if they are equal. This check will acquire the |lock_| so the caller
  96. // should not hold the lock when calling this method.
  97. bool ThreadSafeCheckShutdown(int thread_count);
  98. private:
  99. // Both worker threads and controller use the following to synchronize.
  100. Lock lock_;
  101. ConditionVariable work_is_available_; // To tell threads there is work.
  102. // Conditions to notify the controlling process (if it is interested).
  103. ConditionVariable all_threads_have_ids_; // All threads are running.
  104. ConditionVariable no_more_tasks_; // Task count is zero.
  105. const int thread_count_;
  106. int waiting_thread_count_;
  107. scoped_ptr<PlatformThreadHandle[]> thread_handles_;
  108. std::vector<int> assignment_history_; // Number of assignment per worker.
  109. std::vector<int> completion_history_; // Number of completions per worker.
  110. int thread_started_counter_; // Used to issue unique id to workers.
  111. int shutdown_task_count_; // Number of tasks told to shutdown
  112. int task_count_; // Number of assignment tasks waiting to be processed.
  113. TimeDelta worker_delay_; // Time each task takes to complete.
  114. bool allow_help_requests_; // Workers can signal more workers.
  115. bool shutdown_; // Set when threads need to terminate.
  116. DFAKE_MUTEX(locked_methods_);
  117. };
  118. //------------------------------------------------------------------------------
  119. // The next section contains the actual tests.
  120. //------------------------------------------------------------------------------
  121. TEST_F(ConditionVariableTest, StartupShutdownTest) {
  122. Lock lock;
  123. // First try trivial startup/shutdown.
  124. {
  125. ConditionVariable cv1(&lock);
  126. } // Call for cv1 destruction.
  127. // Exercise with at least a few waits.
  128. ConditionVariable cv(&lock);
  129. lock.Acquire();
  130. cv.TimedWait(kTenMs); // Wait for 10 ms.
  131. cv.TimedWait(kTenMs); // Wait for 10 ms.
  132. lock.Release();
  133. lock.Acquire();
  134. cv.TimedWait(kTenMs); // Wait for 10 ms.
  135. cv.TimedWait(kTenMs); // Wait for 10 ms.
  136. cv.TimedWait(kTenMs); // Wait for 10 ms.
  137. lock.Release();
  138. } // Call for cv destruction.
  139. TEST_F(ConditionVariableTest, TimeoutTest) {
  140. Lock lock;
  141. ConditionVariable cv(&lock);
  142. lock.Acquire();
  143. TimeTicks start = TimeTicks::Now();
  144. const TimeDelta WAIT_TIME = TimeDelta::FromMilliseconds(300);
  145. // Allow for clocking rate granularity.
  146. const TimeDelta FUDGE_TIME = TimeDelta::FromMilliseconds(50);
  147. cv.TimedWait(WAIT_TIME + FUDGE_TIME);
  148. TimeDelta duration = TimeTicks::Now() - start;
  149. // We can't use EXPECT_GE here as the TimeDelta class does not support the
  150. // required stream conversion.
  151. EXPECT_TRUE(duration >= WAIT_TIME);
  152. lock.Release();
  153. }
  154. #if defined(OS_POSIX)
  155. const int kDiscontinuitySeconds = 2;
  156. void ALLOW_UNUSED BackInTime(Lock* lock) {
  157. AutoLock auto_lock(*lock);
  158. timeval tv;
  159. gettimeofday(&tv, NULL);
  160. tv.tv_sec -= kDiscontinuitySeconds;
  161. settimeofday(&tv, NULL);
  162. }
  163. #endif
  164. // Suddenly got flaky on Win, see http://crbug.com/10607 (starting at
  165. // comment #15).
  166. #if defined(OS_WIN)
  167. #define MAYBE_MultiThreadConsumerTest DISABLED_MultiThreadConsumerTest
  168. #else
  169. #define MAYBE_MultiThreadConsumerTest MultiThreadConsumerTest
  170. #endif
  171. // Test serial task servicing, as well as two parallel task servicing methods.
  172. TEST_F(ConditionVariableTest, MAYBE_MultiThreadConsumerTest) {
  173. const int kThreadCount = 10;
  174. WorkQueue queue(kThreadCount); // Start the threads.
  175. const int kTaskCount = 10; // Number of tasks in each mini-test here.
  176. Time start_time; // Used to time task processing.
  177. {
  178. butil::AutoLock auto_lock(*queue.lock());
  179. while (!queue.EveryIdWasAllocated())
  180. queue.all_threads_have_ids()->Wait();
  181. }
  182. // If threads aren't in a wait state, they may start to gobble up tasks in
  183. // parallel, short-circuiting (breaking) this test.
  184. queue.SpinUntilAllThreadsAreWaiting();
  185. {
  186. // Since we have no tasks yet, all threads should be waiting by now.
  187. butil::AutoLock auto_lock(*queue.lock());
  188. EXPECT_EQ(0, queue.GetNumThreadsTakingAssignments());
  189. EXPECT_EQ(0, queue.GetNumThreadsCompletingTasks());
  190. EXPECT_EQ(0, queue.task_count());
  191. EXPECT_EQ(0, queue.GetMaxCompletionsByWorkerThread());
  192. EXPECT_EQ(0, queue.GetMinCompletionsByWorkerThread());
  193. EXPECT_EQ(0, queue.GetNumberOfCompletedTasks());
  194. // Set up to make each task include getting help from another worker, so
  195. // so that the work gets done in paralell.
  196. queue.ResetHistory();
  197. queue.SetTaskCount(kTaskCount);
  198. queue.SetWorkTime(kThirtyMs);
  199. queue.SetAllowHelp(true);
  200. start_time = Time::Now();
  201. }
  202. queue.work_is_available()->Signal(); // But each worker can signal another.
  203. // Wait till we at least start to handle tasks (and we're not all waiting).
  204. queue.SpinUntilTaskCountLessThan(kTaskCount);
  205. // Wait to allow the all workers to get done.
  206. queue.SpinUntilAllThreadsAreWaiting();
  207. {
  208. // Wait until all work tasks have at least been assigned.
  209. butil::AutoLock auto_lock(*queue.lock());
  210. while (queue.task_count())
  211. queue.no_more_tasks()->Wait();
  212. // To avoid racy assumptions, we'll just assert that at least 2 threads
  213. // did work. We know that the first worker should have gone to sleep, and
  214. // hence a second worker should have gotten an assignment.
  215. EXPECT_LE(2, queue.GetNumThreadsTakingAssignments());
  216. EXPECT_EQ(kTaskCount, queue.GetNumberOfCompletedTasks());
  217. // Try to ask all workers to help, and only a few will do the work.
  218. queue.ResetHistory();
  219. queue.SetTaskCount(3);
  220. queue.SetWorkTime(kThirtyMs);
  221. queue.SetAllowHelp(false);
  222. }
  223. queue.work_is_available()->Broadcast(); // Make them all try.
  224. // Wait till we at least start to handle tasks (and we're not all waiting).
  225. queue.SpinUntilTaskCountLessThan(3);
  226. // Wait to allow the 3 workers to get done.
  227. queue.SpinUntilAllThreadsAreWaiting();
  228. {
  229. butil::AutoLock auto_lock(*queue.lock());
  230. EXPECT_EQ(3, queue.GetNumThreadsTakingAssignments());
  231. EXPECT_EQ(3, queue.GetNumThreadsCompletingTasks());
  232. EXPECT_EQ(0, queue.task_count());
  233. EXPECT_EQ(1, queue.GetMaxCompletionsByWorkerThread());
  234. EXPECT_EQ(0, queue.GetMinCompletionsByWorkerThread());
  235. EXPECT_EQ(3, queue.GetNumberOfCompletedTasks());
  236. // Set up to make each task get help from another worker.
  237. queue.ResetHistory();
  238. queue.SetTaskCount(3);
  239. queue.SetWorkTime(kThirtyMs);
  240. queue.SetAllowHelp(true); // Allow (unnecessary) help requests.
  241. }
  242. queue.work_is_available()->Broadcast(); // Signal all threads.
  243. // Wait till we at least start to handle tasks (and we're not all waiting).
  244. queue.SpinUntilTaskCountLessThan(3);
  245. // Wait to allow the 3 workers to get done.
  246. queue.SpinUntilAllThreadsAreWaiting();
  247. {
  248. butil::AutoLock auto_lock(*queue.lock());
  249. EXPECT_EQ(3, queue.GetNumThreadsTakingAssignments());
  250. EXPECT_EQ(3, queue.GetNumThreadsCompletingTasks());
  251. EXPECT_EQ(0, queue.task_count());
  252. EXPECT_EQ(1, queue.GetMaxCompletionsByWorkerThread());
  253. EXPECT_EQ(0, queue.GetMinCompletionsByWorkerThread());
  254. EXPECT_EQ(3, queue.GetNumberOfCompletedTasks());
  255. // Set up to make each task get help from another worker.
  256. queue.ResetHistory();
  257. queue.SetTaskCount(20); // 2 tasks per thread.
  258. queue.SetWorkTime(kThirtyMs);
  259. queue.SetAllowHelp(true);
  260. }
  261. queue.work_is_available()->Signal(); // But each worker can signal another.
  262. // Wait till we at least start to handle tasks (and we're not all waiting).
  263. queue.SpinUntilTaskCountLessThan(20);
  264. // Wait to allow the 10 workers to get done.
  265. queue.SpinUntilAllThreadsAreWaiting(); // Should take about 60 ms.
  266. {
  267. butil::AutoLock auto_lock(*queue.lock());
  268. EXPECT_EQ(10, queue.GetNumThreadsTakingAssignments());
  269. EXPECT_EQ(10, queue.GetNumThreadsCompletingTasks());
  270. EXPECT_EQ(0, queue.task_count());
  271. EXPECT_EQ(20, queue.GetNumberOfCompletedTasks());
  272. // Same as last test, but with Broadcast().
  273. queue.ResetHistory();
  274. queue.SetTaskCount(20); // 2 tasks per thread.
  275. queue.SetWorkTime(kThirtyMs);
  276. queue.SetAllowHelp(true);
  277. }
  278. queue.work_is_available()->Broadcast();
  279. // Wait till we at least start to handle tasks (and we're not all waiting).
  280. queue.SpinUntilTaskCountLessThan(20);
  281. // Wait to allow the 10 workers to get done.
  282. queue.SpinUntilAllThreadsAreWaiting(); // Should take about 60 ms.
  283. {
  284. butil::AutoLock auto_lock(*queue.lock());
  285. EXPECT_EQ(10, queue.GetNumThreadsTakingAssignments());
  286. EXPECT_EQ(10, queue.GetNumThreadsCompletingTasks());
  287. EXPECT_EQ(0, queue.task_count());
  288. EXPECT_EQ(20, queue.GetNumberOfCompletedTasks());
  289. queue.SetShutdown();
  290. }
  291. queue.work_is_available()->Broadcast(); // Force check for shutdown.
  292. SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromMinutes(1),
  293. queue.ThreadSafeCheckShutdown(kThreadCount));
  294. }
  295. TEST_F(ConditionVariableTest, LargeFastTaskTest) {
  296. const int kThreadCount = 200;
  297. WorkQueue queue(kThreadCount); // Start the threads.
  298. Lock private_lock; // Used locally for master to wait.
  299. butil::AutoLock private_held_lock(private_lock);
  300. ConditionVariable private_cv(&private_lock);
  301. {
  302. butil::AutoLock auto_lock(*queue.lock());
  303. while (!queue.EveryIdWasAllocated())
  304. queue.all_threads_have_ids()->Wait();
  305. }
  306. // Wait a bit more to allow threads to reach their wait state.
  307. queue.SpinUntilAllThreadsAreWaiting();
  308. {
  309. // Since we have no tasks, all threads should be waiting by now.
  310. butil::AutoLock auto_lock(*queue.lock());
  311. EXPECT_EQ(0, queue.GetNumThreadsTakingAssignments());
  312. EXPECT_EQ(0, queue.GetNumThreadsCompletingTasks());
  313. EXPECT_EQ(0, queue.task_count());
  314. EXPECT_EQ(0, queue.GetMaxCompletionsByWorkerThread());
  315. EXPECT_EQ(0, queue.GetMinCompletionsByWorkerThread());
  316. EXPECT_EQ(0, queue.GetNumberOfCompletedTasks());
  317. // Set up to make all workers do (an average of) 20 tasks.
  318. queue.ResetHistory();
  319. queue.SetTaskCount(20 * kThreadCount);
  320. queue.SetWorkTime(kFortyFiveMs);
  321. queue.SetAllowHelp(false);
  322. }
  323. queue.work_is_available()->Broadcast(); // Start up all threads.
  324. // Wait until we've handed out all tasks.
  325. {
  326. butil::AutoLock auto_lock(*queue.lock());
  327. while (queue.task_count() != 0)
  328. queue.no_more_tasks()->Wait();
  329. }
  330. // Wait till the last of the tasks complete.
  331. queue.SpinUntilAllThreadsAreWaiting();
  332. {
  333. // With Broadcast(), every thread should have participated.
  334. // but with racing.. they may not all have done equal numbers of tasks.
  335. butil::AutoLock auto_lock(*queue.lock());
  336. EXPECT_EQ(kThreadCount, queue.GetNumThreadsTakingAssignments());
  337. EXPECT_EQ(kThreadCount, queue.GetNumThreadsCompletingTasks());
  338. EXPECT_EQ(0, queue.task_count());
  339. EXPECT_LE(20, queue.GetMaxCompletionsByWorkerThread());
  340. EXPECT_EQ(20 * kThreadCount, queue.GetNumberOfCompletedTasks());
  341. // Set up to make all workers do (an average of) 4 tasks.
  342. queue.ResetHistory();
  343. queue.SetTaskCount(kThreadCount * 4);
  344. queue.SetWorkTime(kFortyFiveMs);
  345. queue.SetAllowHelp(true); // Might outperform Broadcast().
  346. }
  347. queue.work_is_available()->Signal(); // Start up one thread.
  348. // Wait until we've handed out all tasks
  349. {
  350. butil::AutoLock auto_lock(*queue.lock());
  351. while (queue.task_count() != 0)
  352. queue.no_more_tasks()->Wait();
  353. }
  354. // Wait till the last of the tasks complete.
  355. queue.SpinUntilAllThreadsAreWaiting();
  356. {
  357. // With Signal(), every thread should have participated.
  358. // but with racing.. they may not all have done four tasks.
  359. butil::AutoLock auto_lock(*queue.lock());
  360. EXPECT_EQ(kThreadCount, queue.GetNumThreadsTakingAssignments());
  361. EXPECT_EQ(kThreadCount, queue.GetNumThreadsCompletingTasks());
  362. EXPECT_EQ(0, queue.task_count());
  363. EXPECT_LE(4, queue.GetMaxCompletionsByWorkerThread());
  364. EXPECT_EQ(4 * kThreadCount, queue.GetNumberOfCompletedTasks());
  365. queue.SetShutdown();
  366. }
  367. queue.work_is_available()->Broadcast(); // Force check for shutdown.
  368. // Wait for shutdowns to complete.
  369. SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromMinutes(1),
  370. queue.ThreadSafeCheckShutdown(kThreadCount));
  371. }
  372. //------------------------------------------------------------------------------
  373. // Finally we provide the implementation for the methods in the WorkQueue class.
  374. //------------------------------------------------------------------------------
  375. WorkQueue::WorkQueue(int thread_count)
  376. : lock_(),
  377. work_is_available_(&lock_),
  378. all_threads_have_ids_(&lock_),
  379. no_more_tasks_(&lock_),
  380. thread_count_(thread_count),
  381. waiting_thread_count_(0),
  382. thread_handles_(new PlatformThreadHandle[thread_count]),
  383. assignment_history_(thread_count),
  384. completion_history_(thread_count),
  385. thread_started_counter_(0),
  386. shutdown_task_count_(0),
  387. task_count_(0),
  388. allow_help_requests_(false),
  389. shutdown_(false) {
  390. EXPECT_GE(thread_count_, 1);
  391. ResetHistory();
  392. SetTaskCount(0);
  393. SetWorkTime(TimeDelta::FromMilliseconds(30));
  394. for (int i = 0; i < thread_count_; ++i) {
  395. PlatformThreadHandle pth;
  396. EXPECT_TRUE(PlatformThread::Create(0, this, &pth));
  397. thread_handles_[i] = pth;
  398. }
  399. }
  400. WorkQueue::~WorkQueue() {
  401. {
  402. butil::AutoLock auto_lock(lock_);
  403. SetShutdown();
  404. }
  405. work_is_available_.Broadcast(); // Tell them all to terminate.
  406. for (int i = 0; i < thread_count_; ++i) {
  407. PlatformThread::Join(thread_handles_[i]);
  408. }
  409. EXPECT_EQ(0, waiting_thread_count_);
  410. }
  411. int WorkQueue::GetThreadId() {
  412. DFAKE_SCOPED_RECURSIVE_LOCK(locked_methods_);
  413. DCHECK(!EveryIdWasAllocated());
  414. return thread_started_counter_++; // Give out Unique IDs.
  415. }
  416. bool WorkQueue::EveryIdWasAllocated() const {
  417. DFAKE_SCOPED_RECURSIVE_LOCK(locked_methods_);
  418. return thread_count_ == thread_started_counter_;
  419. }
  420. TimeDelta WorkQueue::GetAnAssignment(int thread_id) {
  421. DFAKE_SCOPED_RECURSIVE_LOCK(locked_methods_);
  422. DCHECK_LT(0, task_count_);
  423. assignment_history_[thread_id]++;
  424. if (0 == --task_count_) {
  425. no_more_tasks_.Signal();
  426. }
  427. return worker_delay_;
  428. }
  429. void WorkQueue::WorkIsCompleted(int thread_id) {
  430. DFAKE_SCOPED_RECURSIVE_LOCK(locked_methods_);
  431. completion_history_[thread_id]++;
  432. }
  433. int WorkQueue::task_count() const {
  434. DFAKE_SCOPED_RECURSIVE_LOCK(locked_methods_);
  435. return task_count_;
  436. }
  437. bool WorkQueue::allow_help_requests() const {
  438. DFAKE_SCOPED_RECURSIVE_LOCK(locked_methods_);
  439. return allow_help_requests_;
  440. }
  441. bool WorkQueue::shutdown() const {
  442. lock_.AssertAcquired();
  443. DFAKE_SCOPED_RECURSIVE_LOCK(locked_methods_);
  444. return shutdown_;
  445. }
  446. // Because this method is called from the test's main thread we need to actually
  447. // take the lock. Threads will call the thread_shutting_down() method with the
  448. // lock already acquired.
  449. bool WorkQueue::ThreadSafeCheckShutdown(int thread_count) {
  450. bool all_shutdown;
  451. butil::AutoLock auto_lock(lock_);
  452. {
  453. // Declare in scope so DFAKE is guranteed to be destroyed before AutoLock.
  454. DFAKE_SCOPED_RECURSIVE_LOCK(locked_methods_);
  455. all_shutdown = (shutdown_task_count_ == thread_count);
  456. }
  457. return all_shutdown;
  458. }
  459. void WorkQueue::thread_shutting_down() {
  460. lock_.AssertAcquired();
  461. DFAKE_SCOPED_RECURSIVE_LOCK(locked_methods_);
  462. shutdown_task_count_++;
  463. }
  464. Lock* WorkQueue::lock() {
  465. return &lock_;
  466. }
  467. ConditionVariable* WorkQueue::work_is_available() {
  468. return &work_is_available_;
  469. }
  470. ConditionVariable* WorkQueue::all_threads_have_ids() {
  471. return &all_threads_have_ids_;
  472. }
  473. ConditionVariable* WorkQueue::no_more_tasks() {
  474. return &no_more_tasks_;
  475. }
  476. void WorkQueue::ResetHistory() {
  477. for (int i = 0; i < thread_count_; ++i) {
  478. assignment_history_[i] = 0;
  479. completion_history_[i] = 0;
  480. }
  481. }
  482. int WorkQueue::GetMinCompletionsByWorkerThread() const {
  483. int minumum = completion_history_[0];
  484. for (int i = 0; i < thread_count_; ++i)
  485. minumum = std::min(minumum, completion_history_[i]);
  486. return minumum;
  487. }
  488. int WorkQueue::GetMaxCompletionsByWorkerThread() const {
  489. int maximum = completion_history_[0];
  490. for (int i = 0; i < thread_count_; ++i)
  491. maximum = std::max(maximum, completion_history_[i]);
  492. return maximum;
  493. }
  494. int WorkQueue::GetNumThreadsTakingAssignments() const {
  495. int count = 0;
  496. for (int i = 0; i < thread_count_; ++i)
  497. if (assignment_history_[i])
  498. count++;
  499. return count;
  500. }
  501. int WorkQueue::GetNumThreadsCompletingTasks() const {
  502. int count = 0;
  503. for (int i = 0; i < thread_count_; ++i)
  504. if (completion_history_[i])
  505. count++;
  506. return count;
  507. }
  508. int WorkQueue::GetNumberOfCompletedTasks() const {
  509. int total = 0;
  510. for (int i = 0; i < thread_count_; ++i)
  511. total += completion_history_[i];
  512. return total;
  513. }
  514. void WorkQueue::SetWorkTime(TimeDelta delay) {
  515. worker_delay_ = delay;
  516. }
  517. void WorkQueue::SetTaskCount(int count) {
  518. task_count_ = count;
  519. }
  520. void WorkQueue::SetAllowHelp(bool allow) {
  521. allow_help_requests_ = allow;
  522. }
  523. void WorkQueue::SetShutdown() {
  524. lock_.AssertAcquired();
  525. shutdown_ = true;
  526. }
  527. void WorkQueue::SpinUntilAllThreadsAreWaiting() {
  528. while (true) {
  529. {
  530. butil::AutoLock auto_lock(lock_);
  531. if (waiting_thread_count_ == thread_count_)
  532. break;
  533. }
  534. PlatformThread::Sleep(TimeDelta::FromMilliseconds(30));
  535. }
  536. }
  537. void WorkQueue::SpinUntilTaskCountLessThan(int task_count) {
  538. while (true) {
  539. {
  540. butil::AutoLock auto_lock(lock_);
  541. if (task_count_ < task_count)
  542. break;
  543. }
  544. PlatformThread::Sleep(TimeDelta::FromMilliseconds(30));
  545. }
  546. }
  547. //------------------------------------------------------------------------------
  548. // Define the standard worker task. Several tests will spin out many of these
  549. // threads.
  550. //------------------------------------------------------------------------------
  551. // The multithread tests involve several threads with a task to perform as
  552. // directed by an instance of the class WorkQueue.
  553. // The task is to:
  554. // a) Check to see if there are more tasks (there is a task counter).
  555. // a1) Wait on condition variable if there are no tasks currently.
  556. // b) Call a function to see what should be done.
  557. // c) Do some computation based on the number of milliseconds returned in (b).
  558. // d) go back to (a).
  559. // WorkQueue::ThreadMain() implements the above task for all threads.
  560. // It calls the controlling object to tell the creator about progress, and to
  561. // ask about tasks.
  562. void WorkQueue::ThreadMain() {
  563. int thread_id;
  564. {
  565. butil::AutoLock auto_lock(lock_);
  566. thread_id = GetThreadId();
  567. if (EveryIdWasAllocated())
  568. all_threads_have_ids()->Signal(); // Tell creator we're ready.
  569. }
  570. Lock private_lock; // Used to waste time on "our work".
  571. while (1) { // This is the main consumer loop.
  572. TimeDelta work_time;
  573. bool could_use_help;
  574. {
  575. butil::AutoLock auto_lock(lock_);
  576. while (0 == task_count() && !shutdown()) {
  577. ++waiting_thread_count_;
  578. work_is_available()->Wait();
  579. --waiting_thread_count_;
  580. }
  581. if (shutdown()) {
  582. // Ack the notification of a shutdown message back to the controller.
  583. thread_shutting_down();
  584. return; // Terminate.
  585. }
  586. // Get our task duration from the queue.
  587. work_time = GetAnAssignment(thread_id);
  588. could_use_help = (task_count() > 0) && allow_help_requests();
  589. } // Release lock
  590. // Do work (outside of locked region.
  591. if (could_use_help)
  592. work_is_available()->Signal(); // Get help from other threads.
  593. if (work_time > TimeDelta::FromMilliseconds(0)) {
  594. // We could just sleep(), but we'll instead further exercise the
  595. // condition variable class, and do a timed wait.
  596. butil::AutoLock auto_lock(private_lock);
  597. ConditionVariable private_cv(&private_lock);
  598. private_cv.TimedWait(work_time); // Unsynchronized waiting.
  599. }
  600. {
  601. butil::AutoLock auto_lock(lock_);
  602. // Send notification that we completed our "work."
  603. WorkIsCompleted(thread_id);
  604. }
  605. }
  606. }
  607. } // namespace
  608. } // namespace butil