OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/pending_task.h" | 11 #include "base/pending_task.h" |
| 12 #include "base/threading/thread_local.h" |
12 #include "base/tracked_objects.h" | 13 #include "base/tracked_objects.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
| 19 base::LazyInstance<ThreadLocalBoolean>::Leaky |
| 20 g_worker_pool_running_on_this_thread = LAZY_INSTANCE_INITIALIZER; |
| 21 |
18 DWORD CALLBACK WorkItemCallback(void* param) { | 22 DWORD CALLBACK WorkItemCallback(void* param) { |
19 PendingTask* pending_task = static_cast<PendingTask*>(param); | 23 PendingTask* pending_task = static_cast<PendingTask*>(param); |
20 TRACE_EVENT2("task", "WorkItemCallback::Run", | 24 TRACE_EVENT2("task", "WorkItemCallback::Run", |
21 "src_file", pending_task->posted_from.file_name(), | 25 "src_file", pending_task->posted_from.file_name(), |
22 "src_func", pending_task->posted_from.function_name()); | 26 "src_func", pending_task->posted_from.function_name()); |
23 | 27 |
24 tracked_objects::TrackedTime start_time = | 28 tracked_objects::TrackedTime start_time = |
25 tracked_objects::ThreadData::NowForStartOfRun(pending_task->birth_tally); | 29 tracked_objects::ThreadData::NowForStartOfRun(pending_task->birth_tally); |
26 | 30 |
| 31 g_worker_pool_running_on_this_thread.Get().Set(true); |
27 pending_task->task.Run(); | 32 pending_task->task.Run(); |
| 33 g_worker_pool_running_on_this_thread.Get().Set(false); |
28 | 34 |
29 tracked_objects::ThreadData::TallyRunOnWorkerThreadIfTracking( | 35 tracked_objects::ThreadData::TallyRunOnWorkerThreadIfTracking( |
30 pending_task->birth_tally, | 36 pending_task->birth_tally, |
31 tracked_objects::TrackedTime(pending_task->time_posted), start_time, | 37 tracked_objects::TrackedTime(pending_task->time_posted), start_time, |
32 tracked_objects::ThreadData::NowForEndOfRun()); | 38 tracked_objects::ThreadData::NowForEndOfRun()); |
33 | 39 |
34 delete pending_task; | 40 delete pending_task; |
35 return 0; | 41 return 0; |
36 } | 42 } |
37 | 43 |
38 // Takes ownership of |pending_task| | 44 // Takes ownership of |pending_task| |
39 bool PostTaskInternal(PendingTask* pending_task, bool task_is_slow) { | 45 bool PostTaskInternal(PendingTask* pending_task, bool task_is_slow) { |
40 ULONG flags = 0; | 46 ULONG flags = 0; |
41 if (task_is_slow) | 47 if (task_is_slow) |
42 flags |= WT_EXECUTELONGFUNCTION; | 48 flags |= WT_EXECUTELONGFUNCTION; |
43 | 49 |
44 if (!QueueUserWorkItem(WorkItemCallback, pending_task, flags)) { | 50 if (!QueueUserWorkItem(WorkItemCallback, pending_task, flags)) { |
45 DLOG(ERROR) << "QueueUserWorkItem failed: " << GetLastError(); | 51 DLOG(ERROR) << "QueueUserWorkItem failed: " << GetLastError(); |
46 delete pending_task; | 52 delete pending_task; |
47 return false; | 53 return false; |
48 } | 54 } |
49 | 55 |
50 return true; | 56 return true; |
51 } | 57 } |
52 | 58 |
53 } // namespace | 59 } // namespace |
54 | 60 |
| 61 // static |
55 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, | 62 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, |
56 const base::Closure& task, bool task_is_slow) { | 63 const base::Closure& task, bool task_is_slow) { |
57 PendingTask* pending_task = new PendingTask(from_here, task); | 64 PendingTask* pending_task = new PendingTask(from_here, task); |
58 return PostTaskInternal(pending_task, task_is_slow); | 65 return PostTaskInternal(pending_task, task_is_slow); |
59 } | 66 } |
60 | 67 |
| 68 // static |
| 69 bool WorkerPool::RunsTasksOnCurrentThread() { |
| 70 return g_worker_pool_running_on_this_thread.Get().Get(); |
| 71 } |
| 72 |
61 } // namespace base | 73 } // namespace base |
OLD | NEW |