00001 /* 00002 FALCON - The Falcon Programming Language. 00003 FILE: mt.h 00004 00005 Multithreaded extensions. 00006 ------------------------------------------------------------------- 00007 Author: Giancarlo Niccolai 00008 Begin: Sat, 13 Dec 2008 13:08:38 +0100 00009 00010 ------------------------------------------------------------------- 00011 (C) Copyright 2008: the FALCON developers (see list in AUTHORS file) 00012 00013 See LICENSE file for licensing details. 00014 */ 00015 00016 00017 #ifndef FALCON_MT_H 00018 #define FALCON_MT_H 00019 00020 #include <falcon/setup.h> 00021 #include <falcon/types.h> 00022 #include <falcon/basealloc.h> 00023 00024 #ifdef FALCON_SYSTEM_WIN 00025 #include <falcon/mt_win.h> 00026 #else 00027 #include <falcon/mt_posix.h> 00028 #endif 00029 00030 namespace Falcon { 00031 00037 class FALCON_DYN_CLASS Runnable 00038 { 00039 public: 00040 virtual ~Runnable() {}; 00041 virtual void* run() = 0; 00042 }; 00043 00045 class FALCON_DYN_CLASS ThreadParams: public BaseAlloc 00046 { 00047 uint32 m_stackSize; 00048 bool m_bDetached; 00049 00050 public: 00051 ThreadParams(): 00052 m_stackSize(0), 00053 m_bDetached( false ) 00054 {} 00055 00056 ThreadParams &stackSize( uint32 size ) { m_stackSize = size; return *this; } 00057 ThreadParams &detached( bool setting ) { m_bDetached = setting; return *this; } 00058 00059 uint32 stackSize() const { return m_stackSize; } 00060 bool detached() const { return m_bDetached; } 00061 }; 00062 00063 struct SYSTH_DATA; 00064 00086 class FALCON_DYN_CLASS SysThread: public BaseAlloc 00087 { 00088 struct SYSTH_DATA* m_sysdata; 00089 00090 protected: 00091 Runnable* m_runnable; 00092 virtual ~SysThread(); 00093 00094 public: 00095 SysThread( Runnable* r = 0 ); 00096 00100 void attachToCurrent(); 00101 00107 bool start( const ThreadParams ¶ms ); 00108 00114 bool start() { return start( ThreadParams() ); } 00115 00129 void detach(); 00130 00142 bool join( void* &result ); 00143 00148 uint64 getID(); 00149 00154 static uint64 getCurrentID(); 00155 00157 bool isCurrentThread(); 00158 00160 bool equal( const SysThread *th1 ) const; 00161 00162 static bool equal( const SysThread *th1, const SysThread *th2 ) { 00163 return th1->equal( th2 ); 00164 } 00165 00172 virtual void* run(); 00173 00174 SYSTH_DATA *sysdata() const { return m_sysdata; } 00175 00176 static void *RunAThread(void *); 00177 00179 void disengage(); 00180 }; 00181 00182 } 00183 00184 #endif 00185 00186 /* end of mt.h */