Fawkes API  Fawkes Development Version
thread_manager.h
00001 
00002 /***************************************************************************
00003  *  thread_manager.h - Fawkes thread manager
00004  *
00005  *  Created: Thu Nov  3 19:08:23 2006 (on train to Cologne)
00006  *  Copyright  2006-2009  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #ifndef __LIBS_BASEAPP_THREAD_MANAGER_H_
00025 #define __LIBS_BASEAPP_THREAD_MANAGER_H_
00026 
00027 #include <core/threading/thread_list.h>
00028 #include <core/threading/thread_collector.h>
00029 #include <core/exception.h>
00030 #include <aspect/blocked_timing.h>
00031 #include <aspect/blocked_timing/executor.h>
00032 
00033 #include <core/utils/lock_map.h>
00034 #include <list>
00035 
00036 namespace fawkes {
00037 #if 0 /* just to make Emacs auto-indent happy */
00038 }
00039 #endif
00040 class Mutex;
00041 class WaitCondition;
00042 class ThreadInitializer;
00043 class ThreadFinalizer;
00044 
00045 class ThreadManager
00046 : public ThreadCollector,
00047   public BlockedTimingExecutor
00048 {
00049  public:
00050   ThreadManager();
00051   ThreadManager(ThreadInitializer *initializer, ThreadFinalizer *finalizer);
00052   virtual ~ThreadManager();
00053 
00054   void set_inifin(ThreadInitializer *initializer,
00055                   ThreadFinalizer *finalizer);
00056 
00057   virtual void add(ThreadList &tl)
00058   {
00059     add_maybelocked(tl, /* lock */ true);
00060   }
00061 
00062   virtual void add(Thread *t)
00063   {
00064     add_maybelocked(t, /* lock */ true);
00065   }
00066 
00067   virtual void remove(ThreadList &tl)
00068   {
00069     remove_maybelocked(tl, /* lock */ true);
00070   }
00071 
00072   virtual void remove(Thread *t)
00073   {
00074     remove_maybelocked(t, /* lock */ true);
00075   }
00076 
00077   virtual void force_remove(ThreadList &tl);
00078   virtual void force_remove(Thread *t);
00079 
00080   virtual void wakeup_and_wait(BlockedTimingAspect::WakeupHook hook,
00081                                unsigned int timeout_usec = 0);
00082   virtual void wakeup(BlockedTimingAspect::WakeupHook hook,
00083                       Barrier *barrier = 0);
00084   virtual void try_recover(std::list<std::string> &recovered_threads);
00085 
00086   virtual bool timed_threads_exist();
00087   virtual void wait_for_timed_threads();
00088   virtual void interrupt_timed_thread_wait();
00089 
00090   ThreadCollector *  aspect_collector() const;
00091 
00092  private:
00093   void internal_add_thread(Thread *t);
00094   void internal_remove_thread(Thread *t);
00095   void add_maybelocked(ThreadList &tl, bool lock);
00096   void add_maybelocked(Thread *t, bool lock);
00097   void remove_maybelocked(ThreadList &tl, bool lock);
00098   void remove_maybelocked(Thread *t, bool lock);
00099 
00100   class ThreadManagerAspectCollector : public ThreadCollector
00101   {
00102    public:
00103     ThreadManagerAspectCollector(ThreadManager *parent_manager);
00104 
00105     virtual void add(ThreadList &tl);
00106     virtual void add(Thread *t);
00107 
00108     virtual void remove(ThreadList &tl);
00109     virtual void remove(Thread *t);
00110 
00111     virtual void force_remove(ThreadList &tl);
00112     virtual void force_remove(Thread *t);
00113 
00114    private:
00115     ThreadManager *__parent_manager;
00116   };
00117 
00118  private:
00119   ThreadInitializer *__initializer;
00120   ThreadFinalizer   *__finalizer;
00121 
00122   LockMap< BlockedTimingAspect::WakeupHook, ThreadList > __threads;
00123   LockMap< BlockedTimingAspect::WakeupHook, ThreadList >::iterator __tit;
00124 
00125   ThreadList     __untimed_threads;
00126   WaitCondition *__waitcond_timedthreads;
00127 
00128   ThreadManagerAspectCollector *__aspect_collector;
00129   bool __interrupt_timed_thread_wait;
00130 
00131 };
00132 
00133 } // end namespace fawkes
00134 
00135 #endif