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 #ifndef _PTHREAD
00177 #define _PTHREAD
00178
00179 #ifdef P_USE_PRAGMA
00180 #pragma interface
00181 #endif
00182
00183 #ifdef Priority
00184 #undef Priority
00185 #endif
00186
00187 class PSemaphore;
00188
00189 #define PThreadIdentifer PThreadIdentifier
00190
00191 typedef P_THREADIDENTIFIER PThreadIdentifier;
00192
00194
00195
00209 class PThread : public PObject
00210 {
00211 PCLASSINFO(PThread, PObject);
00212
00213 public:
00216
00217 enum Priority {
00219 LowestPriority,
00220
00222 LowPriority,
00223
00225 NormalPriority,
00226
00228 HighPriority,
00229
00231 HighestPriority,
00232
00233 NumPriorities
00234 };
00235
00237 enum AutoDeleteFlag {
00239 AutoDeleteThread,
00240
00242 NoAutoDeleteThread
00243 };
00244
00267 PThread(
00268 PINDEX ,
00269 AutoDeleteFlag deletion = AutoDeleteThread,
00271 Priority priorityLevel = NormalPriority,
00272 const PString & threadName = PString::Empty()
00273 );
00274
00282 ~PThread();
00284
00291 void PrintOn(
00292 ostream & strm
00293 ) const;
00295
00303 virtual void Restart();
00304
00316 virtual void Terminate();
00317
00323 virtual BOOL IsTerminated() const;
00324
00330 void WaitForTermination() const;
00331 BOOL WaitForTermination(
00332 const PTimeInterval & maxWait
00333 ) const;
00334
00347 virtual void Suspend(
00348 BOOL susp = TRUE
00349 );
00350
00370 virtual void Resume();
00371
00379 virtual BOOL IsSuspended() const;
00380
00382 static void Sleep(
00383 const PTimeInterval & delay
00384 );
00385
00389 virtual void SetPriority(
00390 Priority priorityLevel
00391 );
00392
00398 virtual Priority GetPriority() const;
00399
00403 virtual void SetAutoDelete(
00404 AutoDeleteFlag deletion = AutoDeleteThread
00405 );
00406
00410 void SetNoAutoDelete() { SetAutoDelete(NoAutoDeleteThread); }
00411
00417 virtual PString GetThreadName() const;
00418
00424 virtual void SetThreadName(
00425 const PString & name
00426 );
00428
00436 virtual PThreadIdentifier GetThreadId() const;
00437 static PThreadIdentifier GetCurrentThreadId();
00438
00446 virtual void Main() = 0;
00447
00457 static PThread * Current();
00458
00465 static void Yield();
00466
00471 static PThread * Create(
00472 const PNotifier & notifier,
00473 INT parameter = 0,
00474 AutoDeleteFlag deletion = AutoDeleteThread,
00476 Priority priorityLevel = NormalPriority,
00477 const PString & threadName = PString::Empty(),
00478 PINDEX stackSize = 65536
00479 );
00481
00482 protected:
00483 void InitialiseProcessThread();
00484
00485
00486
00487
00488 private:
00489 PThread();
00490
00491
00492 friend class PProcess;
00493
00494
00495 PThread(const PThread &) { }
00496
00497
00498 PThread & operator=(const PThread &) { return *this; }
00499
00500
00501 BOOL autoDelete;
00502
00503
00504
00505 PString threadName;
00506
00507 private:
00508 unsigned traceBlockIndentLevel;
00509 friend class PTrace::Block;
00510
00511
00512
00513 #ifdef _WIN32
00514 #include "msos/ptlib/thread.h"
00515 #else
00516 #include "unix/ptlib/thread.h"
00517 #endif
00518 };
00519
00524 class PThreadMain : public PThread
00525 {
00526 PCLASSINFO(PThreadMain, PThread);
00527 public:
00528 PThreadMain(BOOL autoDelete = FALSE)
00529 : PThread(10000, autoDelete ? PThread::AutoDeleteThread : PThread::NoAutoDeleteThread)
00530 { PThread::Resume(); }
00531 virtual void Main() = 0;
00532 };
00533
00534 template <class Arg1Type>
00535 class PThreadMain1Arg : public PThread
00536 {
00537 PCLASSINFO(PThreadMain1Arg, PThread);
00538 public:
00539 PThreadMain1Arg(Arg1Type _arg1, BOOL autoDelete = FALSE)
00540 : PThread(10000, autoDelete ? PThread::AutoDeleteThread : PThread::NoAutoDeleteThread),
00541 arg1(_arg1)
00542 { PThread::Resume(); }
00543 virtual void Main();
00544
00545 protected:
00546 Arg1Type arg1;
00547 };
00548
00549 template <class Arg1Type, class Arg2Type>
00550 class PThreadMain2Arg : public PThread
00551 {
00552 PCLASSINFO(PThreadMain2Arg, PThread);
00553 public:
00554 PThreadMain2Arg(Arg1Type _arg1, Arg2Type _arg2, BOOL autoDelete = FALSE)
00555 : PThread(10000, autoDelete ? PThread::AutoDeleteThread : PThread::NoAutoDeleteThread),
00556 arg1(_arg1), arg2(_arg2)
00557 { PThread::Resume(); }
00558 virtual void Main();
00559
00560 protected:
00561 Arg1Type arg1;
00562 Arg2Type arg2;
00563 };
00564
00565 template <class ObjType>
00566 class PThreadObj : public PThread
00567 {
00568 public:
00569 PCLASSINFO(PThreadObj, PThread);
00570 public:
00571 typedef void (ObjType::*ObjTypeFn)();
00572 PThreadObj(ObjType & _obj, ObjTypeFn _fn, BOOL autoDelete = FALSE)
00573 : PThread(10000, autoDelete ? PThread::AutoDeleteThread : PThread::NoAutoDeleteThread),
00574 obj(_obj), fn(_fn)
00575 { PThread::Resume(); }
00576 void Main()
00577 { (obj.*fn)(); }
00578
00579 protected:
00580 ObjType & obj;
00581 ObjTypeFn fn;
00582 };
00583
00584 template <class ObjType, class Arg1Type>
00585 class PThreadObj1Arg : public PThread
00586 {
00587 PCLASSINFO(PThreadObj1Arg, PThread);
00588 public:
00589 typedef void (ObjType::*ObjTypeFn)(int);
00590 PThreadObj1Arg(ObjType & _obj, Arg1Type _arg1, ObjTypeFn _fn, BOOL autoDelete = FALSE)
00591 : PThread(10000, autoDelete ? PThread::AutoDeleteThread : PThread::NoAutoDeleteThread),
00592 obj(_obj), fn(_fn), arg1(_arg1)
00593 { PThread::Resume(); }
00594 void Main()
00595 { (obj.*fn)(arg1); }
00596
00597 protected:
00598 ObjType & obj;
00599 ObjTypeFn fn;
00600 Arg1Type arg1;
00601 };
00602
00603 #endif // _PTHREAD
00604
00605