foobar2000 SDK  2015-01-14
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")
 
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 58 of file threads.cpp.

58  : m_thread(INVALID_HANDLE_VALUE)
59  {
60  }
HANDLE m_thread
Definition: threads.h:39
pfc::thread::~thread ( )
inline

Definition at line 15 of file threads.h.

15 {PFC_ASSERT(!isActive()); waitTillDone();}
bool isActive() const
Definition: threads.cpp:72
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:91
void pfc::thread::close ( )
private

Definition at line 62 of file threads.cpp.

62  {
63  if (isActive()) {
64 
65  int ctxPriority = GetThreadPriority( GetCurrentThread() );
66  if (ctxPriority > GetThreadPriority( m_thread ) ) SetThreadPriority( m_thread, ctxPriority );
67 
68  if (WaitForSingleObject(m_thread,INFINITE) != WAIT_OBJECT_0) crash();
69  CloseHandle(m_thread); m_thread = INVALID_HANDLE_VALUE;
70  }
71  }
bool isActive() const
Definition: threads.cpp:72
void crash()
Definition: other.cpp:105
HANDLE m_thread
Definition: threads.h:39
int pfc::thread::currentPriority ( )
static

Definition at line 104 of file threads.cpp.

104  {
105  return GetThreadPriority( GetCurrentThread() );
106  }
void pfc::thread::entry ( )
private

Definition at line 51 of file threads.cpp.

51  {
52  try {
53  threadProc();
54  } catch(...) {}
55  }
virtual void threadProc()
Definition: threads.h:28
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:51
static void* pfc::thread::g_entry ( void *  arg)
staticprivate
int pfc::thread::getPriority ( )

Definition at line 99 of file threads.cpp.

99  {
100  PFC_ASSERT(isActive());
101  return GetThreadPriority( m_thread );
102  }
bool isActive() const
Definition: threads.cpp:72
HANDLE m_thread
Definition: threads.h:39
bool pfc::thread::isActive ( ) const

Definition at line 72 of file threads.cpp.

72  {
73  return m_thread != INVALID_HANDLE_VALUE;
74  }
HANDLE m_thread
Definition: threads.h:39
pfc::thread::PFC_DECLARE_EXCEPTION ( exception_creation  ,
exception  ,
"Could not create thread  
)
void pfc::thread::setPriority ( int  priority)

Definition at line 87 of file threads.cpp.

87  {
88  PFC_ASSERT(isActive());
89  SetThreadPriority(m_thread, priority);
90  }
bool isActive() const
Definition: threads.cpp:72
HANDLE m_thread
Definition: threads.h:39
void pfc::thread::start ( )

Definition at line 91 of file threads.cpp.

91  {
92  startWithPriority(GetThreadPriority(GetCurrentThread()));
93  }
void startWithPriority(int priority)
Definition: threads.cpp:84
void pfc::thread::startWithPriority ( int  priority)

Definition at line 84 of file threads.cpp.

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

Reimplemented in CSimpleThread, and CVerySimpleThread.

Definition at line 28 of file threads.h.

28 {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:62
void pfc::thread::winStart ( int  priority,
DWORD *  outThreadID 
)

Definition at line 75 of file threads.cpp.

75  {
76  close();
77  HANDLE thread;
78  thread = (HANDLE)_beginthreadex(NULL, 0, g_entry, reinterpret_cast<void*>(this), CREATE_SUSPENDED, (unsigned int*)outThreadID);
79  if (thread == NULL) throw exception_creation();
80  SetThreadPriority(thread, priority);
81  ResumeThread(thread);
82  m_thread = thread;
83  }
void close()
Definition: threads.cpp:62
typedef HANDLE(WINAPI *pPowerCreateRequest_t)(__in void *Context)
HANDLE m_thread
Definition: threads.h:39
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:39

Field Documentation

HANDLE pfc::thread::m_thread
private

Definition at line 39 of file threads.h.

pthread_t pfc::thread::m_thread
private

Definition at line 41 of file threads.h.

bool pfc::thread::m_threadValid
private

Definition at line 42 of file threads.h.


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