2 * Copyright (C) 2005-2013 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, see
17 * <http://www.gnu.org/licenses/>.
21 // Thread.h: interface for the CThread class.
23 //////////////////////////////////////////////////////////////////////
30 #include "threads/ThreadImpl.h"
31 #include "threads/ThreadLocal.h"
32 #include "commons/ilog.h"
35 #include <mach/mach.h>
42 virtual ~IRunnable() {}
45 // minimum as mandated by XTL
46 #define THREAD_MINSTACKSIZE 0x10000
48 namespace XbmcThreads { class ThreadSettings; }
52 static XbmcCommons::ILogger* logger;
55 CThread(const char* ThreadName);
58 CThread(IRunnable* pRunnable, const char* ThreadName);
60 void Create(bool bAutoDelete = false, unsigned stacksize = 0);
61 void Sleep(unsigned int milliseconds);
62 int GetSchedRRPriority(void);
63 bool SetPrioritySched_RR(int iPriority);
64 bool IsAutoDelete() const;
65 virtual void StopThread(bool bWait = true);
66 bool IsRunning() const;
68 // -----------------------------------------------------------------------------------
69 // These are platform specific and can be found in ./platform/[platform]/ThreadImpl.cpp
70 // -----------------------------------------------------------------------------------
71 bool IsCurrentThread() const;
72 int GetMinPriority(void);
73 int GetMaxPriority(void);
74 int GetNormalPriority(void);
75 int GetPriority(void);
76 bool SetPriority(const int iPriority);
77 bool WaitForThreadExit(unsigned int milliseconds);
78 float GetRelativeUsage(); // returns the relative cpu usage of this thread since last call
79 int64_t GetAbsoluteUsage();
80 // -----------------------------------------------------------------------------------
82 static bool IsCurrentThread(const ThreadIdentifier tid);
83 static ThreadIdentifier GetCurrentThreadId();
84 static CThread* GetCurrentThread();
85 static inline void SetLogger(XbmcCommons::ILogger* logger_) { CThread::logger = logger_; }
86 static inline XbmcCommons::ILogger* GetLogger() { return CThread::logger; }
88 virtual void OnException(){} // signal termination handler
90 virtual void OnStartup(){};
91 virtual void OnExit(){};
92 virtual void Process();
94 volatile bool m_bStop;
96 enum WaitResponse { WAIT_INTERRUPTED = -1, WAIT_SIGNALED = 0, WAIT_TIMEDOUT = 1 };
99 * This call will wait on a CEvent in an interruptible way such that if
100 * stop is called on the thread the wait will return with a response
101 * indicating what happened.
103 inline WaitResponse AbortableWait(CEvent& event, int timeoutMillis = -1 /* indicates wait forever*/)
105 XbmcThreads::CEventGroup group(&event, &m_StopEvent, NULL);
106 CEvent* result = timeoutMillis < 0 ? group.wait() : group.wait(timeoutMillis);
107 return result == &event ? WAIT_SIGNALED :
108 (result == NULL ? WAIT_TIMEDOUT : WAIT_INTERRUPTED);
112 static THREADFUNC staticThread(void *data);
115 // -----------------------------------------------------------------------------------
116 // These are platform specific and can be found in ./platform/[platform]/ThreadImpl.cpp
117 // -----------------------------------------------------------------------------------
118 ThreadIdentifier ThreadId() const;
119 void SetThreadInfo();
121 void SetSignalHandlers();
122 void SpawnThread(unsigned stacksize);
123 // -----------------------------------------------------------------------------------
125 ThreadIdentifier m_ThreadId;
126 ThreadOpaque m_ThreadOpaque;
131 CCriticalSection m_CriticalSection;
132 IRunnable* m_pRunnable;
133 uint64_t m_iLastUsage;
134 uint64_t m_iLastTime;
137 std::string m_ThreadName;