blob: 582fe1b9f7b39e3ed0946a13df9442d84d6a9e6c [file] [log] [blame]
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2010, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
28#ifndef TALK_BASE_WORKER_H_
29#define TALK_BASE_WORKER_H_
30
31#include "talk/base/constructormagic.h"
32#include "talk/base/messagehandler.h"
33
34namespace talk_base {
35
36class Thread;
37
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
48// destructor.
49class Worker : private MessageHandler {
50 public:
51 Worker();
52
53 // Destroys this Worker, but it must have already been stopped via StopWork().
54 virtual ~Worker();
55
56 // Attaches the worker to the current thread and begins processing work if not
57 // already doing so.
58 bool StartWork();
59 // Stops processing work if currently doing so and detaches from the current
60 // thread.
61 bool StopWork();
62
63 protected:
64 // Signal that work is available to be done. May only be called within the
65 // lifetime of a OnStart()/OnStop() pair.
66 void HaveWork();
67
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;
76
77 private:
78 // Inherited from MessageHandler.
79 virtual void OnMessage(Message *msg);
80
81 // The thread that is currently doing the work.
82 Thread *worker_thread_;
83
84 DISALLOW_COPY_AND_ASSIGN(Worker);
85};
86
87} // namespace talk_base
88
89#endif // TALK_BASE_WORKER_H_