Merge pull request #894 from dteirney/myth-0.25
[vuplus_xbmc] / xbmc / threads / Thread.h
1 /*
2  *      Copyright (C) 2005-2008 Team XBMC
3  *      http://www.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, write to
17  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18  *  http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21
22 // Thread.h: interface for the CThread class.
23 //
24 //////////////////////////////////////////////////////////////////////
25
26 #pragma once
27
28 #include <string>
29 #include <stdint.h>
30 #include "Event.h"
31 #include "threads/ThreadImpl.h"
32 #include "threads/ThreadLocal.h"
33 #include "commons/ilog.h"
34
35 #ifdef TARGET_DARWIN
36 #include <mach/mach.h>
37 #endif
38
39 class IRunnable
40 {
41 public:
42   virtual void Run()=0;
43   virtual ~IRunnable() {}
44 };
45
46 // minimum as mandated by XTL
47 #define THREAD_MINSTACKSIZE 0x10000
48
49 namespace XbmcThreads { class ThreadSettings; }
50
51 class CThread
52 {
53   static XbmcCommons::ILogger* logger;
54
55 protected:
56   CThread(const char* ThreadName);
57
58 public:
59   CThread(IRunnable* pRunnable, const char* ThreadName);
60   virtual ~CThread();
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;
76   bool IsRunning();
77
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_; }
82 protected:
83   virtual void OnStartup(){};
84   virtual void OnExit(){};
85   virtual void OnException(){} // signal termination handler
86   virtual void Process();
87
88   volatile bool m_bStop;
89
90   enum WaitResponse { WAIT_INTERRUPTED = -1, WAIT_SIGNALED = 0, WAIT_TIMEDOUT = 1 };
91
92   /**
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.
96    */
97   inline WaitResponse AbortableWait(CEvent& event, int timeoutMillis)
98   {
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);
103   }
104
105   inline WaitResponse AbortableWait(CEvent& event)
106   {
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);
111   }
112
113 private:
114   static THREADFUNC staticThread(void *data);
115   ThreadIdentifier ThreadId() const;
116   void SetThreadInfo();
117   void TermHandler();
118
119   ThreadIdentifier m_ThreadId;
120   ThreadOpaque m_ThreadOpaque;
121   bool m_bAutoDelete;
122   CEvent m_StopEvent;
123   CEvent m_TermEvent;
124   CEvent m_StartEvent;
125   CCriticalSection m_CriticalSection;
126   IRunnable* m_pRunnable;
127   uint64_t m_iLastUsage;
128   uint64_t m_iLastTime;
129   float m_fLastUsage;
130
131   std::string m_ThreadName;
132
133 #ifdef __APPLE__
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.
136   //
137   mach_port_t m_machThreadPort;
138 #endif
139
140 };