3
* Copyright 2004--2010, 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_WORKER_H_
29
#define TALK_BASE_WORKER_H_
31
#include "talk/base/constructormagic.h"
32
#include "talk/base/messagehandler.h"
38
// A worker is an object that performs some specific long-lived task in an
39
// event-driven manner.
40
// The only method that should be considered thread-safe is HaveWork(), which
41
// allows you to signal the availability of work from any thread. All other
42
// methods are thread-hostile. Specifically:
43
// StartWork()/StopWork() should not be called concurrently with themselves or
44
// each other, and it is an error to call them while the worker is running on
45
// a different thread.
46
// The destructor may not be called if the worker is currently running
47
// (regardless of the thread), but you can call StopWork() in a subclass's
49
class Worker : private MessageHandler {
53
// Destroys this Worker, but it must have already been stopped via StopWork().
56
// Attaches the worker to the current thread and begins processing work if not
59
// Stops processing work if currently doing so and detaches from the current
64
// Signal that work is available to be done. May only be called within the
65
// lifetime of a OnStart()/OnStop() pair.
68
// These must be implemented by a subclass.
69
// Called on the worker thread to start working.
70
virtual void OnStart() = 0;
71
// Called on the worker thread when work has been signalled via HaveWork().
72
virtual void OnHaveWork() = 0;
73
// Called on the worker thread to stop working. Upon return, any pending
74
// OnHaveWork() calls are cancelled.
75
virtual void OnStop() = 0;
78
// Inherited from MessageHandler.
79
virtual void OnMessage(Message *msg);
81
// The thread that is currently doing the work.
82
Thread *worker_thread_;
84
DISALLOW_COPY_AND_ASSIGN(Worker);
87
} // namespace talk_base
89
#endif // TALK_BASE_WORKER_H_