2 * Copyright (C) 2005-2008 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 // Thread.h: interface for the CThread class.
24 //////////////////////////////////////////////////////////////////////
31 #include "threads/ThreadImpl.h"
32 #include "threads/ThreadLocal.h"
33 #include "commons/ilog.h"
36 #include <mach/mach.h>
43 virtual ~IRunnable() {}
46 // minimum as mandated by XTL
47 #define THREAD_MINSTACKSIZE 0x10000
49 namespace XbmcThreads { class ThreadSettings; }
53 static XbmcCommons::ILogger* logger;
56 CThread(const char* ThreadName);
59 CThread(IRunnable* pRunnable, const char* ThreadName);
61 void Create(bool bAutoDelete = false, unsigned stacksize = 0);
62 void Sleep(unsigned int milliseconds);
63 int GetSchedRRPriority(void);
64 bool SetPrioritySched_RR(int iPriority);
65 bool IsAutoDelete() const;
66 virtual void StopThread(bool bWait = true);
67 bool IsRunning() const;
69 // -----------------------------------------------------------------------------------
70 // These are platform specific and can be found in ./platform/[platform]/ThreadImpl.cpp
71 // -----------------------------------------------------------------------------------
72 bool IsCurrentThread() const;
73 int GetMinPriority(void);
74 int GetMaxPriority(void);
75 int GetNormalPriority(void);
76 int GetPriority(void);
77 bool SetPriority(const int iPriority);
78 bool WaitForThreadExit(unsigned int milliseconds);
79 float GetRelativeUsage(); // returns the relative cpu usage of this thread since last call
80 int64_t GetAbsoluteUsage();
81 // -----------------------------------------------------------------------------------
83 static bool IsCurrentThread(const ThreadIdentifier tid);
84 static ThreadIdentifier GetCurrentThreadId();
85 static CThread* GetCurrentThread();
86 static inline void SetLogger(XbmcCommons::ILogger* logger_) { CThread::logger = logger_; }
87 static inline XbmcCommons::ILogger* GetLogger() { return CThread::logger; }
89 virtual void OnException(){} // signal termination handler
91 virtual void OnStartup(){};
92 virtual void OnExit(){};
93 virtual void Process();
95 volatile bool m_bStop;
97 enum WaitResponse { WAIT_INTERRUPTED = -1, WAIT_SIGNALED = 0, WAIT_TIMEDOUT = 1 };
100 * This call will wait on a CEvent in an interruptible way such that if
101 * stop is called on the thread the wait will return with a response
102 * indicating what happened.
104 inline WaitResponse AbortableWait(CEvent& event, int timeoutMillis = -1 /* indicates wait forever*/)
106 XbmcThreads::CEventGroup group(&event, &m_StopEvent, NULL);
107 CEvent* result = timeoutMillis < 0 ? group.wait() : group.wait(timeoutMillis);
108 return result == &event ? WAIT_SIGNALED :
109 (result == NULL ? WAIT_TIMEDOUT : WAIT_INTERRUPTED);
113 static THREADFUNC staticThread(void *data);
116 // -----------------------------------------------------------------------------------
117 // These are platform specific and can be found in ./platform/[platform]/ThreadImpl.cpp
118 // -----------------------------------------------------------------------------------
119 ThreadIdentifier ThreadId() const;
120 void SetThreadInfo();
122 void SetSignalHandlers();
123 void SpawnThread(unsigned stacksize);
124 // -----------------------------------------------------------------------------------
126 ThreadIdentifier m_ThreadId;
127 ThreadOpaque m_ThreadOpaque;
132 CCriticalSection m_CriticalSection;
133 IRunnable* m_pRunnable;
134 uint64_t m_iLastUsage;
135 uint64_t m_iLastTime;
138 std::string m_ThreadName;