00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef CEYLAN_THREAD_H_
00028 #define CEYLAN_THREAD_H_
00029
00030
00031 #include "CeylanSynchronized.h"
00032 #include "CeylanRunnable.h"
00033 #include "CeylanMutex.h"
00034 #include "CeylanSystem.h"
00035 #include "CeylanFeatures.h"
00036
00037
00038 #include <string>
00039
00040
00041
00042
00043 namespace Ceylan
00044 {
00045
00046
00047
00048 namespace System
00049 {
00050
00051
00052
00054 typedef Ceylan::Uint32 ThreadCount ;
00055
00056
00057
00070 class CEYLAN_DLL Thread: public Runnable
00071 {
00072
00073
00074
00085 struct SystemSpecificThreadIdentifier ;
00086
00087
00088
00099 struct SystemSpecificThreadAttribute ;
00100
00101
00102
00113 struct SystemSpecificThreadCondition ;
00114
00115
00116
00117
00118 public:
00119
00120
00121
00122
00128 class ThreadException : public SystemException
00129 {
00130
00131 public:
00132
00133 explicit ThreadException( const std::string & message ):
00134 SystemException( message )
00135 {
00136
00137 }
00138
00139
00140 virtual ~ThreadException() throw()
00141 {
00142
00143 }
00144
00145 } ;
00146
00147
00148
00149
00157 Thread() ;
00158
00159
00160
00168 explicit Thread( const std::string & name ) ;
00169
00170
00171
00173 virtual ~Thread() throw() ;
00174
00175
00176
00194 virtual void run() ;
00195
00196
00197
00199 virtual void start() = 0 ;
00200
00201
00202
00233 virtual void askToStop() ;
00234
00235
00236
00238 SystemSpecificThreadIdentifier & id() const ;
00239
00240
00241
00243 bool isClean() const ;
00244
00245
00246
00248 bool isRunning() const ;
00249
00250
00251
00253 bool hasTerminated() const ;
00254
00255
00256
00258 bool stopDemanded() const ;
00259
00260
00261
00281 void waitUntilOver() ;
00282
00283
00284
00297 virtual const std::string toString(
00298 Ceylan::VerbosityLevels level = Ceylan::high ) const ;
00299
00300
00301
00302
00303
00304
00305
00306
00311 static ThreadCount GetNumberOfThreads() ;
00312
00313
00314
00327 static void Sleep( System::Second seconds,
00328 System::Microsecond microseconds = 0 ) ;
00329
00330
00331
00339 static void Run( Thread & thread ) ;
00340
00341
00342
00343
00374 class CEYLAN_DLL Waiter: protected Mutex
00375 {
00376
00377
00378 public:
00379
00380
00381
00392 Waiter() ;
00393
00394
00395
00397 ~Waiter() throw() ;
00398
00399
00400
00414 bool wait( System::Second seconds = 0 ) ;
00415
00416
00417
00426 bool signal() ;
00427
00428
00429
00438 bool broadcast() ;
00439
00440
00441
00442
00443 private:
00444
00445
00446
00456 Waiter( const Waiter & source ) ;
00457
00458
00459
00468 Waiter & operator = ( const Waiter & source ) ;
00469
00470
00471
00478 SystemSpecificThreadCondition * _condition ;
00479
00480
00481 } ;
00482
00483
00484
00485
00486 protected:
00487
00488
00489
00528 void cancel() ;
00529
00530
00531
00537 virtual void cleanup() ;
00538
00539
00540
00542 void setRunning( bool newRunningStatus ) ;
00543
00544
00545
00547 virtual void threadCreationFailed( int error ) ;
00548
00549
00550
00551
00552 private:
00553
00554
00555
00564 Thread( const Thread & source ) ;
00565
00566
00567
00576 Thread & operator = ( const Thread & source ) ;
00577
00578
00579
00581 SystemSpecificThreadIdentifier * _id ;
00582
00583
00584
00586 SystemSpecificThreadAttribute * _attr ;
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606 #pragma warning( push )
00607 #pragma warning( disable : 4251 )
00608
00609
00610
00611
00612
00618 Ceylan::System::Synchronized<bool> _terminated ;
00619
00620
00621
00627 Ceylan::System::Synchronized<bool> _clean ;
00628
00629
00630
00635 Ceylan::System::Synchronized<bool> _running ;
00636
00637
00638
00644 Ceylan::System::Synchronized<bool> _mustStop ;
00645
00646 #pragma warning( pop )
00647
00648
00649
00654 static Synchronized<ThreadCount> * _Number ;
00655
00656
00657 } ;
00658
00659
00660 }
00661
00662 }
00663
00664
00665
00666 #endif // CEYLAN_THREAD_H_
00667