ithread.h

Go to the documentation of this file.
00001 #ifndef ITHREAD_CLASS
00002 #define ITHREAD_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : ithread / managed_thread                                          *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1998-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "mechanisms_dll.h"
00019 
00020 #include <basis/object_base.h>
00021 
00022 // forward.
00023 class time_stamp;
00024 #ifdef __UNIX__
00025   typedef long unsigned int pthread_t;
00026 #endif
00027 
00029 
00038 class MECHANISMS_CLASS_STYLE ithread : public virtual object_base
00039 {
00040 public:
00041   ithread();
00043 
00052   enum timed_thread_types { TIGHT_INTERVAL, SLACK_INTERVAL };
00053 
00054   ithread(int sleep_timer, timed_thread_types how = SLACK_INTERVAL);
00056 
00071   virtual ~ithread();
00072 
00073   IMPLEMENT_CLASS_NAME("ithread");
00074 
00075   bool start(void *thread_data);
00077 
00081   void stop();
00083 
00087   inline void cancel() { _stop_thread = true; }
00089 
00091   virtual void pre_thread();
00093 
00095   virtual void post_thread();
00097 
00100   virtual void perform_activity(void *thread_data) = 0;
00102 
00107   void exempt_stop();
00109 
00113   void reschedule(int delay);
00115 
00118   void reschedule();
00120 
00123   inline int sleep_time() const { return _sleep_time; }
00125 
00127   inline void sleep_time(int new_sleep) { _sleep_time = new_sleep; }
00129 
00131   // these functions report on the thread state.
00132 
00133   inline bool thread_started() const { return _thread_ready; }
00135 
00137   inline bool thread_finished() const { return !_thread_ready; }
00139 
00144   inline bool thread_active() const { return _thread_active; }
00146 
00150   inline bool should_stop() const { return _stop_thread; }
00152 
00154 private:
00155   bool _thread_ready;  
00156   bool _thread_active;  
00157   bool _stop_thread;  
00158   void *_data;  
00159 #ifdef __UNIX__
00160   pthread_t *_handle;  
00161 #elif defined(__WIN32__)
00162   uintptr_t _handle;  
00163 #endif
00164   int _sleep_time;  
00165   bool _periodic;  
00166   time_stamp *_next_activation;  
00167   timed_thread_types _how;  
00168 
00169   // the OS level thread functions.
00170 #ifdef __UNIX__
00171   static void *periodic_thread_driver(void *hidden_pointer);
00172   static void *one_shot_thread_driver(void *hidden_pointer);
00173 #elif defined(__WIN32__)
00174   static void periodic_thread_driver(void *hidden_pointer);
00175   static void one_shot_thread_driver(void *hidden_pointer);
00176 #endif
00177 
00178   // forbidden.
00179   ithread(const ithread &);
00180   ithread &operator =(const ithread &);
00181 };
00182 
00184 
00185 // an older synonym for the ithread, used in legacy code.
00186 
00187 class managed_thread : public ithread
00188 {
00189 public:
00190   inline managed_thread() : ithread() {}
00191   inline managed_thread(int st, timed_thread_types how = SLACK_INTERVAL)
00192       : ithread(st, how) {}
00193 
00194   virtual void perform_activity(void *thread_data) = 0;
00195 
00196 private:
00197   // forbidden.
00198   managed_thread(const ithread &);
00199   managed_thread &operator =(const managed_thread &);
00200 };
00201 
00202 #endif
00203 

Generated on Thu Nov 20 04:28:59 2008 for HOOPLE Libraries by  doxygen 1.5.1