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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/*
* lftp and utils
*
* Copyright (c) 1996-2008 by Alexander V. Lukyanov (lav@yars.free.net)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* $Id: SMTask.cc,v 1.56 2008/11/27 05:56:28 lav Exp $ */
#include <config.h>
#include <assert.h>
#include <time.h>
#include "trio.h"
#ifdef TIME_WITH_SYS_TIME
# include <sys/types.h>
# include <sys/time.h>
#endif
#include "SMTask.h"
#include "Timer.h"
#include "misc.h"
SMTask *SMTask::chain=0;
SMTask *SMTask::current=0;
xarray<SMTask*> SMTask::stack;
PollVec SMTask::block;
TimeDate SMTask::now;
static int task_count=0;
static SMTask *init_task=new SMTaskInit;
SMTask::SMTask()
{
// insert in the chain
if(current)
{
// insert it so that it would be scanned next
next=current->next;
current->next=this;
}
else
{
next=chain;
chain=this;
}
suspended=false;
suspended_slave=false;
running=0;
ref_count=0;
deleting=false;
task_count++;
#ifdef TASK_DEBUG
printf("new SMTask %p (count=%d)\n",this,task_count);
#endif
}
void SMTask::Suspend()
{
if(suspended)
return;
if(!IsSuspended())
SuspendInternal();
suspended=true;
}
void SMTask::Resume()
{
if(!suspended)
return;
suspended=false;
if(!IsSuspended())
ResumeInternal();
}
void SMTask::SuspendSlave()
{
if(suspended_slave)
return;
if(!IsSuspended())
SuspendInternal();
suspended_slave=true;
}
void SMTask::ResumeSlave()
{
if(!suspended_slave)
return;
suspended_slave=false;
if(!IsSuspended())
ResumeInternal();
}
SMTask::~SMTask()
{
#ifdef TASK_DEBUG
printf("delete SMTask %p (count=%d)\n",this,task_count);
#endif
task_count--;
if(__builtin_expect(running,0))
{
fprintf(stderr,"SMTask(%p).running=%d\n",this,running);
fprintf(stderr,"SMTask stack:");
for(int i=0; i<stack.count(); i++)
fprintf(stderr," %p",stack[i]);
fprintf(stderr,"; current=%p\n",current);
abort();
}
assert(!ref_count);
// remove from the chain
SMTask **scan=&chain;
while(*scan)
{
if(*scan==this)
{
*scan=next;
return;
}
scan=&((*scan)->next);
}
}
void SMTask::DeleteLater()
{
deleting=true;
PrepareToDie();
}
void SMTask::Delete(SMTask *task)
{
if(!task)
return;
task->DeleteLater();
// if possible, delete now.
if(!task->running && !task->ref_count)
delete task;
}
SMTask *SMTask::_SetRef(SMTask *task,SMTask *new_task)
{
_DeleteRef(task);
_MakeRef(new_task);
return new_task;
}
void SMTask::Enter(SMTask *task)
{
stack.append(current);
current=task;
current->running++;
}
void SMTask::Leave(SMTask *task)
{
assert(current==task);
current->running--;
assert(stack.count()>0);
current=stack.last();
stack.chop();
}
int SMTask::Roll(SMTask *task)
{
int m=STALL;
if(task->running || task->deleting)
return m;
Enter(task);
while(!task->deleting && task->Do()==MOVED)
m=MOVED;
Leave(task);
return m;
}
void SMTask::RollAll(const TimeInterval &max_time)
{
Timer limit_timer(max_time);
do { Schedule(); }
while(block.GetTimeout()==0 && !limit_timer.Stopped());
}
int SMTask::CollectGarbage()
{
int count=0;
bool repeat_gc;
do {
repeat_gc=false;
SMTask *scan=chain;
while(scan)
{
if(scan->running || !scan->deleting || scan->ref_count)
{
scan=scan->next;
continue;
}
repeat_gc=true;
count++;
if(scan->next)
{
Enter(scan->next); // protect it from deleting (in scan's dtor)
delete scan;
Leave(scan=current);
}
else
{
delete scan;
break;
}
}
} while(repeat_gc);
return count;
}
void SMTask::Schedule()
{
SMTask *scan;
block.Empty();
// get time once and assume Do() don't take much time
UpdateNow();
int timer_timeout=Timer::GetTimeout();
if(timer_timeout>=0)
block.SetTimeout(timer_timeout);
int res=STALL;
for(scan=chain; scan; scan=scan->next)
{
if(scan->running || scan->IsSuspended())
continue;
Enter(scan); // mark it current and running.
res|=scan->Do(); // let it run.
Leave(scan); // unmark it running and change current.
}
if(CollectGarbage() || res)
block.NoWait();
}
int SMTaskInit::Do()
{
return STALL;
}
SMTaskInit::SMTaskInit()
{
Enter();
}
SMTaskInit::~SMTaskInit()
{
Leave();
}
int SMTask::TaskCount()
{
int count=0;
for(SMTask *scan=chain; scan; scan=scan->next)
count++;
return count;
}
void SMTask::Cleanup()
{
CollectGarbage();
delete init_task;
}
#include <errno.h>
#include "ResMgr.h"
ResDecl enospc_fatal ("xfer:disk-full-fatal","no",ResMgr::BoolValidate,ResMgr::NoClosure);
bool SMTask::NonFatalError(int err)
{
if(E_RETRY(err))
return true;
current->TimeoutS(1);
if(err==ENFILE || err==EMFILE)
return true;
#ifdef ENOBUFS
if(err==ENOBUFS)
return true;
#endif
#ifdef ENOSR
if(err==ENOSR)
return true;
#endif
#ifdef ENOSPC
if(err==ENOSPC)
return !enospc_fatal.QueryBool(0);
#endif
#ifdef EDQUOT
if(err==EDQUOT)
return !enospc_fatal.QueryBool(0);
#endif
current->Timeout(0);
return false; /* fatal error */
}
bool SMTask::TemporaryNetworkError(int err)
{
return temporary_network_error(err);
}
void SMTask::PrintTasks()
{
for(SMTask *scan=chain; scan; scan=scan->next)
{
const char *c=scan->GetLogContext();
if(!c) c="";
printf("%p\t%c%c%c\t%d\t%s\n",scan,scan->running?'R':' ',
scan->suspended?'S':' ',scan->deleting?'D':' ',scan->ref_count,c);
}
}
|