/* * Baltisot * Copyright (C) 1999-2008 Nicolas "Pixel" Noble * * 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 2 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef __TASK_H__ #define __TASK_H__ #ifndef _WIN32 #include #include #else #include #endif #include #include #include #include #undef E_HANDLE #undef Yield class Task : public Base { public: enum { EVT_BURST = 0, EVT_HANDLE, EVT_PROCESS, EVT_TIMEOUT, EVT_TASK, EVT_IDLE, }; enum { TASK_ON_HOLD = 0, TASK_DONE, TASK_BURST, TASK_IDLE, TASK_IDLE_REST, }; enum { W4_STICKY = 1, W4_READING = 2, W4_WRITING = 4, }; Task() : current(0), state(TASK_ON_HOLD), stopped(false), suspended(false), yielded(false), wbta(0), wta(0), BurstHandle(0) { TaskMan::AddTask(this); } virtual ~Task(); virtual String GetName() { return "Unknow Task"; } int Run(); int DryRun(); int GetState() { return state; } void Suspend(int = -1) throw (GeneralException); void WaitFor(Task * t) { t->wbta = this; wta = t; } void WaitFor(Handle * h, int flags = 0) { h->SetNonBlock(); TaskMan::WaitFor(h, this, flags); } void WaitFor(pid_t p) { TaskMan::WaitFor(p, this); } void WaitFor(const timeval & t, int flags = 0) { TaskMan::WaitFor(t, this, flags); } void Yield() { yielded = true; Suspend(TASK_ON_HOLD); } bool Yielded() { return yielded; } void Unyield() { yielded = false; SetBurst(); } Task * WaitedBy() { return wbta; } void SetBurst() { state = TASK_BURST; } void SetIdle() { state = TASK_IDLE; } void IdleRest() { state = TASK_IDLE_REST; } void Stop() { stopped = true; } void Restart() { stopped = false; } bool IsStopped() { return stopped; } void RemoveFromWatches() { wbta = 0; } void RemoveTimeout() { TaskMan::RemoveTimeout(this); } Handle * BurstHandle; protected: virtual int Do() throw (GeneralException) { return TASK_ON_HOLD; } int current; private: class wbta_t { public: wbta_t(Task * ata) : ta(ata) { } Task * ta; }; int state; bool stopped; bool suspended; bool yielded; Task * wbta; Task * wta; }; #endif