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
3 // found in the LICENSE file.
5 #include "base/threading/simple_thread.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/threading/platform_thread.h"
10 #include "base/threading/thread_restrictions.h"
14 SimpleThread::SimpleThread(const std::string
& name_prefix
)
15 : name_prefix_(name_prefix
), name_(name_prefix
),
16 thread_(), event_(true, false), tid_(0), joined_(false) {
19 SimpleThread::SimpleThread(const std::string
& name_prefix
,
20 const Options
& options
)
21 : name_prefix_(name_prefix
), name_(name_prefix
), options_(options
),
22 thread_(), event_(true, false), tid_(0), joined_(false) {
25 SimpleThread::~SimpleThread() {
26 DCHECK(HasBeenStarted()) << "SimpleThread was never started.";
27 DCHECK(HasBeenJoined()) << "SimpleThread destroyed without being Join()ed.";
30 void SimpleThread::Start() {
31 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times.";
33 if (options_
.priority() == ThreadPriority::NORMAL
) {
34 success
= PlatformThread::Create(options_
.stack_size(), this, &thread_
);
36 success
= PlatformThread::CreateWithPriority(options_
.stack_size(), this,
37 &thread_
, options_
.priority());
40 base::ThreadRestrictions::ScopedAllowWait allow_wait
;
41 event_
.Wait(); // Wait for the thread to complete initialization.
44 void SimpleThread::Join() {
45 DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread.";
46 DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times.";
47 PlatformThread::Join(thread_
);
51 bool SimpleThread::HasBeenStarted() {
52 base::ThreadRestrictions::ScopedAllowWait allow_wait
;
53 return event_
.IsSignaled();
56 void SimpleThread::ThreadMain() {
57 tid_
= PlatformThread::CurrentId();
58 // Construct our full name of the form "name_prefix_/TID".
60 name_
.append(IntToString(tid_
));
61 PlatformThread::SetName(name_
);
63 // We've initialized our new thread, signal that we're done to Start().
69 DelegateSimpleThread::DelegateSimpleThread(Delegate
* delegate
,
70 const std::string
& name_prefix
)
71 : SimpleThread(name_prefix
),
75 DelegateSimpleThread::DelegateSimpleThread(Delegate
* delegate
,
76 const std::string
& name_prefix
,
77 const Options
& options
)
78 : SimpleThread(name_prefix
, options
),
82 DelegateSimpleThread::~DelegateSimpleThread() {
85 void DelegateSimpleThread::Run() {
86 DCHECK(delegate_
) << "Tried to call Run without a delegate (called twice?)";
91 DelegateSimpleThreadPool::DelegateSimpleThreadPool(
92 const std::string
& name_prefix
,
94 : name_prefix_(name_prefix
),
95 num_threads_(num_threads
),
99 DelegateSimpleThreadPool::~DelegateSimpleThreadPool() {
100 DCHECK(threads_
.empty());
101 DCHECK(delegates_
.empty());
102 DCHECK(!dry_
.IsSignaled());
105 void DelegateSimpleThreadPool::Start() {
106 DCHECK(threads_
.empty()) << "Start() called with outstanding threads.";
107 for (int i
= 0; i
< num_threads_
; ++i
) {
108 DelegateSimpleThread
* thread
= new DelegateSimpleThread(this, name_prefix_
);
110 threads_
.push_back(thread
);
114 void DelegateSimpleThreadPool::JoinAll() {
115 DCHECK(!threads_
.empty()) << "JoinAll() called with no outstanding threads.";
117 // Tell all our threads to quit their worker loop.
118 AddWork(NULL
, num_threads_
);
120 // Join and destroy all the worker threads.
121 for (int i
= 0; i
< num_threads_
; ++i
) {
126 DCHECK(delegates_
.empty());
129 void DelegateSimpleThreadPool::AddWork(Delegate
* delegate
, int repeat_count
) {
130 AutoLock
locked(lock_
);
131 for (int i
= 0; i
< repeat_count
; ++i
)
132 delegates_
.push(delegate
);
133 // If we were empty, signal that we have work now.
134 if (!dry_
.IsSignaled())
138 void DelegateSimpleThreadPool::Run() {
139 Delegate
* work
= NULL
;
144 AutoLock
locked(lock_
);
145 if (!dry_
.IsSignaled())
148 DCHECK(!delegates_
.empty());
149 work
= delegates_
.front();
152 // Signal to any other threads that we're currently out of work.
153 if (delegates_
.empty())
157 // A NULL delegate pointer signals us to quit.