• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "base/threading/worker_pool.h"
6 
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/debug/trace_event.h"
10 #include "base/logging.h"
11 #include "base/pending_task.h"
12 #include "base/threading/thread_local.h"
13 #include "base/tracked_objects.h"
14 
15 namespace base {
16 
17 namespace {
18 
19 base::LazyInstance<ThreadLocalBoolean>::Leaky
20     g_worker_pool_running_on_this_thread = LAZY_INSTANCE_INITIALIZER;
21 
WorkItemCallback(void * param)22 DWORD CALLBACK WorkItemCallback(void* param) {
23   PendingTask* pending_task = static_cast<PendingTask*>(param);
24   TRACE_EVENT2("toplevel", "WorkItemCallback::Run",
25                "src_file", pending_task->posted_from.file_name(),
26                "src_func", pending_task->posted_from.function_name());
27 
28   tracked_objects::ThreadData::PrepareForStartOfRun(pending_task->birth_tally);
29 
30   g_worker_pool_running_on_this_thread.Get().Set(true);
31 
32   tracked_objects::TaskStopwatch stopwatch;
33   pending_task->task.Run();
34   stopwatch.Stop();
35 
36   g_worker_pool_running_on_this_thread.Get().Set(false);
37 
38   tracked_objects::ThreadData::TallyRunOnWorkerThreadIfTracking(
39       pending_task->birth_tally,
40       tracked_objects::TrackedTime(pending_task->time_posted),
41       stopwatch);
42 
43   delete pending_task;
44   return 0;
45 }
46 
47 // Takes ownership of |pending_task|
PostTaskInternal(PendingTask * pending_task,bool task_is_slow)48 bool PostTaskInternal(PendingTask* pending_task, bool task_is_slow) {
49   ULONG flags = 0;
50   if (task_is_slow)
51     flags |= WT_EXECUTELONGFUNCTION;
52 
53   if (!QueueUserWorkItem(WorkItemCallback, pending_task, flags)) {
54     DPLOG(ERROR) << "QueueUserWorkItem failed";
55     delete pending_task;
56     return false;
57   }
58 
59   return true;
60 }
61 
62 }  // namespace
63 
64 // static
PostTask(const tracked_objects::Location & from_here,const base::Closure & task,bool task_is_slow)65 bool WorkerPool::PostTask(const tracked_objects::Location& from_here,
66                           const base::Closure& task, bool task_is_slow) {
67   PendingTask* pending_task = new PendingTask(from_here, task);
68   return PostTaskInternal(pending_task, task_is_slow);
69 }
70 
71 // static
RunsTasksOnCurrentThread()72 bool WorkerPool::RunsTasksOnCurrentThread() {
73   return g_worker_pool_running_on_this_thread.Get().Get();
74 }
75 
76 }  // namespace base
77