foobar2000 SDK  2015-08-03
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes
pfc::thread

#include <threads.h>

+ Inheritance diagram for pfc::thread:

Public Member Functions

 thread ()
 
 ~thread ()
 
int getPriority ()
 
bool isActive () const
 
 PFC_DECLARE_EXCEPTION (exception_creation, exception,"Could not create thread")
 
pthread_t posixThreadHandle ()
 
void setPriority (int priority)
 
void start ()
 
void startWithPriority (int priority)
 
void waitTillDone ()
 
void winStart (int priority, DWORD *outThreadID)
 
HANDLE winThreadHandle ()
 

Static Public Member Functions

static int currentPriority ()
 

Protected Member Functions

virtual void threadProc ()
 

Private Member Functions

void close ()
 
void entry ()
 

Static Private Member Functions

static void appleStartThreadPrologue ()
 
static unsigned CALLBACK g_entry (void *p_instance)
 
static void * g_entry (void *arg)
 

Private Attributes

HANDLE m_thread
 
pthread_t m_thread
 
bool m_threadValid
 

Detailed Description

IMPORTANT: all classes derived from thread must call waitTillDone() in their destructor, to avoid object destruction during a virtual function call!

Definition at line 11 of file threads.h.

Constructor & Destructor Documentation

pfc::thread::thread ( )

Definition at line 56 of file threads.cpp.

56  : m_thread(INVALID_HANDLE_VALUE)
57  {
58  }
HANDLE m_thread
Definition: threads.h:41
pfc::thread::~thread ( )
inline

Definition at line 15 of file threads.h.

15 {PFC_ASSERT(!isActive()); waitTillDone();}
bool isActive() const
Definition: threads.cpp:70
void waitTillDone()
Definition: threads.h:21

Member Function Documentation

void pfc::thread::appleStartThreadPrologue ( )
staticprivate

Definition at line 28 of file obj-c.mm.

28  {
29  if (![NSThread isMultiThreaded]) [[[NSThread alloc] init] start];
30  }
void start()
Definition: threads.cpp:93
void pfc::thread::close ( )
private

Definition at line 60 of file threads.cpp.

60  {
61  if (isActive()) {
62 
63  int ctxPriority = GetThreadPriority( GetCurrentThread() );
64  if (ctxPriority > GetThreadPriority( m_thread ) ) SetThreadPriority( m_thread, ctxPriority );
65 
66  if (WaitForSingleObject(m_thread,INFINITE) != WAIT_OBJECT_0) crash();
67  CloseHandle(m_thread); m_thread = INVALID_HANDLE_VALUE;
68  }
69  }
bool isActive() const
Definition: threads.cpp:70
DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
Definition: pp-winapi.h:16
void crash()
Definition: other.cpp:112
HANDLE m_thread
Definition: threads.h:41
int pfc::thread::currentPriority ( )
static

Definition at line 106 of file threads.cpp.

106  {
107  return GetThreadPriority( GetCurrentThread() );
108  }
void pfc::thread::entry ( )
private

Definition at line 49 of file threads.cpp.

49  {
50  try {
51  threadProc();
52  } catch(...) {}
53  }
virtual void threadProc()
Definition: threads.h:30
void * pfc::thread::g_entry ( void *  p_instance)
staticprivate

Definition at line 22 of file obj-c.mm.

22  {
23  @autoreleasepool {
24  reinterpret_cast<thread*>(arg)->entry();
25  }
26  return NULL;
27  }
void entry()
Definition: threads.cpp:49
static void* pfc::thread::g_entry ( void *  arg)
staticprivate
int pfc::thread::getPriority ( )

Definition at line 101 of file threads.cpp.

101  {
102  PFC_ASSERT(isActive());
103  return GetThreadPriority( m_thread );
104  }
bool isActive() const
Definition: threads.cpp:70
HANDLE m_thread
Definition: threads.h:41
bool pfc::thread::isActive ( ) const

Definition at line 70 of file threads.cpp.

70  {
71  return m_thread != INVALID_HANDLE_VALUE;
72  }
HANDLE m_thread
Definition: threads.h:41
pfc::thread::PFC_DECLARE_EXCEPTION ( exception_creation  ,
exception  ,
"Could not create thread  
)
pthread_t pfc::thread::posixThreadHandle ( )
inline

Definition at line 27 of file threads.h.

27 { return m_thread; }
HANDLE m_thread
Definition: threads.h:41
void pfc::thread::setPriority ( int  priority)

Definition at line 89 of file threads.cpp.

89  {
90  PFC_ASSERT(isActive());
91  SetThreadPriority(m_thread, priority);
92  }
bool isActive() const
Definition: threads.cpp:70
HANDLE m_thread
Definition: threads.h:41
void pfc::thread::start ( )

Definition at line 93 of file threads.cpp.

93  {
94  startWithPriority(GetThreadPriority(GetCurrentThread()));
95  }
void startWithPriority(int priority)
Definition: threads.cpp:86
void pfc::thread::startWithPriority ( int  priority)

Definition at line 86 of file threads.cpp.

86  {
87  winStart(priority, NULL);
88  }
void winStart(int priority, DWORD *outThreadID)
Definition: threads.cpp:73
virtual void pfc::thread::threadProc ( )
inlineprotectedvirtual

Reimplemented in CSimpleThread, and CVerySimpleThread.

Definition at line 30 of file threads.h.

30 {PFC_ASSERT(!"Stub thread entry - should not get here");}
void pfc::thread::waitTillDone ( )
inline

Definition at line 21 of file threads.h.

21 {close();}
void close()
Definition: threads.cpp:60
void pfc::thread::winStart ( int  priority,
DWORD *  outThreadID 
)

Definition at line 73 of file threads.cpp.

73  {
74  close();
75  HANDLE thread;
76 #ifdef PFC_WINDOWS_DESKTOP_APP
77  thread = (HANDLE)_beginthreadex(NULL, 0, g_entry, reinterpret_cast<void*>(this), CREATE_SUSPENDED, (unsigned int*)outThreadID);
78 #else
79  thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) g_entry, reinterpret_cast<void*>(this), CREATE_SUSPENDED, outThreadID);
80 #endif
81  if (thread == NULL) throw exception_creation();
82  SetThreadPriority(thread, priority);
83  ResumeThread(thread);
84  m_thread = thread;
85  }
void close()
Definition: threads.cpp:60
typedef HANDLE(WINAPI *pPowerCreateRequest_t)(__in void *Context)
HANDLE m_thread
Definition: threads.h:41
static unsigned CALLBACK g_entry(void *p_instance)
Definition: obj-c.mm:22
HANDLE pfc::thread::winThreadHandle ( )
inline

Definition at line 25 of file threads.h.

25 { return m_thread; }
HANDLE m_thread
Definition: threads.h:41

Field Documentation

HANDLE pfc::thread::m_thread
private

Definition at line 41 of file threads.h.

pthread_t pfc::thread::m_thread
private

Definition at line 43 of file threads.h.

bool pfc::thread::m_threadValid
private

Definition at line 44 of file threads.h.


The documentation for this class was generated from the following files: