OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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/worker_pool.h" | |
6 | |
7 #include "base/task.h" | |
8 | |
9 // TODO(dsh): Split this file into worker_pool_win.cc and worker_pool_posix.cc. | |
10 #if defined(OS_WIN) | |
11 | |
12 namespace { | |
13 | |
14 DWORD CALLBACK WorkItemCallback(void* param) { | |
15 Task* task = static_cast<Task*>(param); | |
16 task->Run(); | |
17 delete task; | |
18 return 0; | |
19 } | |
20 | |
21 } // namespace | |
22 | |
23 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, | |
24 Task* task, bool task_is_slow) { | |
25 task->SetBirthPlace(from_here); | |
26 | |
27 ULONG flags = 0; | |
28 if (task_is_slow) | |
29 flags |= WT_EXECUTELONGFUNCTION; | |
30 | |
31 if (!QueueUserWorkItem(WorkItemCallback, task, flags)) { | |
32 DLOG(ERROR) << "QueueUserWorkItem failed: " << GetLastError(); | |
33 delete task; | |
34 return false; | |
35 } | |
36 | |
37 return true; | |
38 } | |
39 | |
40 #elif defined(OS_LINUX) | |
41 | |
42 namespace { | |
43 | |
44 void* PThreadCallback(void* param) { | |
45 Task* task = static_cast<Task*>(param); | |
46 task->Run(); | |
47 delete task; | |
48 return 0; | |
49 } | |
50 | |
51 } // namespace | |
52 | |
53 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, | |
54 Task* task, bool task_is_slow) { | |
55 task->SetBirthPlace(from_here); | |
56 | |
57 pthread_t thread; | |
58 pthread_attr_t attr; | |
59 | |
60 // POSIX does not have a worker thread pool implementation. For now we just | |
61 // create a thread for each task, and ignore |task_is_slow|. | |
62 // TODO(dsh): Implement thread reuse. | |
63 pthread_attr_init(&attr); | |
64 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | |
65 | |
66 int err = pthread_create(&thread, &attr, PThreadCallback, task); | |
67 pthread_attr_destroy(&attr); | |
68 if (err) { | |
69 DLOG(ERROR) << "pthread_create failed: " << err; | |
70 delete task; | |
71 return false; | |
72 } | |
73 | |
74 return true; | |
75 } | |
76 | |
77 #endif // OS_LINUX | |
OLD | NEW |