Thread.h

Go to the documentation of this file.
00001 /** @file Thread.h
00002 * @author Gabor Madl
00003 * @date Created 02/2005
00004 * @brief Thread implementation.
00005 *
00006 *
00007 * =================================================================
00008 * DREAM License v2.0
00009 * 
00010 * DREAM - Distributed Real-time Embedded Analysis Method
00011 * http://dre.sourceforge.net.
00012 * Copyright (c) 2005-2007 Gabor Madl, All Rights Reserved.
00013 * 
00014 * This file is part of DREAM.
00015 * 
00016 * DREAM is free software; you can redistribute it and/or modify it
00017 * under the terms of the GNU General Public License version 2 as
00018 * published by the Free Software Foundation. No future versions of
00019 * the GPL license may be automatically applied to DREAM. It is in
00020 * the sole discretion of the copyright holder to determine whether
00021 * DREAM may be released under a different license or terms. There
00022 * are no restrictions on the use of DREAM for any purpose.
00023 * 
00024 * DREAM is distributed in the hope that it will be useful,
00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00027 * GNU General Public License for more details.
00028 * 
00029 * You should have received a copy of the GNU General Public License
00030 * along with this program; if not, write to the Free Software
00031 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00032 * MA 02110-1301, USA.
00033 * 
00034 * By submitting comments, suggestions, code, code snippets,
00035 * techniques (including that of usage), and algorithms, submitters
00036 * acknowledge that they have the right to do so, that any such
00037 * submissions are given freely and unreservedly, and that they
00038 * waive any claims to copyright or ownership. In addition,
00039 * submitters acknowledge that any such submission might become
00040 * part of the copyright maintained on the overall body of code,
00041 * which comprises DREAM. By making a submission, submitter agrees
00042 * to these terms. Furthermore, submitters acknowledge that the
00043 * incorporation or modification of such submissions is entirely
00044 * at the discretion of the moderators of the DREAM project.
00045 * 
00046 * DREAM links to the Libxml2 third party library. Please see 
00047 * COPYING-libxml for the copyright information of Libxml2.
00048 * =================================================================
00049 */
00050 
00051 #ifndef DREAM_THREAD
00052 #define DREAM_THREAD
00053 
00054 #include <iostream>
00055 #include <map>
00056 #include <algorithm>
00057 
00058 namespace DREAM
00059 {
00060         class NodeList;
00061         class Thread;
00062         class Channel;
00063 }
00064 
00065 #include "Task.h"
00066 #include "Scheduler.h"
00067 #include "Exception.h"
00068 
00069 namespace DREAM
00070 {
00071 
00072 /** This is a simple map to store Nodes.
00073 * It is the base class for Thread that implements priority-based scheduling.
00074 */
00075 class NodeList
00076 {
00077 public:
00078         /** iterator class for convenience. */
00079 //      typedef typename Iterator<> iterator;
00080 
00081         /** const_iterator class for convenience. */
00082 //      typedef typename Const_Iterator<> const_iterator;
00083 
00084         /** Allow iterators to manipulate protected members. */
00085 //      friend class iterator;
00086 
00087         /** Allow iterators to manipulate protected members. */
00088 //      friend class const_iterator;
00089 
00090         /**Constructor. */
00091         NodeList ();
00092 
00093         /**Destructor. */
00094         virtual ~NodeList ();
00095 
00096         /** Adds a new Node to the list 
00097         * @param node_ptr specifies the Node to be added.
00098         */
00099         virtual void add (DREAM::Node* node_ptr);
00100 
00101         /** Clears contents of the map. */
00102         virtual void clear ();
00103 
00104         /** Does nothing. A nonconcurrent thread does not have an execution queue. */
00105         virtual void dequeue (DREAM::Node* node_ptr)
00106 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00107                 throw (DREAM::Exception)
00108 #endif
00109         {};
00110 
00111         /** Does nothing. A nonconcurrent thread does not have an execution queue. */
00112         virtual void enqueue (DREAM::Node* node_ptr) {};
00113 
00114         /** Returns a Node from the list.
00115         * @param id specifies the id of the Node to be returned.
00116         * @return pointer to the Node.
00117         */
00118         virtual DREAM::Node* find (const std::string& id) const;
00119 
00120         /** Returns the whole node_map_ for manipulation.
00121         * @return pointer to the node_map_.
00122         */
00123         virtual DREAM::NODE_MAP* get_map ()
00124 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00125                 throw (DREAM::Exception)
00126 #endif
00127         ;
00128 
00129         /** Publisher.
00130         * Calls the Node::publish () function of all the Nodes in the map.
00131         */
00132         virtual void publish () const;
00133 
00134         /** Removes a Node from the list.
00135         * @param id specifies the Node to be removed.
00136         */
00137         virtual void erase (const std::string& id)
00138 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00139                 throw (DREAM::Exception)
00140 #endif
00141         ;
00142 
00143         /** Takes transitions for every Node in the NodeList. */
00144         virtual void take_transitions ()
00145 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00146                 throw (DREAM::Exception)
00147 #endif
00148         ;
00149 
00150 protected:
00151         /** Map to store Nodes */
00152         DREAM::NODE_MAP node_map_;
00153 };
00154 
00155 /** A Thread is a map of schedulable Nodes.
00156 * The Scheduler manages the execution of Threads based on their priority and the scheduling policiy.
00157 */
00158 class Thread : public DREAM::NodeList
00159 {
00160 public:
00161         /**Constructor. */
00162         Thread (DREAM::Scheduler* scheduler_ptr, uint priority = 0, uint threads = 1);
00163 
00164         /**Destructor. */
00165         virtual ~Thread ();
00166 
00167         /** Adds a Task in the error AVL tree - error_avltree_.
00168         * @param task_ptr is a pointer to the Task which will be put in the error AVL tree - error_avltree_.
00169         */
00170         virtual void add_error (DREAM::Task* task_ptr);
00171 
00172         /** Returns the number of available Threads. */
00173         virtual uint availablethreads () const;
00174 
00175         /** Deploys a Thread to a Scheduler.
00176         * This function updates the scheduler_ptr in the Thread.
00177         * @param scheduler_ptr is the Scheduler where the Thread will be deployed.
00178         */
00179         virtual void deploy (DREAM::Scheduler* scheduler_ptr)
00180                 throw (DREAM::Exception);
00181 
00182         /** Removes an idle Node from the execution queue - exec_map_.
00183         * @param node_ptr is a pointer to the Node which will be removed from the execution queue - exec_map_.
00184         */
00185         virtual void dequeue (DREAM::Node* node_ptr)
00186 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00187                 throw (DREAM::Exception)
00188 #endif
00189         ;
00190 
00191 #ifdef DREAM_RACE_CONDITION
00192         /** Puts an enabled Channel in the execution queue - exec_map_ - and reschedules if necessary.
00193         * @param channel_ptr is a pointer to the Channel which will be put in the execution queue - exec_map_.
00194         */
00195         virtual void enqueue (DREAM::Channel* channel_ptr);
00196 
00197         /** Puts an enabled Task in the execution queue - exec_map_ -, and the recent queue - recent_map_ - and reschedules if necessary.
00198         * @param task_ptr is a pointer to the Task which will be put in the queues.
00199         */
00200         virtual void enqueue (DREAM::Task* task_ptr);
00201 #else
00202         /** Puts an enabled Node in the execution queue - exec_map_ -, and the recent queue - recent_map_ - and reschedules if necessary.
00203         * @param node_ptr is a pointer to the Node which will be put in the queues.
00204         */
00205         virtual void enqueue (DREAM::Node* node_ptr);
00206 #endif
00207 
00208         /** Frees an available Thread from the lane. */
00209         virtual void freethread ()
00210 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00211                 throw (DREAM::Exception)
00212 #endif
00213         ;
00214 
00215         /** Returns the whole exec_map_ for manipulation.
00216         * @return a pointer to the exec_map_.
00217         */
00218         virtual DREAM::NODE_MAP* get_exec_map ()
00219 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00220                 throw (DREAM::Exception)
00221 #endif
00222         ;
00223 
00224 #ifdef DREAM_RACE_CONDITION
00225         /** Returns whether a Task has been recently enabled */
00226         virtual const DREAM::TASK_AVLTREE* get_recent_avltree () const;
00227 #endif
00228 
00229         /** Return the time of the next event generated by the Thread. */
00230         virtual double next_event () const
00231 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00232                 throw (DREAM::Exception)
00233 #endif
00234         ;
00235 
00236         /** Returns the priority of the Thread.
00237         * @return the priority of the Thread priority_.
00238         */
00239         virtual uint priority () const;
00240 
00241 #ifdef DREAM_RACE_CONDITION
00242         /** Returns whether a Task has been recently enabled together with other tasks.
00243         * @param task_ptr is a pointer to the Task to be checked.
00244         */
00245         virtual bool recent (DREAM::Task* task_ptr) const;
00246 #endif
00247 
00248         /** Resets Nodes in the map */
00249         void reset ()
00250 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00251                 throw (DREAM::Exception)
00252 #endif
00253         ;
00254 
00255         /** Checks whether the Thread is schedulable.
00256         * @return true is the Thread is schedulable, false otherwise.
00257         */
00258         virtual bool schedulable () const;
00259 
00260         /** Returns a pointer to the Scheduler.
00261         * Used in output generation.
00262         */
00263         virtual const DREAM::Scheduler* scheduler () const
00264 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00265                 throw (DREAM::Exception)
00266 #endif
00267         ;
00268         
00269         /** Returns a pointer to the Scheduler.
00270         * Used in output generation.
00271         */
00272         virtual DREAM::Scheduler* scheduler ()
00273 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00274                 throw (DREAM::Exception)
00275 #endif
00276         ;
00277         
00278         /** Returns the number of the Threads. */
00279         virtual uint threads () const;
00280 
00281         /** Time jump.
00282         * @param time_step specifies the time step to be made.
00283         */
00284         virtual void time_step (double time_step)
00285 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00286                 throw (DREAM::Exception)
00287 #endif
00288         ;
00289 
00290         /** Function used to output info.
00291         * @param output is the std::string to be displayed.
00292         */
00293         virtual void trace (const std::string& output) const;
00294 
00295         /** Takes an available Thread from the lane. */
00296         virtual void usethread ()
00297 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00298                 throw (DREAM::Exception)
00299 #endif
00300         ;
00301 
00302         /** This function is part of the IF system description generator.
00303         * @param task_map stores the Task pointers.
00304         * @param channel_map stores the Channel pointers.
00305         * @param timer_map stores the Timer pointers.
00306         * @param f_stream is the output stream to be written to.
00307         */
00308         virtual void visitor_if (DREAM::NODE_MAP* task_map, DREAM::NODE_MAP* channel_map, DREAM::NODE_MAP* timer_map, std::ofstream& f_stream)
00309 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00310                 throw (DREAM::Exception)
00311 #endif
00312         ;
00313 
00314         /** This function fills the IDs (names) of tasks, channels and timers to the parameter lists.
00315         * The function is used to generate output for model checkers.
00316         * @param task_map stores the Task pointers.
00317         * @param channel_map stores the Channel pointers.
00318         * @param timer_map stores the Timer pointers.
00319         */
00320         virtual void visitor_map (DREAM::NODE_MAP* task_map, DREAM::NODE_MAP* channel_map, 
00321                 DREAM::NODE_MAP* timer_map)
00322 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00323                         throw (DREAM::Exception)
00324 #endif
00325         ;
00326 
00327         /** This function builds the task list for manipulation.
00328         * The function is used by genetic algorithms.
00329         * @param task_avltree stores the Task pointers.
00330         */
00331         virtual void visitor_task_avltree (DREAM::TASK_AVLTREE* task_avltree)
00332 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00333                 throw (DREAM::Exception)
00334 #endif
00335         ;
00336 
00337         /** This function updates task values in the system.
00338         * The function is used by genetic algorithms.
00339         * @param task_avltree stores the Task pointers.
00340         */
00341         virtual void visitor_update_task_avltree (DREAM::TASK_AVLTREE* task_avltree)
00342 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00343                 throw (DREAM::Exception)
00344 #endif
00345         ;
00346 
00347         /** This function is part of the Uppaal system description generator.
00348         * @param task_map stores the Task pointers.
00349         * @param channel_map stores the Channel pointers.
00350         * @param timer_map stores the Timer pointers.
00351         * @param f_stream is the output stream to be written to.
00352         */
00353         virtual void visitor_uppaal (DREAM::NODE_MAP* task_map, DREAM::NODE_MAP* channel_map, 
00354                 DREAM::NODE_MAP* timer_map, std::ofstream& f_stream)
00355 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00356                         throw (DREAM::Exception)
00357 #endif
00358         ;
00359 
00360 private:
00361 
00362         /** Specifies the number of available threads in the lane. */
00363         uint availablethreads_;
00364 
00365         /** Map to store Nodes. */
00366         DREAM::NODE_MAP exec_map_;
00367 
00368 #ifdef DREAM_RACE_CONDITION
00369         /** AVL tree that stores Nodes that have just become enabled.
00370         * This AVL tree is used by the model checker to check for race conditions.
00371         */
00372         DREAM::TASK_AVLTREE recent_avltree_;
00373 #endif
00374 
00375         /** Priority of the Thread. */
00376         uint priority_;
00377 
00378         /** Pointer to the Scheduler which manages the NodeList/Thread. */
00379         DREAM::Scheduler* scheduler_ptr_;
00380 
00381         /** Specifies the number of threads in the lane. */
00382         uint threads_;
00383 };
00384 
00385 }
00386 
00387 #endif
00388 

Generated on Fri Jul 27 18:30:03 2007 for DREAM by  doxygen 1.5.1