OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 #ifndef CHROME_BROWSER_SYNC_NOTIFIER_BASE_SIGNAL_THREAD_TASK_H_ | |
6 #define CHROME_BROWSER_SYNC_NOTIFIER_BASE_SIGNAL_THREAD_TASK_H_ | |
7 | |
8 #include "talk/base/common.h" | |
9 #include "talk/base/signalthread.h" | |
10 #include "talk/base/sigslot.h" | |
11 #include "talk/base/task.h" | |
12 | |
13 namespace notifier { | |
14 | |
15 template<class T> | |
16 class SignalThreadTask : public talk_base::Task, | |
17 public sigslot::has_slots<> { | |
18 public: | |
19 // Takes ownership of signal_thread. | |
20 SignalThreadTask(talk_base::Task* task_parent, T** signal_thread) | |
21 : talk_base::Task(task_parent), | |
22 signal_thread_(NULL), | |
23 finished_(false) { | |
24 SetSignalThread(signal_thread); | |
25 } | |
26 | |
27 virtual ~SignalThreadTask() { | |
28 ClearSignalThread(); | |
29 } | |
30 | |
31 virtual void Stop() { | |
32 Task::Stop(); | |
33 ClearSignalThread(); | |
34 } | |
35 | |
36 virtual int ProcessStart() { | |
37 ASSERT(GetState() == talk_base::Task::STATE_START); | |
38 signal_thread_->SignalWorkDone.connect( | |
39 this, | |
40 &SignalThreadTask<T>::OnWorkDone); | |
41 signal_thread_->Start(); | |
42 return talk_base::Task::STATE_RESPONSE; | |
43 } | |
44 | |
45 int ProcessResponse() { | |
46 if (!finished_) { | |
47 return talk_base::Task::STATE_BLOCKED; | |
48 } | |
49 SignalWorkDone(signal_thread_); | |
50 ClearSignalThread(); | |
51 return talk_base::Task::STATE_DONE; | |
52 } | |
53 | |
54 sigslot::signal1<T*> SignalWorkDone; | |
55 | |
56 private: | |
57 // Takes ownership of signal_thread. | |
58 void SetSignalThread(T** signal_thread) { | |
59 ASSERT(!signal_thread_ && signal_thread && *signal_thread); | |
60 // Verify that no one is listening to the signal thread for work done. | |
61 // They should be using this class instead. | |
62 ASSERT((*signal_thread)->SignalWorkDone.is_empty()); | |
63 | |
64 signal_thread_ = *signal_thread; | |
65 | |
66 // Helps callers not to use signal thread after this point since this class | |
67 // has taken ownership (and avoid the error of doing | |
68 // signal_thread->Start()). | |
69 *signal_thread = NULL; | |
70 } | |
71 | |
72 void OnWorkDone(talk_base::SignalThread* signal_thread) { | |
73 ASSERT(signal_thread == signal_thread_); | |
74 finished_ = true; | |
75 Wake(); | |
76 } | |
77 | |
78 void ClearSignalThread() { | |
79 if (signal_thread_) { | |
80 // Don't wait on the thread destruction, or we may deadlock. | |
81 signal_thread_->Destroy(false); | |
82 signal_thread_ = NULL; | |
83 } | |
84 } | |
85 | |
86 T* signal_thread_; | |
87 bool finished_; | |
88 DISALLOW_COPY_AND_ASSIGN(SignalThreadTask); | |
89 }; | |
90 | |
91 } // namespace notifier | |
92 | |
93 #endif // CHROME_BROWSER_SYNC_NOTIFIER_BASE_SIGNAL_THREAD_TASK_H_ | |
OLD | NEW |