| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkRunnable.h" | 8 #include "SkRunnable.h" |
| 9 #include "SkThreadPool.h" | 9 #include "SkThreadPool.h" |
| 10 #include "SkThreadUtils.h" | 10 #include "SkThreadUtils.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 if (count < 0) count = num_cores(); | 32 if (count < 0) count = num_cores(); |
| 33 // Create count threads, all running SkThreadPool::Loop. | 33 // Create count threads, all running SkThreadPool::Loop. |
| 34 for (int i = 0; i < count; i++) { | 34 for (int i = 0; i < count; i++) { |
| 35 SkThread* thread = SkNEW_ARGS(SkThread, (&SkThreadPool::Loop, this)); | 35 SkThread* thread = SkNEW_ARGS(SkThread, (&SkThreadPool::Loop, this)); |
| 36 *fThreads.append() = thread; | 36 *fThreads.append() = thread; |
| 37 thread->start(); | 37 thread->start(); |
| 38 } | 38 } |
| 39 } | 39 } |
| 40 | 40 |
| 41 SkThreadPool::~SkThreadPool() { | 41 SkThreadPool::~SkThreadPool() { |
| 42 if (!fDone) { |
| 43 this->wait(); |
| 44 } |
| 45 } |
| 46 |
| 47 void SkThreadPool::wait() { |
| 42 fReady.lock(); | 48 fReady.lock(); |
| 43 fDone = true; | 49 fDone = true; |
| 44 fReady.broadcast(); | 50 fReady.broadcast(); |
| 45 fReady.unlock(); | 51 fReady.unlock(); |
| 46 | 52 |
| 47 // Wait for all threads to stop. | 53 // Wait for all threads to stop. |
| 48 for (int i = 0; i < fThreads.count(); i++) { | 54 for (int i = 0; i < fThreads.count(); i++) { |
| 49 fThreads[i]->join(); | 55 fThreads[i]->join(); |
| 50 SkDELETE(fThreads[i]); | 56 SkDELETE(fThreads[i]); |
| 51 } | 57 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 return; | 98 return; |
| 93 } | 99 } |
| 94 | 100 |
| 95 // If we don't have any threads, obligingly just run the thing now. | 101 // If we don't have any threads, obligingly just run the thing now. |
| 96 if (fThreads.isEmpty()) { | 102 if (fThreads.isEmpty()) { |
| 97 return r->run(); | 103 return r->run(); |
| 98 } | 104 } |
| 99 | 105 |
| 100 // We have some threads. Queue it up! | 106 // We have some threads. Queue it up! |
| 101 fReady.lock(); | 107 fReady.lock(); |
| 108 SkASSERT(!fDone); // We shouldn't be adding work to a pool that's shut down
. |
| 102 LinkedRunnable* linkedRunnable = SkNEW(LinkedRunnable); | 109 LinkedRunnable* linkedRunnable = SkNEW(LinkedRunnable); |
| 103 linkedRunnable->fRunnable = r; | 110 linkedRunnable->fRunnable = r; |
| 104 fQueue.addToHead(linkedRunnable); | 111 fQueue.addToHead(linkedRunnable); |
| 105 fReady.signal(); | 112 fReady.signal(); |
| 106 fReady.unlock(); | 113 fReady.unlock(); |
| 107 } | 114 } |
| OLD | NEW |