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 |
21 } // namespace | 51 } // namespace |
22 | 52 |
23 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, | 53 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, |
24 Task* task, bool task_is_slow) { | 54 Task* task, bool task_is_slow) { |
25 task->SetBirthPlace(from_here); | 55 PendingTask* pending_task = |
56 new PendingTask(from_here, | |
57 base::Bind(&subtle::TaskClosureAdapter::Run, | |
58 new subtle::TaskClosureAdapter(task))); | |
26 | 59 |
27 ULONG flags = 0; | 60 ULONG flags = 0; |
willchan no longer on Chromium
2011/07/10 16:47:33
Maybe rather than copy/pasting we should use a fun
awong
2011/07/14 23:03:30
You mean make PendingTask more global? I'm not su
willchan no longer on Chromium
2011/07/19 12:18:26
I mean taking lines 61-67 and pulling it out into
awong
2011/07/19 21:08:20
Ah. That makes more sense.
Done.
| |
28 if (task_is_slow) | 61 if (task_is_slow) |
29 flags |= WT_EXECUTELONGFUNCTION; | 62 flags |= WT_EXECUTELONGFUNCTION; |
30 | 63 |
31 if (!QueueUserWorkItem(WorkItemCallback, task, flags)) { | 64 if (!QueueUserWorkItem(WorkItemCallback, pending_task, flags)) { |
32 DLOG(ERROR) << "QueueUserWorkItem failed: " << GetLastError(); | 65 DLOG(ERROR) << "QueueUserWorkItem failed: " << GetLastError(); |
33 delete task; | 66 delete pending_task; |
34 return false; | 67 return false; |
35 } | 68 } |
36 | 69 |
70 return true; | |
71 } | |
72 | |
73 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, | |
74 const base::Closure& task, bool task_is_slow) { | |
75 PendingTask* pending_task = new PendingTask(from_here, task); | |
76 | |
77 ULONG flags = 0; | |
78 if (task_is_slow) | |
79 flags |= WT_EXECUTELONGFUNCTION; | |
80 | |
81 if (!QueueUserWorkItem(WorkItemCallback, pending_task, flags)) { | |
82 DLOG(ERROR) << "QueueUserWorkItem failed: " << GetLastError(); | |
83 delete pending_task; | |
84 return false; | |
85 } | |
86 | |
37 return true; | 87 return true; |
38 } | 88 } |
39 | 89 |
40 } // namespace base | 90 } // namespace base |
OLD | NEW |