OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 #include "util/thread/worker_thread.h" | |
16 | |
17 #include "base/logging.h" | |
18 #include "util/synchronization/semaphore.h" | |
19 #include "util/thread/thread.h" | |
20 | |
21 namespace crashpad { | |
22 | |
23 namespace internal { | |
24 | |
25 class WorkerThreadImpl final : public Thread { | |
26 public: | |
27 WorkerThreadImpl(WorkerThread* self, double initial_work_delay) | |
28 : self_(self), | |
29 initial_work_delay_(initial_work_delay), | |
30 semaphore_(0) {} | |
31 ~WorkerThreadImpl() {} | |
32 | |
33 void ThreadMain() override { | |
34 if (initial_work_delay_ > 0) | |
35 semaphore_.TimedWait(initial_work_delay_); | |
36 | |
37 while (self_->running_) { | |
38 self_->delegate_->DoWork(self_); | |
39 semaphore_.TimedWait(self_->work_interval_); | |
40 } | |
41 } | |
42 | |
43 void SignalSemaphore() { | |
44 semaphore_.Signal(); | |
45 } | |
46 | |
47 private: | |
48 WorkerThread* self_; // Weak, owns this. | |
Mark Mentovai
2015/12/15 17:56:58
Sort for better packing: Semaphore, double, pointe
Robert Sesek
2015/12/23 19:22:32
Done.
| |
49 double initial_work_delay_; | |
50 Semaphore semaphore_; | |
Mark Mentovai
2015/12/15 17:56:58
Can we retain the comment we had before?
// TOD
Robert Sesek
2015/12/23 19:22:31
Done.
| |
51 }; | |
52 | |
53 } // namespace internal | |
54 | |
55 WorkerThread::WorkerThread(double work_interval, | |
56 WorkerThread::Delegate* delegate) | |
57 : work_interval_(work_interval), | |
58 delegate_(delegate), | |
59 impl_(), | |
60 running_(false) {} | |
61 | |
62 WorkerThread::~WorkerThread() {} | |
Mark Mentovai
2015/12/15 17:56:58
DCHECK(!running_); ?
Robert Sesek
2015/12/23 19:22:31
Done, but Thread::~Thread also DCHECKs this.
Mark Mentovai
2016/01/04 16:01:31
Robert Sesek wrote:
| |
63 | |
64 void WorkerThread::Start(double initial_work_delay) { | |
65 DCHECK(!impl_); | |
66 DCHECK(!running_); | |
67 | |
68 running_ = true; | |
69 impl_.reset(new internal::WorkerThreadImpl(this, initial_work_delay)); | |
70 impl_->Start(); | |
71 } | |
72 | |
73 void WorkerThread::Stop() { | |
74 DCHECK(running_); | |
75 DCHECK(impl_); | |
76 | |
77 if (!running_) | |
78 return; | |
79 | |
80 running_ = false; | |
81 | |
82 impl_->SignalSemaphore(); | |
83 impl_->Join(); | |
84 impl_.reset(); | |
85 } | |
86 | |
87 void WorkerThread::DoWorkNow() { | |
88 DCHECK(running_); | |
89 impl_->SignalSemaphore(); | |
90 } | |
91 | |
92 } // namespace crashpad | |
OLD | NEW |