OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/threading/worker_pool.h" | 5 #include "base/threading/worker_pool.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/task.h" | 9 #include "base/task.h" |
| 10 #include "base/tracked_objects.h" |
9 | 11 |
10 namespace base { | 12 namespace base { |
11 | 13 |
12 namespace { | 14 namespace { |
13 | 15 |
| 16 struct PendingTask { |
| 17 PendingTask( |
| 18 const tracked_objects::Location& posted_from, |
| 19 const base::Closure& task) |
| 20 : task(task) { |
| 21 #if defined(TRACK_ALL_TASK_OBJECTS) |
| 22 post_births = tracked_objects::ThreadData::TallyABirthIfActive(posted_from); |
| 23 time_posted = TimeTicks::Now(); |
| 24 #endif // defined(TRACK_ALL_TASK_OBJECTS) |
| 25 } |
| 26 |
| 27 #if defined(TRACK_ALL_TASK_OBJECTS) |
| 28 // Counter for location where the Closure was posted from. |
| 29 tracked_objects::Births* post_births; |
| 30 |
| 31 // Time the task was posted. |
| 32 TimeTicks time_posted; |
| 33 #endif // defined(TRACK_ALL_TASK_OBJECTS) |
| 34 |
| 35 // The task to run. |
| 36 base::Closure task; |
| 37 }; |
| 38 |
14 DWORD CALLBACK WorkItemCallback(void* param) { | 39 DWORD CALLBACK WorkItemCallback(void* param) { |
15 Task* task = static_cast<Task*>(param); | 40 PendingTask* pending_task = static_cast<PendingTask*>(param); |
16 task->Run(); | 41 pending_task->task.Run(); |
17 delete task; | 42 #if defined(TRACK_ALL_TASK_OBJECTS) |
| 43 tracked_objects::ThreadData::TallyADeathIfActive( |
| 44 pending_task->post_births, |
| 45 TimeTicks::Now() - pending_task->time_posted); |
| 46 #endif // defined(TRACK_ALL_TASK_OBJECTS) |
| 47 delete pending_task; |
18 return 0; | 48 return 0; |
19 } | 49 } |
20 | 50 |
| 51 // Takes ownership of |pending_task| |
| 52 bool PostTaskInternal(PendingTask* pending_task, bool task_is_slow) { |
| 53 ULONG flags = 0; |
| 54 if (task_is_slow) |
| 55 flags |= WT_EXECUTELONGFUNCTION; |
| 56 |
| 57 if (!QueueUserWorkItem(WorkItemCallback, pending_task, flags)) { |
| 58 DLOG(ERROR) << "QueueUserWorkItem failed: " << GetLastError(); |
| 59 delete pending_task; |
| 60 return false; |
| 61 } |
| 62 |
| 63 return true; |
| 64 } |
| 65 |
21 } // namespace | 66 } // namespace |
22 | 67 |
23 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, | 68 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, |
24 Task* task, bool task_is_slow) { | 69 Task* task, bool task_is_slow) { |
25 task->SetBirthPlace(from_here); | 70 PendingTask* pending_task = |
| 71 new PendingTask(from_here, |
| 72 base::Bind(&subtle::TaskClosureAdapter::Run, |
| 73 new subtle::TaskClosureAdapter(task))); |
| 74 return PostTaskInternal(pending_task, task_is_slow); |
| 75 } |
26 | 76 |
27 ULONG flags = 0; | 77 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, |
28 if (task_is_slow) | 78 const base::Closure& task, bool task_is_slow) { |
29 flags |= WT_EXECUTELONGFUNCTION; | 79 PendingTask* pending_task = new PendingTask(from_here, task); |
30 | 80 return PostTaskInternal(pending_task, task_is_slow); |
31 if (!QueueUserWorkItem(WorkItemCallback, task, flags)) { | |
32 DLOG(ERROR) << "QueueUserWorkItem failed: " << GetLastError(); | |
33 delete task; | |
34 return false; | |
35 } | |
36 | |
37 return true; | |
38 } | 81 } |
39 | 82 |
40 } // namespace base | 83 } // namespace base |
OLD | NEW |