3
* Copyright 2004--2009, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#ifndef TALK_BASE_SIGNALTHREAD_H_
29
#define TALK_BASE_SIGNALTHREAD_H_
33
#include "talk/base/constructormagic.h"
34
#include "talk/base/thread.h"
35
#include "talk/base/sigslot.h"
39
///////////////////////////////////////////////////////////////////////////////
40
// SignalThread - Base class for worker threads. The main thread should call
41
// Start() to begin work, and then follow one of these models:
42
// Normal: Wait for SignalWorkDone, and then call Release to destroy.
43
// Cancellation: Call Release(true), to abort the worker thread.
44
// Fire-and-forget: Call Release(false), which allows the thread to run to
45
// completion, and then self-destruct without further notification.
46
// Periodic tasks: Wait for SignalWorkDone, then eventually call Start()
47
// again to repeat the task. When the instance isn't needed anymore,
48
// call Release. DoWork, OnWorkStart and OnWorkStop are called again,
50
// The subclass should override DoWork() to perform the background task. By
51
// periodically calling ContinueWork(), it can check for cancellation.
52
// OnWorkStart and OnWorkDone can be overridden to do pre- or post-work
53
// tasks in the context of the main thread.
54
///////////////////////////////////////////////////////////////////////////////
56
class SignalThread : public sigslot::has_slots<>, protected MessageHandler {
60
// Context: Main Thread. Call before Start to change the worker's name.
61
bool SetName(const std::string& name, const void* obj);
63
// Context: Main Thread. Call before Start to change the worker's priority.
64
bool SetPriority(ThreadPriority priority);
66
// Context: Main Thread. Call to begin the worker thread.
69
// Context: Main Thread. If the worker thread is not running, deletes the
70
// object immediately. Otherwise, asks the worker thread to abort processing,
71
// and schedules the object to be deleted once the worker exits.
72
// SignalWorkDone will not be signalled. If wait is true, does not return
73
// until the thread is deleted.
74
void Destroy(bool wait);
76
// Context: Main Thread. If the worker thread is complete, deletes the
77
// object immediately. Otherwise, schedules the object to be deleted once
78
// the worker thread completes. SignalWorkDone will be signalled.
81
// Context: Main Thread. Signalled when work is complete.
82
sigslot::signal1<SignalThread *> SignalWorkDone;
84
enum { ST_MSG_WORKER_DONE, ST_MSG_FIRST_AVAILABLE };
87
virtual ~SignalThread();
89
Thread* worker() { return &worker_; }
91
// Context: Main Thread. Subclass should override to do pre-work setup.
92
virtual void OnWorkStart() { }
94
// Context: Worker Thread. Subclass should override to do work.
95
virtual void DoWork() = 0;
97
// Context: Worker Thread. Subclass should call periodically to
98
// dispatch messages and determine if the thread should terminate.
101
// Context: Worker Thread. Subclass should override when extra work is
102
// needed to abort the worker thread.
103
virtual void OnWorkStop() { }
105
// Context: Main Thread. Subclass should override to do post-work cleanup.
106
virtual void OnWorkDone() { }
108
// Context: Any Thread. If subclass overrides, be sure to call the base
109
// implementation. Do not use (message_id < ST_MSG_FIRST_AVAILABLE)
110
virtual void OnMessage(Message *msg);
114
kInit, // Initialized, but not started
115
kRunning, // Started and doing work
116
kReleasing, // Same as running, but to be deleted when work is done
117
kComplete, // Work is done
118
kStopping, // Work is being interrupted
121
class Worker : public Thread {
123
explicit Worker(SignalThread* parent) : parent_(parent) {}
124
virtual void Run() { parent_->Run(); }
127
SignalThread* parent_;
129
DISALLOW_IMPLICIT_CONSTRUCTORS(Worker);
134
explicit EnterExit(SignalThread* t) : t_(t) {
136
// If refcount_ is zero then the object has already been deleted and we
137
// will be double-deleting it in ~EnterExit()! (shouldn't happen)
138
ASSERT(t_->refcount_ != 0);
142
bool d = (0 == --t_->refcount_);
151
DISALLOW_IMPLICIT_CONSTRUCTORS(EnterExit);
155
void OnMainThreadDestroyed();
163
DISALLOW_COPY_AND_ASSIGN(SignalThread);
166
///////////////////////////////////////////////////////////////////////////////
168
} // namespace talk_base
170
#endif // TALK_BASE_SIGNALTHREAD_H_