OLD | NEW |
(Empty) | |
| 1 #include "SkOnce.h" |
| 2 #include "SkRunnable.h" |
| 3 #include "SkThreadPool.h" |
| 4 #include "Test.h" |
| 5 #include "TestClassDef.h" |
| 6 |
| 7 DEF_SK_ONCE(add_five, int* x) { |
| 8 *x += 5; |
| 9 } |
| 10 |
| 11 DEF_TEST(SkOnce_Singlethreaded, r) { |
| 12 int x = 0; |
| 13 |
| 14 // No matter how many times we do this, x will be 5. |
| 15 SK_ONCE(add_five, &x); |
| 16 SK_ONCE(add_five, &x); |
| 17 SK_ONCE(add_five, &x); |
| 18 SK_ONCE(add_five, &x); |
| 19 SK_ONCE(add_five, &x); |
| 20 |
| 21 REPORTER_ASSERT(r, 5 == x); |
| 22 } |
| 23 |
| 24 |
| 25 DEF_SK_ONCE(add_six, int* x) { |
| 26 *x += 6; |
| 27 } |
| 28 |
| 29 DEF_TEST(SkOnce_Multithreaded, r) { |
| 30 const int kTasks = 16, kThreads = 4; |
| 31 |
| 32 // Make a bunch of tasks that will race to be the first to add six. |
| 33 struct : public SkRunnable { |
| 34 int* ptr; |
| 35 virtual void run() SK_OVERRIDE { |
| 36 SK_ONCE(add_six, ptr); |
| 37 } |
| 38 } racers[kTasks]; |
| 39 |
| 40 // Point them at x. |
| 41 int x = 0; |
| 42 for (int i = 0; i < kTasks; i++) { |
| 43 racers[i].ptr = &x; |
| 44 } |
| 45 |
| 46 // Let them race. |
| 47 SkAutoTDelete<SkThreadPool> pool(new SkThreadPool(kThreads)); |
| 48 for (int i = 0; i < kTasks; i++) { |
| 49 pool->add(&racers[i]); |
| 50 } |
| 51 pool.free(); // Blocks until all threads are done. |
| 52 |
| 53 // Only one should have done the +=. |
| 54 REPORTER_ASSERT(r, 6 == x); |
| 55 } |
OLD | NEW |