blob: be54d9cb4a05010d7f0e22eeca405ce61e9f713d [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_SIGNALTHREAD_H_
12#define RTC_BASE_SIGNALTHREAD_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"
17#include "rtc_base/constructormagic.h"
18#include "rtc_base/nullsocketserver.h"
Artem Titove41c4332018-07-25 15:04:28 +020019#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/thread.h"
deadbeef8290ddf2017-07-11 16:56:05 -070021
22namespace 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
Yves Gerey665174f2018-06-19 15:03:05 +020041class SignalThread : public sigslot::has_slots<>, protected MessageHandler {
deadbeef8290ddf2017-07-11 16:56:05 -070042 public:
Niels Möller77d37112018-01-15 12:59:43 +010043 SignalThread();
deadbeef8290ddf2017-07-11 16:56:05 -070044
45 // Context: Main Thread. Call before Start to change the worker's name.
46 bool SetName(const std::string& name, const void* obj);
47
48 // Context: Main Thread. Call to begin the worker thread.
49 void Start();
50
51 // Context: Main Thread. If the worker thread is not running, deletes the
52 // object immediately. Otherwise, asks the worker thread to abort processing,
53 // and schedules the object to be deleted once the worker exits.
54 // SignalWorkDone will not be signalled. If wait is true, does not return
55 // until the thread is deleted.
56 void Destroy(bool wait);
57
58 // Context: Main Thread. If the worker thread is complete, deletes the
59 // object immediately. Otherwise, schedules the object to be deleted once
60 // the worker thread completes. SignalWorkDone will be signalled.
61 void Release();
62
63 // Context: Main Thread. Signalled when work is complete.
Yves Gerey665174f2018-06-19 15:03:05 +020064 sigslot::signal1<SignalThread*> SignalWorkDone;
deadbeef8290ddf2017-07-11 16:56:05 -070065
66 enum { ST_MSG_WORKER_DONE, ST_MSG_FIRST_AVAILABLE };
67
68 protected:
69 ~SignalThread() override;
70
71 Thread* worker() { return &worker_; }
72
73 // Context: Main Thread. Subclass should override to do pre-work setup.
Yves Gerey665174f2018-06-19 15:03:05 +020074 virtual void OnWorkStart() {}
deadbeef8290ddf2017-07-11 16:56:05 -070075
76 // Context: Worker Thread. Subclass should override to do work.
77 virtual void DoWork() = 0;
78
79 // Context: Worker Thread. Subclass should call periodically to
80 // dispatch messages and determine if the thread should terminate.
81 bool ContinueWork();
82
83 // Context: Worker Thread. Subclass should override when extra work is
84 // needed to abort the worker thread.
Yves Gerey665174f2018-06-19 15:03:05 +020085 virtual void OnWorkStop() {}
deadbeef8290ddf2017-07-11 16:56:05 -070086
87 // Context: Main Thread. Subclass should override to do post-work cleanup.
Yves Gerey665174f2018-06-19 15:03:05 +020088 virtual void OnWorkDone() {}
deadbeef8290ddf2017-07-11 16:56:05 -070089
90 // Context: Any Thread. If subclass overrides, be sure to call the base
91 // implementation. Do not use (message_id < ST_MSG_FIRST_AVAILABLE)
92 void OnMessage(Message* msg) override;
93
94 private:
95 enum State {
Yves Gerey665174f2018-06-19 15:03:05 +020096 kInit, // Initialized, but not started
97 kRunning, // Started and doing work
98 kReleasing, // Same as running, but to be deleted when work is done
99 kComplete, // Work is done
100 kStopping, // Work is being interrupted
deadbeef8290ddf2017-07-11 16:56:05 -0700101 };
102
103 class Worker : public Thread {
104 public:
Taylor Brandstetter08672602018-03-02 15:20:33 -0800105 explicit Worker(SignalThread* parent);
deadbeef8290ddf2017-07-11 16:56:05 -0700106 ~Worker() override;
107 void Run() override;
108 bool IsProcessingMessages() override;
109
110 private:
111 SignalThread* parent_;
112
113 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Worker);
114 };
115
danilchap3c6abd22017-09-06 05:46:29 -0700116 class RTC_SCOPED_LOCKABLE EnterExit {
deadbeef8290ddf2017-07-11 16:56:05 -0700117 public:
danilchap3c6abd22017-09-06 05:46:29 -0700118 explicit EnterExit(SignalThread* t) RTC_EXCLUSIVE_LOCK_FUNCTION(t->cs_)
deadbeef8290ddf2017-07-11 16:56:05 -0700119 : t_(t) {
120 t_->cs_.Enter();
121 // If refcount_ is zero then the object has already been deleted and we
122 // will be double-deleting it in ~EnterExit()! (shouldn't happen)
123 RTC_DCHECK_NE(0, t_->refcount_);
124 ++t_->refcount_;
125 }
danilchap3c6abd22017-09-06 05:46:29 -0700126 ~EnterExit() RTC_UNLOCK_FUNCTION() {
deadbeef8290ddf2017-07-11 16:56:05 -0700127 bool d = (0 == --t_->refcount_);
128 t_->cs_.Leave();
129 if (d)
130 delete t_;
131 }
132
133 private:
134 SignalThread* t_;
135
136 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(EnterExit);
137 };
138
139 void Run();
140 void OnMainThreadDestroyed();
141
142 Thread* main_;
143 Worker worker_;
144 CriticalSection cs_;
145 State state_;
146 int refcount_;
147
148 RTC_DISALLOW_COPY_AND_ASSIGN(SignalThread);
149};
150
151///////////////////////////////////////////////////////////////////////////////
152
153} // namespace rtc
154
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200155#endif // RTC_BASE_SIGNALTHREAD_H_