[email protected] | 5846da0 | 2009-03-06 20:35:45 | [diff] [blame^] | 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 | namespace { |
| 10 | |
| 11 | void* PThreadCallback(void* param) { |
| 12 | Task* task = static_cast<Task*>(param); |
| 13 | task->Run(); |
| 14 | delete task; |
| 15 | return 0; |
| 16 | } |
| 17 | |
| 18 | } // namespace |
| 19 | |
| 20 | bool WorkerPool::PostTask(const tracked_objects::Location& from_here, |
| 21 | Task* task, bool task_is_slow) { |
| 22 | task->SetBirthPlace(from_here); |
| 23 | |
| 24 | pthread_t thread; |
| 25 | pthread_attr_t attr; |
| 26 | |
| 27 | // POSIX does not have a worker thread pool implementation. For now we just |
| 28 | // create a thread for each task, and ignore |task_is_slow|. |
| 29 | // TODO(dsh): Implement thread reuse. |
| 30 | pthread_attr_init(&attr); |
| 31 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 32 | |
| 33 | int err = pthread_create(&thread, &attr, PThreadCallback, task); |
| 34 | pthread_attr_destroy(&attr); |
| 35 | if (err) { |
| 36 | DLOG(ERROR) << "pthread_create failed: " << err; |
| 37 | delete task; |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | return true; |
| 42 | } |