00001 namespace pfc {
00002 t_size getOptimalWorkerThreadCount();
00003 t_size getOptimalWorkerThreadCountEx(t_size taskCountLimit);
00004
00005 #ifdef _WINDOWS
00007 class thread {
00008 public:
00009 PFC_DECLARE_EXCEPTION(exception_creation, exception, "Could not create thread");
00010 thread() : m_thread(INVALID_HANDLE_VALUE) {}
00011 ~thread() {PFC_ASSERT(!isActive()); waitTillDone();}
00012 void start() {
00013 close();
00014 HANDLE thread;
00015 const int priority = GetThreadPriority(GetCurrentThread());
00016 const bool overridePriority = (priority != THREAD_PRIORITY_NORMAL);
00017 thread = CreateThread(NULL,0,g_entry,reinterpret_cast<void*>(this),overridePriority ? CREATE_SUSPENDED : 0,NULL);
00018 if (thread == NULL) throw exception_creation();
00019 if (overridePriority) {
00020 SetThreadPriority(thread, priority);
00021 ResumeThread(thread);
00022 }
00023 m_thread = thread;
00024 }
00025 bool isActive() const {
00026 return m_thread != INVALID_HANDLE_VALUE;
00027 }
00028 void waitTillDone() {
00029 close();
00030 }
00031 protected:
00032 virtual void threadProc() {PFC_ASSERT(!"Stub thread entry - should not get here");}
00033 private:
00034 void close() {
00035 if (isActive()) {
00036 WaitForSingleObject(m_thread,INFINITE);
00037 CloseHandle(m_thread); m_thread = INVALID_HANDLE_VALUE;
00038 }
00039 }
00040
00041 static DWORD CALLBACK g_entry(void* p_instance) {
00042 return reinterpret_cast<thread*>(p_instance)->entry();
00043 }
00044 unsigned entry() {
00045 try {
00046 threadProc();
00047 } catch(...) {}
00048 return 0;
00049 }
00050 HANDLE m_thread;
00051
00052 PFC_CLASS_NOT_COPYABLE_EX(thread)
00053 };
00054 #else
00055 #error PORTME
00056 #endif
00057 }