34 #ifndef PTLIB_THREAD_H
35 #define PTLIB_THREAD_H
316 virtual void Main() = 0;
348 PINDEX stackSize = 65536
387 { traceBlockIndentLevel = 0; }
391 unsigned traceBlockIndentLevel;
394 #ifndef P_HAS_THREADLOCAL_STORAGE
398 #endif // P_HAS_THREADLOCAL_STORAGE
403 #include "msos/ptlib/thread.h"
405 #include "unix/ptlib/thread.h"
410 #if defined(_WIN32) && !defined(_WIN32_WCE)
411 #define PTHREAD_ID_FMT "%u"
413 #define PTHREAD_ID_FMT "0x%lx"
417 #pragma warning(disable:4355)
469 template<
typename Arg1Type>
488 { (*m_function)(
m_arg1); }
508 template<
typename Arg1Type,
typename Arg2Type>
513 typedef void (*
FnType)(Arg1Type arg1, Arg2Type arg2);
520 PThread2Arg(
const char * file,
int line, Arg1Type arg1, Arg2Type arg2,
FnType function,
bool autoDel =
false)
548 template<
typename Arg1Type,
typename Arg2Type,
typename Arg3Type>
553 typedef void (*
FnType)(Arg1Type arg1, Arg2Type arg2, Arg3Type arg3);
561 PThread3Arg(
const char * file,
int line, Arg1Type arg1, Arg2Type arg2, Arg3Type arg3,
FnType function,
bool autoDel =
false)
595 template <
typename ObjType>
605 bool autoDel =
false,
606 const char * name = NULL,
645 template <
class ObjType,
typename Arg1Type>
656 bool autoDel =
false,
657 const char * name = NULL,
681 template <
class ObjType,
typename Arg1Type,
typename Arg2Type>
693 bool autoDel =
false,
694 const char * name = NULL,
728 #define P_HAS_THREADLOCAL_STORAGE 1
730 template <
class Storage_T>
731 class PThreadLocalStorage
735 typedef Storage_T value_type;
737 PThreadLocalStorage()
738 { key = TlsAlloc(); }
740 ~PThreadLocalStorage()
747 {
return (value_type *) TlsGetValue(key); }
749 void Set(value_type * v)
750 { TlsSetValue(key, (LPVOID)v); }
756 #elif defined(P_PTHREADS)
760 #define P_HAS_THREADLOCAL_STORAGE 1
762 template <
class Storage_T>
763 class PThreadLocalStorage
766 typedef pthread_key_t Key_T;
767 typedef Storage_T value_type;
769 PThreadLocalStorage()
770 { pthread_key_create(&key, NULL); }
772 ~PThreadLocalStorage()
773 { pthread_key_delete(key); }
779 {
return (value_type *)pthread_getspecific(key); }
781 void Set(value_type * v)
782 { pthread_setspecific(key, v); }
790 #undef P_HAS_THREADLOCAL_STORAGE 1
791 #warning("Thread local storage not supported");
797 #pragma warning(default:4355)
800 #endif // PTLIB_THREAD_H
Runs approximately twice as often as normal.
Definition: thread.h:81
Arg2Type m_arg2
Definition: thread.h:717
void WaitForTermination() const
Block and wait for the thread to terminate.
AutoDeleteFlag
Codes for thread autodelete flag.
Definition: thread.h:89
PThread1Arg(Arg1Type arg1, FnType function, bool autoDel=false)
Definition: thread.h:476
This class defines a thread synchronisation object.
Definition: semaphor.h:78
virtual PString GetThreadName() const
Get the name of the thread.
Class to encapsulate tracing functions.
Definition: object.h:292
Arg1Type m_arg1
Definition: thread.h:532
Times for execution of the thread.
Definition: thread.h:295
FnType m_function
Definition: thread.h:573
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
Will only run if all other threads are blocked.
Definition: thread.h:75
Runs approximately half as often as normal.
Definition: thread.h:77
virtual PBoolean IsTerminated() const
Determine if the thread has been terminated or ran to completion.
virtual void Main()
User override function for the main execution routine of the thread.
Definition: thread.h:450
Class specialisation for PNotifierTemplate
void(ObjType::* ObjTypeFn)(Arg1Type, Arg2Type)
Definition: thread.h:686
PThread3Arg(Arg1Type arg1, Arg2Type arg2, Arg3Type arg3, FnType function, bool autoDel=false)
Definition: thread.h:554
void(* FnType)()
Definition: thread.h:440
static PThread * Create(const PNotifier ¬ifier, const PString &threadName)
Definition: thread.h:350
Arg1Type m_arg1
Definition: thread.h:716
~PThread()
Destroy the thread, this simply calls the Terminate() function with all its restrictions and penaltie...
void(ObjType::* ObjTypeFn)(Arg1Type)
Definition: thread.h:650
Arg2Type m_arg3
Definition: thread.h:576
void(* FnType)(Arg1Type arg1, Arg2Type arg2)
Definition: thread.h:513
friend class PHouseKeepingThread
Definition: thread.h:364
Arg2Type m_arg2
Definition: thread.h:575
virtual void Main()=0
User override function for the main execution routine of the thread.
Arg1Type m_arg1
Definition: thread.h:492
ObjTypeFn m_function
Definition: thread.h:677
virtual void Resume()
Resume thread execution, this is identical to Suspend(false).
FnType m_function
Definition: thread.h:531
PThreadMain(FnType function, bool autoDel=false)
Definition: thread.h:441
virtual void Suspend(PBoolean susp=true)
Suspend or resume the thread.
Is only thread that will run, unless blocked.
Definition: thread.h:83
Priority
Codes for thread priorities.
Definition: thread.h:74
Define some templates to simplify the declaration of simple PThread descendants with one or two param...
Definition: thread.h:436
bool m_autoDelete
Definition: thread.h:375
PThreadMain(const char *file, int line, FnType function, bool autoDel=false)
Definition: thread.h:445
Arg1Type m_arg1
Definition: thread.h:678
void PrintOn(ostream &strm) const
Standard stream print function.
static void Sleep(const PTimeInterval &delay)
Suspend the current thread for the specified amount of time.
virtual void Main()
User override function for the main execution routine of the thread.
Definition: thread.h:487
virtual void Terminate()
Terminate the thread.
This class represents an operating system process.
Definition: pprocess.h:227
PString m_threadName
Definition: thread.h:378
BOOL PBoolean
Definition: object.h:102
virtual PThreadIdentifier GetThreadId() const
Get operating system specific thread identifier for this thread.
Definition: thread.h:291
ObjType & m_object
Definition: thread.h:624
virtual const char * GetClass(unsigned ancestor=0) const
Get the current dynamic type of the object instance.
Definition: object.h:1158
void(* FnType)(Arg1Type arg1)
Definition: thread.h:474
ObjType & m_object
Definition: thread.h:714
virtual void SetAutoDelete(AutoDeleteFlag deletion=AutoDeleteThread)
Set the flag indicating thread object is to be automatically deleted when the thread ends...
bool m_isProcess
Definition: thread.h:374
PThreadObj(ObjType &obj, ObjTypeFn function, bool autoDel=false, const char *name=NULL, PThread::Priority priority=PThread::NormalPriority)
Definition: thread.h:602
PThread2Arg(const char *file, int line, Arg1Type arg1, Arg2Type arg2, FnType function, bool autoDel=false)
Definition: thread.h:520
The character string class.
Definition: pstring.h:108
static void Yield()
Yield to another thread without blocking.
FnType m_function
Definition: thread.h:454
PTimeInterval m_kernel
Total kernel CPU time in milliseconds.
Definition: thread.h:298
static PString Empty()
Return an empty string.
Don't delete thread as it may not be on heap.
Definition: thread.h:94
virtual void Main()
User override function for the main execution routine of the thread.
Definition: thread.h:569
PThread3Arg(const char *file, int line, Arg1Type arg1, Arg2Type arg2, Arg3Type arg3, FnType function, bool autoDel=false)
Definition: thread.h:561
void SetNoAutoDelete()
Reet the flag indicating thread object is to be automatically deleted when the thread ends...
Definition: thread.h:265
This class defines a thread of execution in the system.
Definition: thread.h:66
PTimeInterval m_real
Total real time since thread start in milliseconds.
Definition: thread.h:297
virtual void Main()
User override function for the main execution routine of the thread.
Definition: thread.h:527
PMutex m_threadNameMutex
Definition: thread.h:379
Arg2Type m_arg2
Definition: thread.h:533
bool GetTimes(Times ×)
Get the thread execution times.
static PThreadIdentifier GetCurrentThreadId()
void Main()
User override function for the main execution routine of the thread.
Definition: thread.h:618
static PThread * Create(const PNotifier ¬ifier, INT parameter=0, AutoDeleteFlag deletion=AutoDeleteThread, Priority priorityLevel=NormalPriority, const PString &threadName=PString::Empty(), PINDEX stackSize=65536)
Create a simple thread executing the specified notifier.
void Main()
User override function for the main execution routine of the thread.
Definition: thread.h:670
This template class maps the PAbstractList to a specific object type, and adds functionality that all...
Definition: lists.h:582
bool IsAutoDelete() const
Definition: thread.h:356
void(ObjType::* ObjTypeFn)()
Definition: thread.h:600
static PThread * Current()
Get the currently running thread object instance.
virtual void Restart()
Restart a terminated thread using the same stack priority etc that was current when the thread termin...
Automatically delete thread object on termination.
Definition: thread.h:91
virtual PBoolean IsSuspended() const
Determine if the thread is currently suspended.
void(* FnType)(Arg1Type arg1, Arg2Type arg2, Arg3Type arg3)
Definition: thread.h:553
PINDEX m_originalStackSize
Definition: thread.h:376
virtual void SetThreadName(const PString &name)
Change the name of the thread.
PThreadObj1Arg(ObjType &obj, Arg1Type arg1, ObjTypeFn function, bool autoDel=false, const char *name=NULL, PThread::Priority priority=PThread::NormalPriority)
Definition: thread.h:652
virtual Priority GetPriority() const
Get the current priority of the thread in the current process.
void Main()
User override function for the main execution routine of the thread.
Definition: thread.h:708
PThread(PINDEX, AutoDeleteFlag deletion=AutoDeleteThread, Priority priorityLevel=NormalPriority, const PString &threadName=PString::Empty())
Create a new thread instance.
PString psprintf(const char *fmt,...)
The same as the standard C snprintf(fmt, 1000, ...), but returns a PString instead of a const char *...
ObjType & m_object
Definition: thread.h:676
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
Normal priority for a thread.
Definition: thread.h:79
Arg1Type m_arg1
Definition: thread.h:574
friend class PExternalThread
Definition: thread.h:363
PThreadObj2Arg(ObjType &obj, Arg1Type arg1, Arg2Type arg2, ObjTypeFn function, bool autoDel=false, const char *name=NULL, PThread::Priority priority=PThread::NormalPriority)
Definition: thread.h:688
PTimeInterval m_user
Total user CPU time in milliseconds.
Definition: thread.h:299
PThread2Arg(Arg1Type arg1, Arg2Type arg2, FnType function, bool autoDel=false)
Definition: thread.h:514
friend ostream & operator<<(ostream &strm, const Times ×)
PThreadIdentifier m_threadId
Definition: thread.h:381
virtual void SetPriority(Priority priorityLevel)
Set the priority of the thread relative to other threads in the current process.
ObjTypeFn m_function
Definition: thread.h:715
ObjTypeFn m_function
Definition: thread.h:625
PThread1Arg(const char *file, int line, Arg1Type arg1, FnType function, bool autoDel=false)
Definition: thread.h:481
FnType m_function
Definition: thread.h:491