Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

acsThreadBase.h

Go to the documentation of this file.
00001 00002 #ifndef acsThreadBase_h 00003 #define acsThreadBase_h 00004 00005 /******************************************************************************* 00006 * ALMA - Atacama Large Millimiter Array 00007 * (c) European Southern Observatory, 2004 00008 * 00009 *This library is free software; you can redistribute it and/or 00010 *modify it under the terms of the GNU Lesser General Public 00011 *License as published by the Free Software Foundation; either 00012 *version 2.1 of the License, or (at your option) any later version. 00013 * 00014 *This library is distributed in the hope that it will be useful, 00015 *but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 *Lesser General Public License for more details. 00018 * 00019 *You should have received a copy of the GNU Lesser General Public 00020 *License along with this library; if not, write to the Free Software 00021 *Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 * 00023 * "@(#) $Id: acsThreadBase.h,v 1.32 2008/10/14 22:47:10 bjeram Exp $" 00024 * 00025 * who when what 00026 * -------- ---------- ---------------------------------------------- 00027 * almamgr 2000-12-03 created 00028 */ 00029 00035 #include <acsutil.h> 00036 #include <acsutilThreadInit.h> 00037 00038 #include <map> 00039 #include <vector> 00040 00041 #include <ace/Thread.h> 00042 #include <ace/Thread_Manager.h> 00043 #include <ace/Synch.h> 00044 00045 #include <acscommonC.h> 00046 #include <ace/SString.h> 00047 #include <logging.h> 00048 #include <loggingLoggable.h> 00049 00050 #include "acsthreadErrType.h" 00051 00052 namespace ACS { 00053 00058 TimeInterval getTime(); 00059 00105 class ThreadBase : public Logging::Loggable 00106 { 00107 public: 00108 00118 static TimeInterval defaultResponseTime; 00119 00128 static TimeInterval defaultSleepTime; 00129 00131 static ThreadBase * NullThreadBase; 00132 00134 static InitThreadFunc InitThread; 00135 00137 static DoneThreadFunc DoneThread; 00138 00139 public: 00140 00151 enum SleepReturn { 00152 SLEEP_OK=0, 00153 SLEEP_SUSPEND, 00154 SLEEP_INTERRUPTED, 00155 SLEEP_ERROR 00156 }; 00157 00179 ThreadBase(const ACE_CString& _name, ACE_Thread_Manager* _threadManager, 00180 void* _threadProcedure, void* _parameter, 00181 const TimeInterval& _responseTime=ThreadBase::defaultResponseTime, 00182 const TimeInterval& _sleepTime=ThreadBase::defaultSleepTime, 00183 const bool _create=true, 00184 const long _thrFlags= THR_NEW_LWP | THR_DETACHED, 00185 const size_t _stackSize=ACE_DEFAULT_THREAD_STACKSIZE); 00186 00199 virtual ~ThreadBase(); 00200 00212 static void setInitializers(InitThreadFunc InitThread_, DoneThreadFunc DoneThread_) { 00213 InitThread=InitThread_; 00214 DoneThread=DoneThread_; 00215 } 00216 00221 ACE_CString getName() const { return name_m; } 00222 00228 void* getThreadProcedure() const { return threadProcedure_mp; } 00229 00235 TimeInterval getResponseTime() const { return responseTime_m; } 00236 00242 void setResponseTime(const TimeInterval& _responseTime) { responseTime_m=_responseTime; }; 00243 00249 TimeInterval getSleepTime() const { return sleepTime_m; } 00250 00256 void setSleepTime(const TimeInterval& _sleepTime) { 00257 sleepTime_m=_sleepTime; 00258 }; 00259 00264 void setPriority(int _priority); 00265 00270 int getPriority(); 00271 00279 bool suspend(); 00280 00285 virtual bool resume(); 00286 00291 bool isSuspended() const { return suspendStatus_m != 0; } 00292 00297 bool isStopped() const { return stopped_m; } 00298 00304 virtual void exit() { exitRequest_m=true; } 00305 00310 bool exitRequested() const { return exitRequest_m; } 00311 00317 void setStopped() { stopped_m=true; } 00318 // !!! this can be done by ThreadBase function automatically - using wrapper method 00319 00330 bool stop( bool terminating = false ); 00331 00332 00345 bool cancel(); 00346 00355 bool terminate(); 00356 00364 bool restart(); 00365 00373 void makeTimeInterval(); 00374 00383 bool isResponding() const; 00384 00389 bool isAlive() const { return !stopped_m; } 00390 00402 bool check(); 00403 00428 SleepReturn sleep(TimeInterval timeIn100ns = 0) const; 00429 00430 00434 ACE_thread_t getThreadID() { return threadID_m; } 00435 00436 protected: 00451 bool create(const long _thrFlags= THR_NEW_LWP | THR_DETACHED); 00452 00458 virtual void yield(); 00459 00460 private: 00461 00463 void* threadProcedure_mp; 00464 00466 const void* parameter_mp; 00467 00469 TimeInterval responseTime_m; 00470 00472 TimeInterval sleepTime_m; 00473 00475 TimeInterval timeStamp_m; 00476 00478 volatile int suspendStatus_m; 00479 00481 volatile bool exitRequest_m; 00482 00484 volatile bool stopped_m; 00485 00487 ACE_CString name_m; 00488 00489 // ACE speciic 00491 ACE_thread_t threadID_m; 00492 00494 long thrFlags_m; 00495 00497 size_t stackSize_m; 00498 00500 ACE_Thread_Manager * threadManager_mp; 00501 00503 mutable ACE_Thread_Semaphore m_suspendSemaphore; 00504 mutable ACE_Thread_Semaphore m_sleepSemaphore; 00505 }; 00506 00507 00508 00510 typedef std::map<ACE_CString, ThreadBase*> ThreadMap; 00511 00512 // fwd 00519 // class Thread; 00520 00521 00527 class ThreadManagerBase 00528 { 00535 // friend class ACS::Thread; 00536 00537 public: 00538 00542 ThreadManagerBase() 00543 { 00544 threadManager_mp = ACE_Thread_Manager::instance(); 00545 } 00546 00551 ~ThreadManagerBase(); 00552 00557 int getThreadCount() const {return threads_m.size();} 00558 00564 ACE_CString getThreadName(const int pos) const { return threads_m[pos]->getName(); } 00565 00566 00572 ThreadBase* getThreadAt(const int pos) const { return static_cast<ThreadBase*>(threads_m[pos]); } 00573 00574 00580 ThreadBase* getThreadByName(const ACE_CString& name) { 00581 ACE_Guard<ACE_Recursive_Thread_Mutex> guard(m_addRemoveMutex); 00582 00583 ThreadMap::iterator i = threadMap_m.find(name); 00584 if (i!=threadMap_m.end()) 00585 return static_cast<ThreadBase*>((*i).second); 00586 else 00587 return NULL; 00588 } 00589 00595 ThreadBase* getThreadByID(ACE_thread_t id) 00596 { 00597 00598 for(unsigned int i=0UL; i < threads_m.size(); i++) 00599 if (threads_m[i]->getThreadID() == id) 00600 return threads_m[i]; 00601 return NULL; 00602 } 00603 00604 00621 ThreadBase * create(const ACE_CString& name, void * threadProc, void * parameter, 00622 const TimeInterval& responseTime=ThreadBase::defaultResponseTime, 00623 const TimeInterval& sleepTime=ThreadBase::defaultSleepTime, 00624 const long _thrFlags= THR_NEW_LWP | THR_DETACHED); 00625 00634 bool add(const ACE_CString& name, ThreadBase * acsBaseThread); 00635 00642 bool stop(const ACE_CString& name); 00643 00649 bool stopAll(); 00650 00656 void exit(const ACE_CString& name); 00657 00662 void exitAll(); 00663 00671 bool cancel(const ACE_CString& name); 00672 00677 bool cancelAll(); 00678 00684 bool terminate(const ACE_CString& name); 00685 00690 bool terminateAll(); 00691 00698 bool restart(const ACE_CString& name); 00699 00704 bool restartAll(); 00705 00711 bool restartDead(); 00712 00718 bool suspend(const ACE_CString& name); 00719 00724 bool suspendAll(); 00725 00731 bool resume(const ACE_CString& name); 00732 00737 bool resumeAll(); 00738 00745 bool isAlive(const ACE_CString& name); 00746 00753 bool areAllAlive(); 00754 00760 int join(const ACE_thread_t& tid); 00761 00766 ACE_Thread_Manager* getACEThreadManager() { return threadManager_mp; } 00767 00768 protected: 00769 00775 void add2map(const ACE_CString& name, ThreadBase* thread) 00776 { 00777 00778 threadMap_m[name]=thread; 00779 threads_m.push_back(thread); 00780 } 00781 00782 void removeFromMap(const ACE_CString& name) 00783 { 00784 ACE_Guard<ACE_Recursive_Thread_Mutex> guard(m_addRemoveMutex); 00785 00786 ThreadMap::iterator i = threadMap_m.find(name); 00787 if (i!=threadMap_m.end()) 00788 { 00789 for (std::vector<ThreadBase*>::iterator thr = threads_m.begin(); thr!=threads_m.end(); ++thr) 00790 { 00791 if (static_cast<ThreadBase*>((*i).second) == *thr) 00792 { 00793 threads_m.erase(thr); 00794 break; 00795 }//if 00796 }//for 00797 }//if 00798 threadMap_m.erase(name); 00799 }//removeFromMap 00800 00801 protected: 00803 ACE_Recursive_Thread_Mutex m_addRemoveMutex; 00804 00805 private: 00806 00808 ACE_Thread_Manager * threadManager_mp; 00809 00811 ThreadMap threadMap_m; 00812 00814 std::vector<ThreadBase*> threads_m; 00815 00819 void operator=(const ThreadManagerBase&); 00820 00824 ThreadManagerBase(const ThreadManagerBase&); 00825 00826 }; 00827 00835 class ThreadBaseParameter { 00836 00837 public: 00838 00844 ThreadBaseParameter(ThreadBase * thread, 00845 const void * parameter = 0) : 00846 thread_mp(thread), parameter_mp(parameter) {} 00847 00852 const void * getParameter() const { return parameter_mp; } 00853 00858 ThreadBase * getThreadBase() const { return thread_mp; } 00859 00864 // ThreadBase * getBACIThread() const { return getThreadBase(); } 00865 private: 00866 00868 ThreadBase * thread_mp; 00869 00871 const void * parameter_mp; 00872 }; 00873 00874 00909 class ThreadSyncGuard 00910 { 00911 public: 00912 00919 ThreadSyncGuard(ACE_Recursive_Thread_Mutex * mutex, bool block=true); 00920 00925 ~ThreadSyncGuard(); 00926 00930 void acquire(); 00931 00935 void release(); 00936 00938 ACE_Recursive_Thread_Mutex * mutex_mp; 00939 00941 bool acquired_m; 00942 }; 00943 00944 }; //namespace ACS 00945 00946 #endif 00947 00948 00949 00950 00951

Generated on Thu Apr 30 02:30:49 2009 for ACS C++ API by doxygen 1.3.8