foobar2000 SDK  2015-01-14
threads.h
Go to the documentation of this file.
1 #ifdef _WIN32
2 #include <process.h>
3 #else
4 #include <pthread.h>
5 #endif
6 namespace pfc {
9 
11  class thread {
12  public:
13  PFC_DECLARE_EXCEPTION(exception_creation, exception, "Could not create thread");
14  thread();
15  ~thread() {PFC_ASSERT(!isActive()); waitTillDone();}
16  void startWithPriority(int priority);
17  void setPriority(int priority);
18  int getPriority();
19  void start();
20  bool isActive() const;
21  void waitTillDone() {close();}
22  static int currentPriority();
23 #ifdef _WINDOWS
24  void winStart(int priority, DWORD * outThreadID);
26 #endif
27  protected:
28  virtual void threadProc() {PFC_ASSERT(!"Stub thread entry - should not get here");}
29  private:
30  void close();
31 #ifdef _WINDOWS
32  static unsigned CALLBACK g_entry(void* p_instance);
33 #else
34  static void * g_entry( void * arg );
35 #endif
36  void entry();
37 
38 #ifdef _WINDOWS
40 #else
41  pthread_t m_thread;
42  bool m_threadValid; // there is no invalid pthread_t, so we keep a separate 'valid' flag
43 #endif
44 
45 #ifdef __APPLE__
46  static void appleStartThreadPrologue();
47 #endif
48  PFC_CLASS_NOT_COPYABLE_EX(thread)
49  };
50 }
pthread_t m_thread
Definition: threads.h:41
void start()
Definition: threads.cpp:91
HANDLE winThreadHandle()
Definition: threads.h:25
t_size getOptimalWorkerThreadCountEx(t_size taskCountLimit)
Definition: threads.cpp:45
void startWithPriority(int priority)
Definition: threads.cpp:84
void close()
Definition: threads.cpp:62
t_size getOptimalWorkerThreadCount()
Definition: threads.cpp:17
bool isActive() const
Definition: threads.cpp:72
typedef HANDLE(WINAPI *pPowerCreateRequest_t)(__in void *Context)
IMPORTANT: all classes derived from thread must call waitTillDone() in their destructor, to avoid object destruction during a virtual function call!
Definition: threads.h:11
size_t t_size
Definition: int_types.h:48
PFC_DECLARE_EXCEPTION(exception_creation, exception,"Could not create thread")
void setPriority(int priority)
Definition: threads.cpp:87
virtual void threadProc()
Definition: threads.h:28
HANDLE m_thread
Definition: threads.h:39
std::exception exception
Definition: primitives.h:193
int getPriority()
Definition: threads.cpp:99
static unsigned CALLBACK g_entry(void *p_instance)
Definition: obj-c.mm:22
bool m_threadValid
Definition: threads.h:42
void waitTillDone()
Definition: threads.h:21
static int currentPriority()
Definition: threads.cpp:104
static void appleStartThreadPrologue()
Definition: obj-c.mm:28
void winStart(int priority, DWORD *outThreadID)
Definition: threads.cpp:75
void entry()
Definition: threads.cpp:51