summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jingle/libjingle/talk/base/task.cc
blob: 41ae9068b3e166e4c5aa082a671a9cdbe4967f55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * libjingle
 * Copyright 2004--2005, Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products 
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "task.h"
#include "taskrunner.h"

#include <algorithm>

namespace buzz {

Task::Task(Task * tqparent) :
    state_(STATE_INIT),
    tqparent_(tqparent),
    blocked_(false),
    done_(false),
    aborted_(false),
    busy_(false),
    error_(false),
    child_error_(false),
    start_time_(0) {
  runner_ = ((tqparent == NULL) ? (TaskRunner *)this : tqparent->GetRunner());
  if (tqparent_ != NULL) {
    tqparent_->AddChild(this);
  }
}

unsigned long long
Task::CurrentTime() {
  return runner_->CurrentTime();
}

unsigned long long
Task::ElapsedTime() {
  return CurrentTime() - start_time_;
}

void
Task::Start() {
  if (state_ != STATE_INIT)
    return;
  GetRunner()->StartTask(this);
  start_time_ = CurrentTime();
}

void
Task::Step() {
  if (done_) {
#ifdef DEBUG
    // we do not know how !blocked_ happens when done_ - should be impossible.
    // But it causes problems, so in retail build, we force blocked_, and
    // under debug we assert.
    assert(blocked_);
#else
    blocked_ = true;
#endif
    return;
  }

  // Async Error() was called
  if (error_) {
    done_ = true;
    state_ = STATE_ERROR;
    blocked_ = true;
//   obsolete - an errored task is not considered done now
//   SignalDone();
    Stop();
    return;
  }

  busy_ = true;
  int new_state = Process(state_);
  busy_ = false;

  if (aborted_) {
    Abort(true); // no need to wake because we're awake
    return;
  }
  
  if (new_state == STATE_BLOCKED) {
    blocked_ = true;
  }
  else {
    state_ = new_state;
    blocked_ = false;
  }
  
  if (new_state == STATE_DONE) {
    done_ = true;
  }
  else if (new_state == STATE_ERROR) {
    done_ = true;
    error_ = true;
  }

  if (done_) {
//  obsolete - call this yourself
//    SignalDone();
    Stop();
    blocked_ = true;
  }
}

void
Task::Abort(bool nowake) {
  if (aborted_ || done_)
    return;
  aborted_ = true;
  if (!busy_) {
    done_ = true;
    blocked_ = true;
    error_ = true;
    Stop();
    if (!nowake)
      Wake(); // to self-delete
  }
}

void
Task::Wake() {
  if (done_)
    return;
  if (blocked_) {
    blocked_ = false;
    GetRunner()->WakeTasks();
  }
}

void
Task::Error() {
  if (error_ || done_)
    return;
  error_ = true;
  Wake();
}

std::string
Task::GetStateName(int state) const {
  static const std::string STR_BLOCKED("BLOCKED");
  static const std::string STR_INIT("INIT");
  static const std::string STR_START("START");
  static const std::string STR_DONE("DONE");
  static const std::string STR_ERROR("ERROR");
  static const std::string STR_RESPONSE("RESPONSE");
  static const std::string STR_HUH("??");
  switch (state) {
    case STATE_BLOCKED: return STR_BLOCKED;
    case STATE_INIT: return STR_INIT;
    case STATE_START: return STR_START;
    case STATE_DONE: return STR_DONE;
    case STATE_ERROR: return STR_ERROR;
    case STATE_RESPONSE: return STR_RESPONSE;
  }
  return STR_HUH;
}

int Task::Process(int state) {
  switch (state) {
    case STATE_INIT:
      return STATE_START;
    case STATE_START:
      return ProcessStart();
    case STATE_RESPONSE:
      return ProcessResponse();
    case STATE_DONE:
    case STATE_ERROR:
      return STATE_BLOCKED;
  }
  return STATE_ERROR;
}

void
Task::AddChild(Task * child) {
  tqchildren_.insert(child);
}

bool
Task::AllChildrenDone() {
  for (ChildSet::iterator it = tqchildren_.begin(); it != tqchildren_.end(); ++it) {
    if (!(*it)->IsDone())
      return false;
  }
  return true;
}

bool
Task::AnyChildError() {
  return child_error_;
}

void
Task::AbortAllChildren() {
  if (tqchildren_.size() > 0) {
    ChildSet copy = tqchildren_;
    for (ChildSet::iterator it = copy.begin(); it != copy.end(); ++it) {
      (*it)->Abort(true); // Note we do not wake
    }
  }
}

void
Task::Stop() {
  AbortAllChildren(); // No need to wake because we're either awake or in abort
  tqparent_->OnChildStopped(this);
}

void
Task::OnChildStopped(Task * child) {
  if (child->HasError())
    child_error_ = true;
  tqchildren_.erase(child);
}


}