wefts_thread.h

Go to the documentation of this file.
00001 /* 00002 wefts_thread.h 00003 Main thread class 00004 00005 $Id: wefts_thread.h,v 1.20 2004/03/28 21:45:40 jonnymind Exp $ 00006 --------------------------------------------- 00007 Begin : 2003-08-02 23:45 00008 Author : Giancarlo Niccolai 00009 00010 Last modified because: 00011 Implementing OS independent base threading 00012 */ 00013 00014 /************************************************************************** 00015 * This program is free software; you can redistribute it and/or modify * 00016 * it under the terms of the GNU Library General Public License as * 00017 * published by the Free Software Foundation; either version 2.1 of the * 00018 * License, or (at your option) any later version. * 00019 ***************************************************************************/ 00020 00021 #ifndef WT_THREAD_H 00022 #define WT_THREAD_H 00023 00024 #include <wefts_os.h> 00025 #include <wefts_counter.h> 00026 #include <wefts_cleanup.h> 00027 #include <stack> 00028 00029 namespace Wefts { 00030 00037 extern Counter runningThreads; 00038 00049 Counter &startedThreads(); 00050 00051 00052 00143 class Thread 00144 { 00145 private: 00157 inline virtual void executionEnd() { m_running = false; } 00158 00160 void setCancel() { setCancel( m_canCancel ); } 00161 00163 CleanupList m_cleanupHandlers; 00164 00165 friend void s_ThreadCleanupper( void *); 00166 friend void *s_ThreadRunner( void *); 00167 friend void *s_ThreadRunFunc( void *); 00168 00170 OSPriority m_reqPriority; 00171 00173 int m_reqBoost; 00174 00176 Mutex m_guard; 00177 00179 void *m_runReturn; 00180 00181 protected: 00183 OSThread m_thread; 00185 bool m_canCancel; 00187 void *m_thread_data; 00189 bool m_detached; 00191 bool m_stopped; 00193 bool m_wasCanceled; 00194 00198 volatile bool m_running; 00199 00203 int m_thSequence; 00204 00212 void testCancel() { 00213 m_thread.testCancel(); //if available, allow OS deferred cancellation to take control 00214 // or use our kind cancellation. 00215 m_guard.lock(); 00216 if ( m_thread.same() && m_stopped ) { 00217 m_guard.unlock(); 00218 m_thread.exit(0); 00219 } 00220 m_guard.unlock(); 00221 } 00222 00230 void setCancel( bool cando ); 00231 00243 void doDetach() { m_thread.detach(); } 00244 00245 00246 public: 00277 Thread( OSPriority prio = PrioNormal, int boost = 0); 00278 00329 virtual ~Thread(); 00330 00339 bool start( bool detachable = false, bool cancellable = true ); 00340 00361 bool stop(); 00362 00376 void detach() { 00377 m_guard.lock(); 00378 // used to allow cleanup routines that delete must be called on this. 00379 m_detached = true; 00380 doDetach(); 00381 m_guard.unlock(); 00382 } 00383 00385 bool detached() { 00386 volatile bool ret; 00387 m_guard.lock(); 00388 ret = m_detached; 00389 m_guard.unlock(); 00390 return ret; 00391 } 00392 00423 virtual void *join() throw( NotJoinableError ); 00424 00436 bool running() { 00437 bool ret; 00438 m_guard.lock(); 00439 ret = m_running; 00440 m_guard.unlock(); 00441 return ret; 00442 } 00443 00445 OSThread *getThread() { return &m_thread; } 00446 00448 inline void setData( void *data ) { 00449 m_guard.lock(); 00450 m_thread_data = data; 00451 m_guard.unlock(); 00452 } 00453 00461 inline int sequence() const { return m_thSequence; } 00462 00489 virtual void cleanup(); 00490 00505 bool pushCleanup( CleanupHandler *handler, int value=0 ) 00506 { 00507 //avoid pushing a cleanup handler while the thread is diyng 00508 m_guard.lock(); 00509 if ( m_stopped && ! m_running ) { 00510 m_guard.unlock(); 00511 return false; 00512 } 00513 // don't push "this" as caller: we'll directly send "this" at cleanup. 00514 m_cleanupHandlers.push_front( CleanupItem( handler, value ) ); 00515 m_guard.unlock(); 00516 return true; 00517 } 00518 00532 bool popCleanup( bool execute=false ); 00533 00541 bool popCleanup( CleanupHandler *handler, bool execute=false ); 00542 00550 bool priority( OSPriority prio, int boost = 0 ); 00551 00588 virtual void *run()=0; 00589 00593 void *getRunReturn() { return m_runReturn; } 00594 }; 00595 00596 } 00597 00598 #endif 00599 /* end of wefts_thread.h */

Generated on Tue Oct 5 14:57:00 2004 for Wefts by doxygen 1.3.7