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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 #ifndef _PTHREAD
00202 #define _PTHREAD
00203
00204 #ifdef P_USE_PRAGMA
00205 #pragma interface
00206 #endif
00207
00208 #ifdef Priority
00209 #undef Priority
00210 #endif
00211
00212 class PSemaphore;
00213
00214 #define PThreadIdentifer PThreadIdentifier
00215
00216 typedef P_THREADIDENTIFIER PThreadIdentifier;
00217
00219
00220
00234 class PThread : public PObject
00235 {
00236 PCLASSINFO(PThread, PObject);
00237
00238 public:
00241
00242 enum Priority {
00244 LowestPriority,
00245
00247 LowPriority,
00248
00250 NormalPriority,
00251
00253 HighPriority,
00254
00256 HighestPriority,
00257
00258 NumPriorities
00259 };
00260
00262 enum AutoDeleteFlag {
00264 AutoDeleteThread,
00265
00267 NoAutoDeleteThread
00268 };
00269
00292 PThread(
00293 PINDEX ,
00294 AutoDeleteFlag deletion = AutoDeleteThread,
00296 Priority priorityLevel = NormalPriority,
00297 const PString & threadName = PString::Empty()
00298 );
00299
00307 ~PThread();
00309
00316 void PrintOn(
00317 ostream & strm
00318 ) const;
00320
00328 virtual void Restart();
00329
00341 virtual void Terminate();
00342
00348 virtual BOOL IsTerminated() const;
00349
00355 void WaitForTermination() const;
00356 BOOL WaitForTermination(
00357 const PTimeInterval & maxWait
00358 ) const;
00359
00372 virtual void Suspend(
00373 BOOL susp = TRUE
00374 );
00375
00395 virtual void Resume();
00396
00404 virtual BOOL IsSuspended() const;
00405
00407 static void Sleep(
00408 const PTimeInterval & delay
00409 );
00410
00414 virtual void SetPriority(
00415 Priority priorityLevel
00416 );
00417
00423 virtual Priority GetPriority() const;
00424
00428 virtual void SetAutoDelete(
00429 AutoDeleteFlag deletion = AutoDeleteThread
00430 );
00431
00435 void SetNoAutoDelete() { SetAutoDelete(NoAutoDeleteThread); }
00436
00442 virtual PString GetThreadName() const;
00443
00449 virtual void SetThreadName(
00450 const PString & name
00451 );
00453
00461 virtual PThreadIdentifier GetThreadId() const;
00462 static PThreadIdentifier GetCurrentThreadId();
00463
00471 virtual void Main() = 0;
00472
00482 static PThread * Current();
00483
00490 static void Yield();
00491
00496 static PThread * Create(
00497 const PNotifier & notifier,
00498 INT parameter = 0,
00499 AutoDeleteFlag deletion = AutoDeleteThread,
00501 Priority priorityLevel = NormalPriority,
00502 const PString & threadName = PString::Empty(),
00503 PINDEX stackSize = 65536
00504 );
00506
00507 protected:
00508 void InitialiseProcessThread();
00509
00510
00511
00512
00513 private:
00514 PThread();
00515
00516
00517 friend class PProcess;
00518
00519
00520 PThread(const PThread &) { }
00521
00522
00523 PThread & operator=(const PThread &) { return *this; }
00524
00525
00526 BOOL autoDelete;
00527
00528
00529
00530 PString threadName;
00531
00532 private:
00533 #if PTRACING
00534 PStack<PStringStream> traceStreams;
00535 unsigned traceLevel;
00536 friend class PTrace;
00537
00538 unsigned traceBlockIndentLevel;
00539 friend class PTrace::Block;
00540 #endif
00541
00542
00543
00544 #ifdef _WIN32
00545 #include "msos/ptlib/thread.h"
00546 #else
00547 #include "unix/ptlib/thread.h"
00548 #endif
00549 };
00550
00551 #ifdef _MSC_VER
00552 #pragma warning(disable:4355)
00553 #endif
00554
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571 class PThreadMain : public PThread
00572 {
00573 PCLASSINFO(PThreadMain, PThread);
00574 public:
00575 typedef void (*FnType)();
00576 PThreadMain(FnType _fn, BOOL autoDelete = FALSE)
00577 : PThread(10000, autoDelete ? PThread::AutoDeleteThread : PThread::NoAutoDeleteThread), fn(_fn)
00578 { PThread::Resume(); }
00579 PThreadMain(const char * _file, int _line, FnType _fn, BOOL autoDelete = FALSE)
00580 : PThread(10000, autoDelete ? PThread::AutoDeleteThread : PThread::NoAutoDeleteThread, NormalPriority,
00581 psprintf("%s:%08x-%s:%i", GetClass(), (void *)this, _file, _line)), fn(_fn)
00582 { PThread::Resume(); }
00583 virtual void Main()
00584 { (*fn)(); }
00585 FnType fn;
00586 };
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600 template<typename Arg1Type>
00601 class PThread1Arg : public PThread
00602 {
00603 PCLASSINFO(PThread1Arg, PThread);
00604 public:
00605 typedef void (*FnType)(Arg1Type arg1);
00606 PThread1Arg(Arg1Type _arg1, FnType _fn, BOOL autoDelete = FALSE)
00607 : PThread(10000, autoDelete ? PThread::AutoDeleteThread : PThread::NoAutoDeleteThread), fn(_fn),
00608 arg1(_arg1)
00609 { PThread::Resume(); }
00610 PThread1Arg(const char * _file, int _line, Arg1Type _arg1, FnType _fn, BOOL autoDelete = FALSE)
00611 : PThread(10000, autoDelete ? PThread::AutoDeleteThread : PThread::NoAutoDeleteThread, NormalPriority,
00612 psprintf("%s:%08x-%s:%i", GetClass(), (void *)this, _file, _line)), fn(_fn),
00613 arg1(_arg1)
00614 { PThread::Resume(); }
00615 virtual void Main()
00616 { (*fn)(arg1); }
00617 FnType fn;
00618 Arg1Type arg1;
00619 };
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634 template<typename Arg1Type, typename Arg2Type>
00635 class PThread2Arg : public PThread
00636 {
00637 PCLASSINFO(PThread2Arg, PThread);
00638 public:
00639 typedef void (*FnType)(Arg1Type arg1, Arg2Type arg2);
00640 PThread2Arg(Arg1Type _arg1, Arg2Type _arg2, FnType _fn, BOOL autoDelete = FALSE)
00641 : PThread(10000, autoDelete ? PThread::AutoDeleteThread : PThread::NoAutoDeleteThread), fn(_fn),
00642 arg1(_arg1), arg2(_arg2)
00643 { PThread::Resume(); }
00644 PThread2Arg(const char * _file, int _line, Arg1Type _arg1, Arg2Type _arg2, FnType _fn, BOOL autoDelete = FALSE)
00645 : PThread(10000, autoDelete ? PThread::AutoDeleteThread : PThread::NoAutoDeleteThread, NormalPriority,
00646 psprintf("%s:%08x-%s:%i", GetClass(), (void *)this, _file, _line)), fn(_fn),
00647 arg1(_arg1), arg2(_arg2)
00648 { PThread::Resume(); }
00649 virtual void Main()
00650 { (*fn)(arg1, arg2); }
00651 FnType fn;
00652 Arg1Type arg1;
00653 Arg2Type arg2;
00654 };
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672 template <typename ObjType>
00673 class PThreadObj : public PThread
00674 {
00675 public:
00676 PCLASSINFO(PThreadObj, PThread);
00677 public:
00678 typedef void (ObjType::*ObjTypeFn)();
00679 PThreadObj(ObjType & _obj, ObjTypeFn _fn, BOOL autoDelete = FALSE)
00680 : PThread(10000, autoDelete ? PThread::AutoDeleteThread : PThread::NoAutoDeleteThread),
00681 obj(_obj), fn(_fn)
00682 { PThread::Resume(); }
00683 PThreadObj(const char * _file, int _line, ObjType & _obj, ObjTypeFn _fn, BOOL autoDelete = FALSE)
00684 : PThread(10000, autoDelete ? PThread::AutoDeleteThread : PThread::NoAutoDeleteThread, NormalPriority,
00685 psprintf("%s:%08x-%s:%i", GetClass(), (void *)this, _file, _line)),
00686 obj(_obj), fn(_fn)
00687 { PThread::Resume(); }
00688 void Main()
00689 { (obj.*fn)(); }
00690
00691 protected:
00692 ObjType & obj;
00693 ObjTypeFn fn;
00694 };
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713 template <class ObjType, typename Arg1Type>
00714 class PThreadObj1Arg : public PThread
00715 {
00716 PCLASSINFO(PThreadObj1Arg, PThread);
00717 public:
00718 typedef void (ObjType::*ObjTypeFn)(Arg1Type);
00719 PThreadObj1Arg(ObjType & _obj, Arg1Type _arg1, ObjTypeFn _fn, BOOL autoDelete = FALSE)
00720 : PThread(10000, autoDelete ? PThread::AutoDeleteThread : PThread::NoAutoDeleteThread),
00721 obj(_obj), fn(_fn), arg1(_arg1)
00722 { PThread::Resume(); }
00723 PThreadObj1Arg(const char * _file, int _line, ObjType & _obj, Arg1Type _arg1, ObjTypeFn _fn, BOOL autoDelete = FALSE)
00724 : PThread(10000, autoDelete ? PThread::AutoDeleteThread : PThread::NoAutoDeleteThread, NormalPriority,
00725 psprintf("%s:%08x-%s:%i", GetClass(), (void *)this, _file, _line)),
00726 obj(_obj), fn(_fn), arg1(_arg1)
00727 { PThread::Resume(); }
00728 void Main()
00729 { (obj.*fn)(arg1); }
00730
00731 protected:
00732 ObjType & obj;
00733 ObjTypeFn fn;
00734 Arg1Type arg1;
00735 };
00736
00737 #ifdef _MSC_VER
00738 #pragma warning(default:4355)
00739 #endif
00740
00741 #endif // _PTHREAD
00742
00743