xorp

task.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2006-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 #ifndef __LIBXORP_TASK_HH__
00023 #define __LIBXORP_TASK_HH__
00024 
00025 
00026 #include "debug.h"
00027 #include "round_robin.hh"
00028 #include "callback.hh"
00029 
00030 
00031 
00032 class TaskList;
00033 class TaskNode;
00034 class XorpTask;
00035 
00036 typedef XorpCallback0<void>::RefPtr OneoffTaskCallback;
00037 typedef XorpCallback0<bool>::RefPtr RepeatedTaskCallback;
00038 typedef XorpCallback1<void, XorpTask&>::RefPtr BasicTaskCallback;
00039 
00040 
00041 class TaskNode :
00042     public NONCOPYABLE,
00043     public RoundRobinObjBase
00044 {
00045 public:
00046     TaskNode(TaskList* task_list, BasicTaskCallback cb);
00047     virtual ~TaskNode();
00048 
00049     void add_ref();
00050     void release_ref();
00051 
00052     void schedule(int priority, int weight);
00053     void reschedule();
00054     void unschedule();
00055 
00056     int priority()              const   { return _priority; }
00057     int weight()                const   { return _weight; }
00058 
00059     virtual void run(XorpTask &) {};     // Implemented by children
00060 
00061 private:
00062     TaskList*   _task_list; // TaskList this node is associated with
00063     BasicTaskCallback _cb;
00064     int     _ref_cnt;   // Number of referring XorpTask objects
00065     int     _priority;  // Scheduling priority
00066     int     _weight;    // Scheduling weight
00067 };
00068 
00069 
00070 class XorpTask {
00071 public:
00072     XorpTask() : _task_node(NULL) {}
00073     XorpTask(const XorpTask& xorp_task);
00074     XorpTask(TaskNode* task_node);
00075     XorpTask(TaskList* task_list, BasicTaskCallback cb);
00076     ~XorpTask();
00077 
00078     //
00079     // Task/Timer priorities. Those are suggested values.
00080     //
00081     static const int PRIORITY_HIGHEST       = 0;
00082     static const int PRIORITY_XRL_KEEPALIVE = 1;
00083     static const int PRIORITY_HIGH      = 2;
00084     static const int PRIORITY_DEFAULT       = 4;
00085     static const int PRIORITY_BACKGROUND    = 7;
00086     static const int PRIORITY_LOWEST        = 9;
00087     static const int PRIORITY_INFINITY      = 255;
00088 
00089     //
00090     // Task/Timer weights. Those are suggested values.
00091     //
00092     static const int WEIGHT_DEFAULT     = 1;
00093 
00094     XorpTask& operator=(const XorpTask& other);
00095     void schedule(int priority, int weight);
00096     void reschedule();
00097     void unschedule();
00098     bool scheduled() const;
00099 
00100 private:
00101     TaskNode* _task_node;
00102 };
00103 
00104 
00105 class TaskList {
00106 public:
00107     ~TaskList();
00108 
00113     void run();
00114 
00122     XorpTask new_oneoff_task(const OneoffTaskCallback& cb,
00123                  int priority = XorpTask::PRIORITY_DEFAULT,
00124                  int weight = XorpTask::WEIGHT_DEFAULT);
00125 
00135     XorpTask new_task(const RepeatedTaskCallback& cb,
00136               int priority = XorpTask::PRIORITY_DEFAULT,
00137               int weight = XorpTask::WEIGHT_DEFAULT);
00138 
00144     int get_runnable_priority() const;
00145 
00146     void schedule_node(TaskNode* node);
00147     void unschedule_node(TaskNode* node);
00148 
00149     bool empty() const;
00150 
00151 private:
00152     RoundRobinQueue* find_round_robin(int priority);
00153 
00154     map<int, RoundRobinQueue*> _rr_list;
00155 };
00156 
00157 
00158 // ----------------------------------------------------------------------------
00159 // inline Task methods
00160 
00161 inline
00162 XorpTask::XorpTask(TaskList* task_list, BasicTaskCallback cb)
00163     : _task_node(new TaskNode(task_list, cb))
00164 {
00165     if (_task_node != NULL)
00166     _task_node->add_ref();
00167 }
00168 
00169 inline
00170 XorpTask::XorpTask(const XorpTask& xorp_task)
00171     : _task_node(xorp_task._task_node)
00172 {
00173     debug_msg("XorpTask copy constructor %p, n = %p\n", this, _task_node);
00174 
00175     if (_task_node != NULL)
00176     _task_node->add_ref();
00177 }
00178 
00179 inline
00180 XorpTask::~XorpTask()
00181 {
00182     debug_msg("XorpTask destructor %p, n = %p\n", this, _task_node);
00183 
00184     if (_task_node != NULL)
00185     _task_node->release_ref();
00186 }
00187 
00188 inline
00189 XorpTask::XorpTask(TaskNode* task_node)
00190     : _task_node(task_node)
00191 {
00192     debug_msg("XorpTask constructor %p, n = %p\n", this, _task_node);
00193 
00194     if (_task_node)
00195     _task_node->add_ref();
00196 }
00197 
00198 inline XorpTask&
00199 XorpTask::operator=(const XorpTask& other)
00200 {
00201     if (other._task_node != NULL)
00202     other._task_node->add_ref();
00203     if (_task_node != NULL)
00204     _task_node->release_ref();
00205     _task_node = other._task_node;
00206     return *this;
00207 }
00208 
00209 inline void
00210 XorpTask::schedule(int priority, int weight)
00211 {
00212     _task_node->schedule(priority, weight);
00213 }
00214 
00215 inline void
00216 XorpTask::reschedule()
00217 {
00218     _task_node->reschedule();
00219 }
00220 
00221 #endif // __LIBXORP_TASK_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations