/[pcsx2_0.9.7]/trunk/pcsx2/System/SysThreads.h
ViewVC logotype

Annotation of /trunk/pcsx2/System/SysThreads.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (hide annotations) (download)
Tue Sep 7 11:08:22 2010 UTC (10 years, 1 month ago) by william
File MIME type: text/plain
File size: 8575 byte(s)
Auto Commited Import of: pcsx2-0.9.7-r3738-debug in ./trunk
1 william 31 /* PCSX2 - PS2 Emulator for PCs
2     * Copyright (C) 2002-2010 PCSX2 Dev Team
3     *
4     * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5     * of the GNU Lesser General Public License as published by the Free Software Found-
6     * ation, either version 3 of the License, or (at your option) any later version.
7     *
8     * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10     * PURPOSE. See the GNU General Public License for more details.
11     *
12     * You should have received a copy of the GNU General Public License along with PCSX2.
13     * If not, see <http://www.gnu.org/licenses/>.
14     */
15    
16     #pragma once
17    
18     #include "System.h"
19    
20     #include "Utilities/PersistentThread.h"
21     #include "x86emitter/tools.h"
22    
23    
24     using namespace Threading;
25    
26     typedef SafeArray<u8> VmStateBuffer;
27    
28     // --------------------------------------------------------------------------------------
29     // SysThreadBase
30     // --------------------------------------------------------------------------------------
31    
32 william 62 class SysThreadBase : public pxThread
33 william 31 {
34 william 62 typedef pxThread _parent;
35 william 31
36     public:
37     // Important: The order of these enumerations matters! Optimized tests are used for both
38     // Closed and Paused states.
39     enum ExecutionMode
40     {
41     // Thread has not been created yet. Typically this is the same as IsRunning()
42     // returning FALSE.
43     ExecMode_NoThreadYet,
44    
45     // Thread is safely paused, with plugins in a "closed" state, and waiting for a
46     // resume/open signal.
47     ExecMode_Closed,
48    
49     // Thread is safely paused, with plugins in an "open" state, and waiting for a
50     // resume/open signal.
51     ExecMode_Paused,
52    
53     // Thread is active and running, with pluigns in an "open" state.
54     ExecMode_Opened,
55    
56     // Close signal has been sent to the thread, but the thread's response is still
57     // pending (thread is busy/running).
58     ExecMode_Closing,
59    
60     // Pause signal has been sent to the thread, but the thread's response is still
61     // pending (thread is busy/running).
62     ExecMode_Pausing,
63     };
64    
65     protected:
66     volatile ExecutionMode m_ExecMode;
67    
68     // This lock is used to avoid simultaneous requests to Suspend/Resume/Pause from
69     // contending threads.
70     MutexRecursive m_ExecModeMutex;
71    
72     // Used to wake up the thread from sleeping when it's in a suspended state.
73     Semaphore m_sem_Resume;
74    
75     // Used to synchronize inline changes from paused to suspended status.
76     Semaphore m_sem_ChangingExecMode;
77    
78     // Locked whenever the thread is not in a suspended state (either closed or paused).
79     // Issue a Wait against this mutex for performing actions that require the thread
80     // to be suspended.
81     Mutex m_RunningLock;
82    
83     public:
84     explicit SysThreadBase();
85     virtual ~SysThreadBase() throw();
86    
87     // Thread safety for IsOpen / IsClosed: The execution mode can change at any time on
88     // any thread, so the actual status may have already changed by the time this function
89     // returns its result. Typically this isn't of major concern. However if you need
90     // more assured execution mode status, issue a lock against the ExecutionModeMutex()
91     // first.
92     bool IsOpen() const
93     {
94     return IsRunning() && (m_ExecMode > ExecMode_Closed);
95     }
96    
97     bool IsClosed() const { return !IsOpen(); }
98     bool IsPaused() const { return !IsRunning() || (m_ExecMode <= ExecMode_Paused); }
99    
100 william 62 bool IsClosing() const
101     {
102     return !IsRunning() || (m_ExecMode <= ExecMode_Closed) || (m_ExecMode == ExecMode_Closing);
103     }
104    
105 william 31 bool HasPendingStateChangeRequest() const
106     {
107 william 62 return m_ExecMode >= ExecMode_Closing;
108 william 31 }
109    
110     ExecutionMode GetExecutionMode() const { return m_ExecMode; }
111     Mutex& ExecutionModeMutex() { return m_ExecModeMutex; }
112    
113     virtual void Suspend( bool isBlocking = true );
114     virtual void Resume();
115     virtual void Pause();
116    
117     protected:
118     virtual void OnStart();
119    
120     // This function is called by Resume immediately prior to releasing the suspension of
121     // the core emulation thread. You should overload this rather than Resume(), since
122     // Resume() has a lot of checks and balances to prevent re-entrance and race conditions.
123     virtual void OnResumeReady() {}
124    
125 william 62 virtual bool StateCheckInThread();
126 william 31 virtual void OnCleanupInThread();
127     virtual void OnStartInThread();
128    
129     // Used internally from Resume(), so let's make it private here.
130     virtual void Start();
131    
132     // Extending classes should implement this, but should not call it. The parent class
133     // handles invocation by the following guidelines: Called *in thread* from StateCheckInThread()
134     // prior to suspending the thread (ie, when Suspend() has been called on a separate
135     // thread, requesting this thread suspend itself temporarily). After this is called,
136     // the thread enters a waiting state on the m_sem_Resume semaphore.
137     virtual void OnSuspendInThread()=0;
138    
139     // Extending classes should implement this, but should not call it. The parent class
140     // handles invocation by the following guidelines: Called *in thread* from StateCheckInThread()
141     // prior to pausing the thread (ie, when Pause() has been called on a separate thread,
142     // requesting this thread pause itself temporarily). After this is called, the thread
143     // enters a waiting state on the m_sem_Resume semaphore.
144     virtual void OnPauseInThread()=0;
145    
146     // Extending classes should implement this, but should not call it. The parent class
147     // handles invocation by the following guidelines: Called from StateCheckInThread() after the
148     // thread has been suspended and then subsequently resumed.
149     // Parameter:
150     // isSuspended - set to TRUE if the thread is returning from a suspended state, or
151     // FALSE if it's returning from a paused state.
152     virtual void OnResumeInThread( bool isSuspended )=0;
153     };
154    
155    
156     // --------------------------------------------------------------------------------------
157     // SysCoreThread class
158     // --------------------------------------------------------------------------------------
159     class SysCoreThread : public SysThreadBase
160     {
161     typedef SysThreadBase _parent;
162    
163     protected:
164     bool m_resetRecompilers;
165     bool m_resetProfilers;
166     bool m_resetVsyncTimers;
167     bool m_resetVirtualMachine;
168    
169     // Indicates if the system has an active virtual machine state. Pretty much always
170     // true anytime between plugins being initialized and plugins being shutdown. Gets
171     // set false when plugins are shutdown, the corethread is canceled, or when an error
172     // occurs while trying to upload a new state into the VM.
173     volatile bool m_hasActiveMachine;
174    
175     wxString m_elf_override;
176    
177     SSE_MXCSR m_mxcsr_saved;
178    
179     public:
180     explicit SysCoreThread();
181     virtual ~SysCoreThread() throw();
182    
183     bool HasPendingStateChangeRequest() const;
184    
185     virtual void OnResumeReady();
186     virtual void Reset();
187     virtual void Cancel( bool isBlocking=true );
188     virtual bool Cancel( const wxTimeSpan& timeout );
189    
190 william 62 virtual bool StateCheckInThread();
191 william 31 virtual void VsyncInThread();
192 william 62 virtual void GameStartingInThread();
193 william 31
194     virtual void ApplySettings( const Pcsx2Config& src );
195     virtual void UploadStateCopy( const VmStateBuffer& copy );
196    
197     virtual bool HasActiveMachine() const { return m_hasActiveMachine; }
198    
199     virtual const wxString& GetElfOverride() const { return m_elf_override; }
200     virtual void SetElfOverride( const wxString& elf );
201    
202     protected:
203     void _reset_stuff_as_needed();
204    
205     virtual void Start();
206     virtual void OnStart();
207     virtual void OnSuspendInThread();
208     virtual void OnPauseInThread() {}
209     virtual void OnResumeInThread( bool IsSuspended );
210     virtual void OnCleanupInThread();
211     virtual void ExecuteTaskInThread();
212     virtual void DoCpuReset();
213     virtual void DoCpuExecute();
214    
215     void _StateCheckThrows();
216     };
217    
218    
219     struct SysStateUnlockedParams
220     {
221     SysStateUnlockedParams() {}
222     };
223    
224     // --------------------------------------------------------------------------------------
225     // IEventListener_SaveStateThread
226     // --------------------------------------------------------------------------------------
227     class IEventListener_SysState : public IEventDispatcher<SysStateUnlockedParams>
228     {
229     public:
230     typedef SysStateUnlockedParams EvtParams;
231    
232     public:
233     IEventListener_SysState() {}
234     virtual ~IEventListener_SysState() throw() {}
235    
236     virtual void DispatchEvent( const SysStateUnlockedParams& status )
237     {
238     SysStateAction_OnUnlocked();
239     }
240    
241     protected:
242     virtual void SysStateAction_OnUnlocked();
243     };
244    
245     // GetCoreThread() is a required external implementation. This function is *NOT*
246     // provided by the PCSX2 core library. It provides an interface for the linking User
247     // Interface apps or DLLs to reference their own instance of SysCoreThread (also allowing
248     // them to extend the class and override virtual methods).
249     //
250     extern SysCoreThread& GetCoreThread();

  ViewVC Help
Powered by ViewVC 1.1.22