blob: d9e8ade9b0e21fa0c739bde402b874d5eeff92ae [file] [log] [blame]
deadbeef8290ddf2017-07-11 16:56:05 -07001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_SIGNAL_THREAD_H_
12#define RTC_BASE_SIGNAL_THREAD_H_
deadbeef8290ddf2017-07-11 16:56:05 -070013
14#include <string>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/constructor_magic.h"
18#include "rtc_base/critical_section.h"
19#include "rtc_base/message_handler.h"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "rtc_base/third_party/sigslot/sigslot.h"
21#include "rtc_base/thread.h"
22#include "rtc_base/thread_annotations.h"
deadbeef8290ddf2017-07-11 16:56:05 -070023
24namespace rtc {
25
26///////////////////////////////////////////////////////////////////////////////
27// SignalThread - Base class for worker threads. The main thread should call
28// Start() to begin work, and then follow one of these models:
29// Normal: Wait for SignalWorkDone, and then call Release to destroy.
30// Cancellation: Call Release(true), to abort the worker thread.
31// Fire-and-forget: Call Release(false), which allows the thread to run to
32// completion, and then self-destruct without further notification.
33// Periodic tasks: Wait for SignalWorkDone, then eventually call Start()
34// again to repeat the task. When the instance isn't needed anymore,
35// call Release. DoWork, OnWorkStart and OnWorkStop are called again,
36// on a new thread.
37// The subclass should override DoWork() to perform the background task. By
38// periodically calling ContinueWork(), it can check for cancellation.
39// OnWorkStart and OnWorkDone can be overridden to do pre- or post-work
40// tasks in the context of the main thread.
41///////////////////////////////////////////////////////////////////////////////
42
Yves Gerey665174f2018-06-19 15:03:05 +020043class SignalThread : public sigslot::has_slots<>, protected MessageHandler {
deadbeef8290ddf2017-07-11 16:56:05 -070044 public:
Niels Möller77d37112018-01-15 12:59:43 +010045 SignalThread();
deadbeef8290ddf2017-07-11 16:56:05 -070046
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.
Yves Gerey665174f2018-06-19 15:03:05 +020066 sigslot::signal1<SignalThread*> SignalWorkDone;
deadbeef8290ddf2017-07-11 16:56:05 -070067
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.
Yves Gerey665174f2018-06-19 15:03:05 +020076 virtual void OnWorkStart() {}
deadbeef8290ddf2017-07-11 16:56:05 -070077
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.
Yves Gerey665174f2018-06-19 15:03:05 +020087 virtual void OnWorkStop() {}
deadbeef8290ddf2017-07-11 16:56:05 -070088
89 // Context: Main Thread. Subclass should override to do post-work cleanup.
Yves Gerey665174f2018-06-19 15:03:05 +020090 virtual void OnWorkDone() {}
deadbeef8290ddf2017-07-11 16:56:05 -070091
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 {
Yves Gerey665174f2018-06-19 15:03:05 +020098 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
deadbeef8290ddf2017-07-11 16:56:05 -0700103 };
104
105 class Worker : public Thread {
106 public:
Taylor Brandstetter08672602018-03-02 15:20:33 -0800107 explicit Worker(SignalThread* parent);
deadbeef8290ddf2017-07-11 16:56:05 -0700108 ~Worker() override;
109 void Run() override;
Niels Möller8909a632018-09-06 08:42:44 +0200110 bool IsProcessingMessagesForTesting() override;
deadbeef8290ddf2017-07-11 16:56:05 -0700111
112 private:
113 SignalThread* parent_;
114
115 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Worker);
116 };
117
danilchap3c6abd22017-09-06 05:46:29 -0700118 class RTC_SCOPED_LOCKABLE EnterExit {
deadbeef8290ddf2017-07-11 16:56:05 -0700119 public:
danilchap3c6abd22017-09-06 05:46:29 -0700120 explicit EnterExit(SignalThread* t) RTC_EXCLUSIVE_LOCK_FUNCTION(t->cs_)
deadbeef8290ddf2017-07-11 16:56:05 -0700121 : t_(t) {
122 t_->cs_.Enter();
123 // If refcount_ is zero then the object has already been deleted and we
124 // will be double-deleting it in ~EnterExit()! (shouldn't happen)
125 RTC_DCHECK_NE(0, t_->refcount_);
126 ++t_->refcount_;
127 }
danilchap3c6abd22017-09-06 05:46:29 -0700128 ~EnterExit() RTC_UNLOCK_FUNCTION() {
deadbeef8290ddf2017-07-11 16:56:05 -0700129 bool d = (0 == --t_->refcount_);
130 t_->cs_.Leave();
131 if (d)
132 delete t_;
133 }
134
135 private:
136 SignalThread* t_;
137
138 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(EnterExit);
139 };
140
141 void Run();
142 void OnMainThreadDestroyed();
143
144 Thread* main_;
145 Worker worker_;
146 CriticalSection cs_;
147 State state_;
148 int refcount_;
149
150 RTC_DISALLOW_COPY_AND_ASSIGN(SignalThread);
151};
152
153///////////////////////////////////////////////////////////////////////////////
154
155} // namespace rtc
156
Steve Anton10542f22019-01-11 09:11:00 -0800157#endif // RTC_BASE_SIGNAL_THREAD_H_