Remove LiveTV menu.
[vuplus_xbmc] / xbmc / threads / Thread.h
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
21 // Thread.h: interface for the CThread class.
22 //
23 //////////////////////////////////////////////////////////////////////
24
25 #pragma once
26
27 #include <string>
28 #include <stdint.h>
29 #include "Event.h"
30 #include "threads/ThreadImpl.h"
31 #include "threads/ThreadLocal.h"
32 #include "commons/ilog.h"
33
34 #ifdef TARGET_DARWIN
35 #include <mach/mach.h>
36 #endif
37
38 class IRunnable
39 {
40 public:
41   virtual void Run()=0;
42   virtual ~IRunnable() {}
43 };
44
45 // minimum as mandated by XTL
46 #define THREAD_MINSTACKSIZE 0x10000
47
48 namespace XbmcThreads { class ThreadSettings; }
49
50 class CThread
51 {
52   static XbmcCommons::ILogger* logger;
53
54 protected:
55   CThread(const char* ThreadName);
56
57 public:
58   CThread(IRunnable* pRunnable, const char* ThreadName);
59   virtual ~CThread();
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;
67
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   // -----------------------------------------------------------------------------------
81
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; }
87
88   virtual void OnException(){} // signal termination handler
89 protected:
90   virtual void OnStartup(){};
91   virtual void OnExit(){};
92   virtual void Process();
93
94   volatile bool m_bStop;
95
96   enum WaitResponse { WAIT_INTERRUPTED = -1, WAIT_SIGNALED = 0, WAIT_TIMEDOUT = 1 };
97
98   /**
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.
102    */
103   inline WaitResponse AbortableWait(CEvent& event, int timeoutMillis = -1 /* indicates wait forever*/)
104   {
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);
109   }
110
111 private:
112   static THREADFUNC staticThread(void *data);
113   void Action();
114
115   // -----------------------------------------------------------------------------------
116   // These are platform specific and can be found in ./platform/[platform]/ThreadImpl.cpp
117   // -----------------------------------------------------------------------------------
118   ThreadIdentifier ThreadId() const;
119   void SetThreadInfo();
120   void TermHandler();
121   void SetSignalHandlers();
122   void SpawnThread(unsigned stacksize);
123   // -----------------------------------------------------------------------------------
124
125   ThreadIdentifier m_ThreadId;
126   ThreadOpaque m_ThreadOpaque;
127   bool m_bAutoDelete;
128   CEvent m_StopEvent;
129   CEvent m_TermEvent;
130   CEvent m_StartEvent;
131   CCriticalSection m_CriticalSection;
132   IRunnable* m_pRunnable;
133   uint64_t m_iLastUsage;
134   uint64_t m_iLastTime;
135   float m_fLastUsage;
136
137   std::string m_ThreadName;
138 };