3 * Copyright 2004--2006, 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_TASK_H__
29 #define TALK_BASE_TASK_H__
32 #include "talk/base/basictypes.h"
33 #include "talk/base/scoped_ptr.h"
34 #include "talk/base/sigslot.h"
35 #include "talk/base/taskparent.h"
37 /////////////////////////////////////////////////////////////////////
41 /////////////////////////////////////////////////////////////////////
43 // Task is a state machine infrastructure. States are pushed forward by
44 // pushing forwards a TaskRunner that holds on to all Tasks. The purpose
45 // of Task is threefold:
47 // (1) It manages ongoing work on the UI thread. Multitasking without
48 // threads, keeping it easy, keeping it real. :-) It does this by
49 // organizing a set of states for each task. When you return from your
50 // Process*() function, you return an integer for the next state. You do
51 // not go onto the next state yourself. Every time you enter a state,
52 // you check to see if you can do anything yet. If not, you return
53 // STATE_BLOCKED. If you _could_ do anything, do not return
54 // STATE_BLOCKED - even if you end up in the same state, return
55 // STATE_mysamestate. When you are done, return STATE_DONE and then the
56 // task will self-delete sometime afterwards.
58 // (2) It helps you avoid all those reentrancy problems when you chain
59 // too many triggers on one thread. Basically if you want to tell a task
60 // to process something for you, you feed your task some information and
61 // then you Wake() it. Don't tell it to process it right away. If it
62 // might be working on something as you send it information, you may want
63 // to have a queue in the task.
65 // (3) Finally it helps manage parent tasks and children. If a parent
66 // task gets aborted, all the children tasks are too. The nice thing
67 // about this, for example, is if you have one parent task that
68 // represents, say, and Xmpp connection, then you can spawn a whole bunch
69 // of infinite lifetime child tasks and now worry about cleaning them up.
70 // When the parent task goes to STATE_DONE, the task engine will make
71 // sure all those children are aborted and get deleted.
73 // Notice that Task has a few built-in states, e.g.,
75 // STATE_INIT - the task isn't running yet
76 // STATE_START - the task is in its first state
77 // STATE_RESPONSE - the task is in its second state
78 // STATE_DONE - the task is done
80 // STATE_ERROR - indicates an error - we should audit the error code in
81 // light of any usage of it to see if it should be improved. When I
82 // first put down the task stuff I didn't have a good sense of what was
83 // needed for Abort and Error, and now the subclasses of Task will ground
84 // the design in a stronger way.
86 // STATE_NEXT - the first undefined state number. (like WM_USER) - you
87 // can start defining more task states there.
89 // When you define more task states, just override Process(int state) and
90 // add your own switch statement. If you want to delegate to
91 // Task::Process, you can effectively delegate to its switch statement.
92 // No fancy method pointers or such - this is all just pretty low tech,
93 // easy to debug, and fast.
95 // Also notice that Task has some primitive built-in timeout functionality.
97 // A timeout is defined as "the task stays in STATE_BLOCKED longer than
100 // Descendant classes can override this behavior by calling the
101 // various protected methods to change the timeout behavior. For
102 // instance, a descendand might call SuspendTimeout() when it knows
103 // that it isn't waiting for anything that might timeout, but isn't
104 // yet in the STATE_DONE state.
107 namespace talk_base {
109 // Executes a sequence of steps
110 class Task : public TaskParent {
112 Task(TaskParent *parent);
115 int32 unique_id() { return unique_id_; }
119 int GetState() const { return state_; }
120 bool HasError() const { return (GetState() == STATE_ERROR); }
121 bool Blocked() const { return blocked_; }
122 bool IsDone() const { return done_; }
125 // Called from outside to stop task without any more callbacks
126 void Abort(bool nowake = false);
130 int64 timeout_time() const { return timeout_time_; }
131 int timeout_seconds() const { return timeout_seconds_; }
132 void set_timeout_seconds(int timeout_seconds);
134 sigslot::signal0<> SignalTimeout;
136 // Called inside the task to signal that the task may be unblocked
148 STATE_NEXT = 5, // Subclasses which need more states start here and higher
151 // Called inside to advise that the task should wake and signal an error
156 virtual std::string GetStateName(int state) const;
157 virtual int Process(int state);
159 virtual int ProcessStart() = 0;
160 virtual int ProcessResponse() { return STATE_DONE; }
165 void SuspendTimeout();
166 void ResumeTimeout();
169 virtual int OnTimeout() {
170 // by default, we are finished after timing out
185 int timeout_seconds_;
186 bool timeout_suspended_;
189 static int32 unique_id_seed_;
192 } // namespace talk_base
194 #endif // TALK_BASE_TASK_H__