foobar2000 SDK  2015-08-03
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 _WIN32
24  void winStart(int priority, DWORD * outThreadID);
26 #else
27  pthread_t posixThreadHandle() { return m_thread; }
28 #endif
29  protected:
30  virtual void threadProc() {PFC_ASSERT(!"Stub thread entry - should not get here");}
31  private:
32  void close();
33 #ifdef _WIN32
34  static unsigned CALLBACK g_entry(void* p_instance);
35 #else
36  static void * g_entry( void * arg );
37 #endif
38  void entry();
39 
40 #ifdef _WIN32
42 #else
43  pthread_t m_thread;
44  bool m_threadValid; // there is no invalid pthread_t, so we keep a separate 'valid' flag
45 #endif
46 
47 #ifdef __APPLE__
48  static void appleStartThreadPrologue();
49 #endif
50  PFC_CLASS_NOT_COPYABLE_EX(thread)
51  };
52 }
pthread_t m_thread
Definition: threads.h:43
void start()
Definition: threads.cpp:93
HANDLE winThreadHandle()
Definition: threads.h:25
t_size getOptimalWorkerThreadCountEx(t_size taskCountLimit)
Definition: threads.cpp:43
void startWithPriority(int priority)
Definition: threads.cpp:86
void close()
Definition: threads.cpp:60
t_size getOptimalWorkerThreadCount()
Definition: threads.cpp:17
bool isActive() const
Definition: threads.cpp:70
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:89
virtual void threadProc()
Definition: threads.h:30
pthread_t posixThreadHandle()
Definition: threads.h:27
HANDLE m_thread
Definition: threads.h:41
std::exception exception
Definition: primitives.h:193
int getPriority()
Definition: threads.cpp:101
static unsigned CALLBACK g_entry(void *p_instance)
Definition: obj-c.mm:22
bool m_threadValid
Definition: threads.h:44
void waitTillDone()
Definition: threads.h:21
static int currentPriority()
Definition: threads.cpp:106
static void appleStartThreadPrologue()
Definition: obj-c.mm:28
void winStart(int priority, DWORD *outThreadID)
Definition: threads.cpp:73
void entry()
Definition: threads.cpp:49