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 bool WaitForThreadExit(unsigned int milliseconds);
63 void Sleep(unsigned int milliseconds);
64 bool SetPriority(const int iPriority);
65 int GetPriority(void);
66 int GetMinPriority(void);
67 int GetMaxPriority(void);
68 int GetNormalPriority(void);
69 int GetSchedRRPriority(void);
70 bool SetPrioritySched_RR(int iPriority);
71 bool IsAutoDelete() const;
72 virtual void StopThread(bool bWait = true);
73 float GetRelativeUsage(); // returns the relative cpu usage of this thread since last call
74 int64_t GetAbsoluteUsage();
75 bool IsCurrentThread() const;
78 static bool IsCurrentThread(const ThreadIdentifier tid);
79 static ThreadIdentifier GetCurrentThreadId();
80 static CThread* GetCurrentThread();
81 static inline void SetLogger(XbmcCommons::ILogger* logger_) { CThread::logger = logger_; }
83 virtual void OnStartup(){};
84 virtual void OnExit(){};
85 virtual void OnException(){} // signal termination handler
86 virtual void Process();
88 volatile bool m_bStop;
90 enum WaitResponse { WAIT_INTERRUPTED = -1, WAIT_SIGNALED = 0, WAIT_TIMEDOUT = 1 };
93 * This call will wait on a CEvent in an interruptible way such that if
94 * stop is called on the thread the wait will return with a respone
95 * indicating what happened.
97 inline WaitResponse AbortableWait(CEvent& event, int timeoutMillis)
99 XbmcThreads::CEventGroup group(&event, &m_StopEvent, NULL);
100 CEvent* result = group.wait(timeoutMillis);
101 return result == &event ? WAIT_SIGNALED :
102 (result == NULL ? WAIT_TIMEDOUT : WAIT_INTERRUPTED);
105 inline WaitResponse AbortableWait(CEvent& event)
107 XbmcThreads::CEventGroup group(&event, &m_StopEvent, NULL);
108 CEvent* result = group.wait();
109 return result == &event ? WAIT_SIGNALED :
110 (result == NULL ? WAIT_TIMEDOUT : WAIT_INTERRUPTED);
114 static THREADFUNC staticThread(void *data);
115 ThreadIdentifier ThreadId() const;
116 void SetThreadInfo();
119 ThreadIdentifier m_ThreadId;
120 ThreadOpaque m_ThreadOpaque;
125 CCriticalSection m_CriticalSection;
126 IRunnable* m_pRunnable;
127 uint64_t m_iLastUsage;
128 uint64_t m_iLastTime;
131 std::string m_ThreadName;
134 // Save the Mach thrad port, I don't think it can be obtained from
135 // the pthread_t. We'll use it for querying timer information.
137 mach_port_t m_machThreadPort;