Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: webrtc/base/signalthread.h

Issue 2877023002: Move webrtc/{base => rtc_base} (Closed)
Patch Set: update presubmit.py and DEPS include rules Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/sha1digest_unittest.cc ('k') | webrtc/base/signalthread.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #ifndef WEBRTC_BASE_SIGNALTHREAD_H_ 11 #ifndef WEBRTC_BASE_SIGNALTHREAD_H_
12 #define WEBRTC_BASE_SIGNALTHREAD_H_ 12 #define WEBRTC_BASE_SIGNALTHREAD_H_
13 13
14 #include <string>
15 14
16 #include "webrtc/base/checks.h" 15 // This header is deprecated and is just left here temporarily during
17 #include "webrtc/base/constructormagic.h" 16 // refactoring. See https://bugs.webrtc.org/7634 for more details.
18 #include "webrtc/base/nullsocketserver.h" 17 #include "webrtc/rtc_base/signalthread.h"
19 #include "webrtc/base/sigslot.h"
20 #include "webrtc/base/thread.h"
21
22 namespace rtc {
23
24 ///////////////////////////////////////////////////////////////////////////////
25 // SignalThread - Base class for worker threads. The main thread should call
26 // Start() to begin work, and then follow one of these models:
27 // Normal: Wait for SignalWorkDone, and then call Release to destroy.
28 // Cancellation: Call Release(true), to abort the worker thread.
29 // Fire-and-forget: Call Release(false), which allows the thread to run to
30 // completion, and then self-destruct without further notification.
31 // Periodic tasks: Wait for SignalWorkDone, then eventually call Start()
32 // again to repeat the task. When the instance isn't needed anymore,
33 // call Release. DoWork, OnWorkStart and OnWorkStop are called again,
34 // on a new thread.
35 // The subclass should override DoWork() to perform the background task. By
36 // periodically calling ContinueWork(), it can check for cancellation.
37 // OnWorkStart and OnWorkDone can be overridden to do pre- or post-work
38 // tasks in the context of the main thread.
39 ///////////////////////////////////////////////////////////////////////////////
40
41 class SignalThread
42 : public sigslot::has_slots<>,
43 protected MessageHandler {
44 public:
45 explicit SignalThread(bool use_socket_server = true);
46
47 // Context: Main Thread. Call before Start to change the worker's name.
48 bool SetName(const std::string& name, const void* obj);
49
50 // Context: Main Thread. Call to begin the worker thread.
51 void Start();
52
53 // Context: Main Thread. If the worker thread is not running, deletes the
54 // object immediately. Otherwise, asks the worker thread to abort processing,
55 // and schedules the object to be deleted once the worker exits.
56 // SignalWorkDone will not be signalled. If wait is true, does not return
57 // until the thread is deleted.
58 void Destroy(bool wait);
59
60 // Context: Main Thread. If the worker thread is complete, deletes the
61 // object immediately. Otherwise, schedules the object to be deleted once
62 // the worker thread completes. SignalWorkDone will be signalled.
63 void Release();
64
65 // Context: Main Thread. Signalled when work is complete.
66 sigslot::signal1<SignalThread *> SignalWorkDone;
67
68 enum { ST_MSG_WORKER_DONE, ST_MSG_FIRST_AVAILABLE };
69
70 protected:
71 ~SignalThread() override;
72
73 Thread* worker() { return &worker_; }
74
75 // Context: Main Thread. Subclass should override to do pre-work setup.
76 virtual void OnWorkStart() { }
77
78 // Context: Worker Thread. Subclass should override to do work.
79 virtual void DoWork() = 0;
80
81 // Context: Worker Thread. Subclass should call periodically to
82 // dispatch messages and determine if the thread should terminate.
83 bool ContinueWork();
84
85 // Context: Worker Thread. Subclass should override when extra work is
86 // needed to abort the worker thread.
87 virtual void OnWorkStop() { }
88
89 // Context: Main Thread. Subclass should override to do post-work cleanup.
90 virtual void OnWorkDone() { }
91
92 // Context: Any Thread. If subclass overrides, be sure to call the base
93 // implementation. Do not use (message_id < ST_MSG_FIRST_AVAILABLE)
94 void OnMessage(Message* msg) override;
95
96 private:
97 enum State {
98 kInit, // Initialized, but not started
99 kRunning, // Started and doing work
100 kReleasing, // Same as running, but to be deleted when work is done
101 kComplete, // Work is done
102 kStopping, // Work is being interrupted
103 };
104
105 class Worker : public Thread {
106 public:
107 explicit Worker(SignalThread* parent, bool use_socket_server)
108 : Thread(use_socket_server
109 ? SocketServer::CreateDefault()
110 : std::unique_ptr<SocketServer>(new NullSocketServer())),
111 parent_(parent) {}
112 ~Worker() override;
113 void Run() override;
114 bool IsProcessingMessages() override;
115
116 private:
117 SignalThread* parent_;
118
119 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Worker);
120 };
121
122 class SCOPED_LOCKABLE EnterExit {
123 public:
124 explicit EnterExit(SignalThread* t) EXCLUSIVE_LOCK_FUNCTION(t->cs_)
125 : t_(t) {
126 t_->cs_.Enter();
127 // If refcount_ is zero then the object has already been deleted and we
128 // will be double-deleting it in ~EnterExit()! (shouldn't happen)
129 RTC_DCHECK_NE(0, t_->refcount_);
130 ++t_->refcount_;
131 }
132 ~EnterExit() UNLOCK_FUNCTION() {
133 bool d = (0 == --t_->refcount_);
134 t_->cs_.Leave();
135 if (d)
136 delete t_;
137 }
138
139 private:
140 SignalThread* t_;
141
142 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(EnterExit);
143 };
144
145 void Run();
146 void OnMainThreadDestroyed();
147
148 Thread* main_;
149 Worker worker_;
150 CriticalSection cs_;
151 State state_;
152 int refcount_;
153
154 RTC_DISALLOW_COPY_AND_ASSIGN(SignalThread);
155 };
156
157 ///////////////////////////////////////////////////////////////////////////////
158
159 } // namespace rtc
160 18
161 #endif // WEBRTC_BASE_SIGNALTHREAD_H_ 19 #endif // WEBRTC_BASE_SIGNALTHREAD_H_
OLDNEW
« no previous file with comments | « webrtc/base/sha1digest_unittest.cc ('k') | webrtc/base/signalthread.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698