ScummVM API documentation
powerman.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef POWERMAN_H
23 #define POWERMAN_H
24 
25 #include "backends/platform/psp/thread.h"
26 #include "common/singleton.h"
27 #include "common/list.h"
28 
29 #include "engines/engine.h" // for PauseToken
30 
31 /*
32  * Implement this class (interface) if you want to use PowerManager's suspend callback functionality
33  *
34  */
35 class Suspendable {
36 public:
37  virtual ~Suspendable() {}
38  virtual int suspend() = 0;
39  virtual int resume() = 0;
40 };
41 
42 /******************************************************************************************************
43 *
44 * This class will call a Suspendable when the PSP goes to suspend/resumes. It also provides the ability to block
45 * a thread when the PSP is going to suspend/suspending, and to wake it up when the PSP is resumed.
46 * This ability is very useful for managing the PSPIoStream class, but may be found useful by other classes as well.
47 *
48 *******************************************************************************************************/
49 class PowerManager: public Common::Singleton<PowerManager> {
50 
51 public:
52  int blockOnSuspend(); /* block if suspending */
53  bool beginCriticalSection(); /* Use a critical section to block (if suspend was already pressed) */
54  void endCriticalSection(); /* and to prevent the PSP from suspending in a particular section */
55  bool registerForSuspend(Suspendable *item); /* register to be called to suspend/resume */
56  bool unregisterForSuspend(Suspendable *item); /* remove from suspend/resume list */
57  void suspend(); /* callback to have all items in list suspend */
58  void resume(); /* callback to have all items in list resume */
59  // Functions for pausing the engine
60  void pollPauseEngine(); /* Poll whether the engine should be paused */
61 
62  enum {
63  Error = -1,
64  NotBlocked = 0,
65  Blocked = 1
66  };
67 
68  enum PauseState {
69  UNPAUSED = 0,
70  PAUSING,
71  PAUSED
72  };
73 
74 private:
75  friend class Common::Singleton<PowerManager>;
76  PowerManager();
77  ~PowerManager();
78 
79  Common::List<Suspendable *> _suspendList; // list to register in
80 
81  volatile bool _pauseFlag; // For pausing, which is before suspending
82  volatile bool _pauseFlagOld; // Save the last state of the flag while polling
83  volatile PauseState _pauseClientState; // Pause state of the target
84  PauseToken _pauseToken;
85 
86  volatile bool _suspendFlag; // protected variable
87  PspMutex _flagMutex; // mutex to access access flag
88  PspMutex _listMutex; // mutex to access Suspendable list
89  PspCondition _threadSleep; // signal to synchronize accessing threads
90  PspCondition _pmSleep; // signal to wake up the PM from a critical section
91  volatile int _criticalCounter; // Counter of how many threads are in a critical section
92  int _error; // error code - PM can't talk to us. For debugging
93  volatile int _PMStatus; // What the PM is doing. for debugging
94 
95  // States for PM to be in (used for debugging)
96  enum PMState {
97  kInitDone = 1,
98  kDestroyPM = 2,
99  kWaitForClientPause = 3,
100  kWaitForClientToFinishPausing = 4,
101  kGettingFlagMutexSuspend = 5,
102  kGotFlagMutexSuspend = 6,
103  kWaitCritSectionSuspend = 7,
104  kDoneWaitingCritSectionSuspend = 8,
105  kGettingListMutexSuspend = 9,
106  kIteratingListSuspend = 10,
107  kDoneIteratingListSuspend = 11,
108  kDoneSuspend = 12,
109  kDonePowerUnlock,
110  kBeginResume,
111  kCheckingPauseFlag,
112  kGettingListMutexResume,
113  kIteratingListResume,
114  kDoneIteratingListResume,
115  kGettingFlagMutexResume,
116  kGotFlagMutexResume,
117  kSignalSuspendedThreadsResume,
118  kDoneSignallingSuspendedThreadsResume,
119  kDoneResume
120  };
121 
122  volatile int _listCounter; /* How many people are in the list - just for debugging */
123 
124  void debugPM(); /* print info about the PM */
125 
126 public:
127  int getPMStatus() const { return _PMStatus; }
128 
129 };
130 
131 // For easy access
132 #define PowerMan PowerManager::instance()
133 
134 #endif /* POWERMAN_H */
Definition: engine.h:102
Definition: powerman.h:35
Definition: list.h:44
Definition: thread.h:62
Definition: powerman.h:49
Definition: thread.h:76
Definition: singleton.h:42