/[pcsx2_0.9.7]/trunk/common/include/Utilities/PersistentThread.h
ViewVC logotype

Diff of /trunk/common/include/Utilities/PersistentThread.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 31 by william, Tue Sep 7 03:24:11 2010 UTC revision 62 by william, Tue Sep 7 11:08:22 2010 UTC
# Line 23  namespace Threading Line 23  namespace Threading
23  {  {
24    
25  // --------------------------------------------------------------------------------------  // --------------------------------------------------------------------------------------
 // IThread - Interface for the public access to PersistentThread.  
 // --------------------------------------------------------------------------------------  
 // Class usage: Can be used for allowing safe nullification of a thread handle.  Rather  
 // than being NULL'd, the handle can be mapped to an IThread implementation which acts  
 // as a do-nothing placebo or an assertion generator.  
 //  
         class IThread  
         {  
                 DeclareNoncopyableObject(IThread);  
   
         public:  
                 IThread() {}  
                 virtual ~IThread() throw() {}  
   
                 virtual bool IsSelf() const { return false; }  
                 virtual bool IsRunning() { return false; }  
   
                 virtual void Start() {}  
                 virtual void Cancel( bool isBlocking = true ) {}  
                 virtual void Block() {}  
                 virtual bool Detach() { return false; }  
         };  
   
 // --------------------------------------------------------------------------------------  
26  //  ThreadDeleteEvent  //  ThreadDeleteEvent
27  // --------------------------------------------------------------------------------------  // --------------------------------------------------------------------------------------
28          class EventListener_Thread : public IEventDispatcher<int>          class EventListener_Thread : public IEventDispatcher<int>
# Line 55  namespace Threading Line 31  namespace Threading
31                  typedef int EvtParams;                  typedef int EvtParams;
32    
33          protected:          protected:
34                  PersistentThread* m_thread;                  pxThread* m_thread;
35    
36          public:          public:
37                  EventListener_Thread()                  EventListener_Thread()
# Line 65  namespace Threading Line 41  namespace Threading
41    
42                  virtual ~EventListener_Thread() throw() {}                  virtual ~EventListener_Thread() throw() {}
43    
44                  void SetThread( PersistentThread& thr ) { m_thread = &thr; }                  void SetThread( pxThread& thr ) { m_thread = &thr; }
45                  void SetThread( PersistentThread* thr ) { m_thread = thr; }                  void SetThread( pxThread* thr ) { m_thread = thr; }
46    
47                  void DispatchEvent( const int& params )                  void DispatchEvent( const int& params )
48                  {                  {
# Line 74  namespace Threading Line 50  namespace Threading
50                  }                  }
51    
52          protected:          protected:
53                  // Invoked by the PersistentThread when the thread execution is ending.  This is                  // Invoked by the pxThread when the thread execution is ending.  This is
54                  // typically more useful than a delete listener since the extended thread information                  // typically more useful than a delete listener since the extended thread information
55                  // provided by virtualized functions/methods will be available.                  // provided by virtualized functions/methods will be available.
56                  // Important!  This event is executed *by the thread*, so care must be taken to ensure                  // Important!  This event is executed *by the thread*, so care must be taken to ensure
# Line 83  namespace Threading Line 59  namespace Threading
59          };          };
60    
61  // --------------------------------------------------------------------------------------  // --------------------------------------------------------------------------------------
62  // PersistentThread - Helper class for the basics of starting/managing persistent threads.  // pxThread - Helper class for the basics of starting/managing persistent threads.
63  // --------------------------------------------------------------------------------------  // --------------------------------------------------------------------------------------
64  // This class is meant to be a helper for the typical threading model of "start once and  // This class is meant to be a helper for the typical threading model of "start once and
65  // reuse many times."  This class incorporates a lot of extra overhead in stopping and  // reuse many times."  This class incorporates a lot of extra overhead in stopping and
# Line 109  namespace Threading Line 85  namespace Threading
85  //    no dependency options for ensuring correct static var initializations).  Use heap  //    no dependency options for ensuring correct static var initializations).  Use heap
86  //    allocation to create thread objects instead.  //    allocation to create thread objects instead.
87  //  //
88          class PersistentThread : public virtual IThread          class pxThread
89          {          {
90                  DeclareNoncopyableObject(PersistentThread);                  DeclareNoncopyableObject(pxThread);
91    
92                  friend void pxYield( int ms );                  friend void pxYield( int ms );
93    
# Line 123  namespace Threading Line 99  namespace Threading
99    
100                  Semaphore       m_sem_event;            // general wait event that's needed by most threads                  Semaphore       m_sem_event;            // general wait event that's needed by most threads
101                  Semaphore       m_sem_startup;          // startup sync tool                  Semaphore       m_sem_startup;          // startup sync tool
102                  Mutex           m_lock_InThread;                // used for canceling and closing threads in a deadlock-safe manner                  Mutex           m_mtx_InThread;         // used for canceling and closing threads in a deadlock-safe manner
103                  MutexRecursive  m_lock_start;   // used to lock the Start() code from starting simultaneous threads accidentally.                  MutexRecursive  m_mtx_start;    // used to lock the Start() code from starting simultaneous threads accidentally.
104                    Mutex           m_mtx_ThreadName;
105    
106                  volatile long m_detached;               // a boolean value which indicates if the m_thread handle is valid                  volatile long m_detached;               // a boolean value which indicates if the m_thread handle is valid
107                  volatile long m_running;                // set true by Start(), and set false by Cancel(), Block(), etc.                  volatile long m_running;                // set true by Start(), and set false by Cancel(), Block(), etc.
# Line 137  namespace Threading Line 114  namespace Threading
114    
115    
116          public:          public:
117                  virtual ~PersistentThread() throw();                  virtual ~pxThread() throw();
118                  PersistentThread();                  pxThread( const wxString& name=L"pxThread" );
                 PersistentThread( const char* name );  
119    
120                  pthread_t GetId() const { return m_thread; }                  pthread_t GetId() const { return m_thread; }
121                  u64 GetCpuTime() const;                  u64 GetCpuTime() const;
# Line 166  namespace Threading Line 142  namespace Threading
142    
143                  bool IsRunning() const;                  bool IsRunning() const;
144                  bool IsSelf() const;                  bool IsSelf() const;
                 wxString GetName() const;  
145                  bool HasPendingException() const { return !!m_except; }                  bool HasPendingException() const { return !!m_except; }
146    
147                    wxString GetName() const;
148                    void SetName( const wxString& newname );
149    
150          protected:          protected:
151                  // Extending classes should always implement your own OnStart(), which is called by                  // Extending classes should always implement your own OnStart(), which is called by
152                  // Start() once necessary locks have been obtained.  Do not override Start() directly                  // Start() once necessary locks have been obtained.  Do not override Start() directly
# Line 177  namespace Threading Line 155  namespace Threading
155    
156                  virtual void OnStartInThread();                  virtual void OnStartInThread();
157    
158                  // This is called when the thread has been canceled or exits normally.  The PersistentThread                  // This is called when the thread has been canceled or exits normally.  The pxThread
159                  // automatically binds it to the pthread cleanup routines as soon as the thread starts.                  // automatically binds it to the pthread cleanup routines as soon as the thread starts.
160                  virtual void OnCleanupInThread();                  virtual void OnCleanupInThread();
161    
# Line 217  namespace Threading Line 195  namespace Threading
195                  void _DoSetThreadName( const wxString& name );                  void _DoSetThreadName( const wxString& name );
196                  void _DoSetThreadName( const char* name );                  void _DoSetThreadName( const char* name );
197                  void _internal_execute();                  void _internal_execute();
198                  void _try_virtual_invoke( void (PersistentThread::*method)() );                  void _try_virtual_invoke( void (pxThread::*method)() );
199                  void _ThreadCleanup();                  void _ThreadCleanup();
200    
201                  static void* _internal_callback( void* func );                  static void* _internal_callback( void* func );
# Line 262  namespace Threading Line 240  namespace Threading
240  //    into smaller sections.  For example, if you have 20,000 items to process, the task  //    into smaller sections.  For example, if you have 20,000 items to process, the task
241  //    can be divided into two threads of 10,000 items each.  //    can be divided into two threads of 10,000 items each.
242  //  //
243          class BaseTaskThread : public PersistentThread          class BaseTaskThread : public pxThread
244          {          {
245          protected:          protected:
246                  volatile bool m_Done;                  volatile bool m_Done;

Legend:
Removed from v.31  
changed lines
  Added in v.62

  ViewVC Help
Powered by ViewVC 1.1.22