Merge pull request #1038 from waven/discstub
[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   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;
68
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   // -----------------------------------------------------------------------------------
82
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; }
88
89   virtual void OnException(){} // signal termination handler
90 protected:
91   virtual void OnStartup(){};
92   virtual void OnExit(){};
93   virtual void Process();
94
95   volatile bool m_bStop;
96
97   enum WaitResponse { WAIT_INTERRUPTED = -1, WAIT_SIGNALED = 0, WAIT_TIMEDOUT = 1 };
98
99   /**
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.
103    */
104   inline WaitResponse AbortableWait(CEvent& event, int timeoutMillis = -1 /* indicates wait forever*/)
105   {
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);
110   }
111
112 private:
113   static THREADFUNC staticThread(void *data);
114   void Action();
115
116   // -----------------------------------------------------------------------------------
117   // These are platform specific and can be found in ./platform/[platform]/ThreadImpl.cpp
118   // -----------------------------------------------------------------------------------
119   ThreadIdentifier ThreadId() const;
120   void SetThreadInfo();
121   void TermHandler();
122   void SetSignalHandlers();
123   void SpawnThread(unsigned stacksize);
124   // -----------------------------------------------------------------------------------
125
126   ThreadIdentifier m_ThreadId;
127   ThreadOpaque m_ThreadOpaque;
128   bool m_bAutoDelete;
129   CEvent m_StopEvent;
130   CEvent m_TermEvent;
131   CEvent m_StartEvent;
132   CCriticalSection m_CriticalSection;
133   IRunnable* m_pRunnable;
134   uint64_t m_iLastUsage;
135   uint64_t m_iLastTime;
136   float m_fLastUsage;
137
138   std::string m_ThreadName;
139 };