1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL: https://scummvm.svn.sourceforge.net/svnroot/scummvm/scummvm/trunk/backends/platform/psp/osys_psp.h $
* $Id: osys_psp.h 49173 2010-05-24 03:05:17Z bluddy $
*
*/
#include <time.h>
#include <psptypes.h>
#include <psprtc.h>
#include <pspthreadman.h>
#include "backends/platform/psp/thread.h"
#include "backends/platform/psp/trace.h"
// Class PspThread --------------------------------------------------
void PspThread::delayMillis(uint32 ms) {
sceKernelDelayThread(ms * 1000);
}
void PspThread::delayMicros(uint32 us) {
sceKernelDelayThread(us);
}
// Class PspSemaphore ------------------------------------------------
//#define __PSP_DEBUG_FUNCS__ /* For debugging function calls */
//#define __PSP_DEBUG_PRINT__ /* For debug printouts */
#include "backends/platform/psp/trace.h"
PspSemaphore::PspSemaphore(int initialValue, int maxValue/*=255*/) {
DEBUG_ENTER_FUNC();
_handle = 0;
_handle = (uint32)sceKernelCreateSema("ScummVM Sema", 0 /* attr */,
initialValue, maxValue,
0 /*option*/);
if (!_handle)
PSP_ERROR("failed to create semaphore.\n");
}
PspSemaphore::~PspSemaphore() {
DEBUG_ENTER_FUNC();
if (_handle)
if (sceKernelDeleteSema((SceUID)_handle) < 0)
PSP_ERROR("failed to delete semaphore.\n");
}
int PspSemaphore::numOfWaitingThreads() {
DEBUG_ENTER_FUNC();
SceKernelSemaInfo info;
info.numWaitThreads = 0;
if (sceKernelReferSemaStatus((SceUID)_handle, &info) < 0)
PSP_ERROR("failed to retrieve semaphore info for handle %d\n", _handle);
return info.numWaitThreads;
}
int PspSemaphore::getValue() {
DEBUG_ENTER_FUNC();
SceKernelSemaInfo info;
info.currentCount = 0;
if (sceKernelReferSemaStatus((SceUID)_handle, &info) < 0)
PSP_ERROR("failed to retrieve semaphore info for handle %d\n", _handle);
return info.currentCount;
}
bool PspSemaphore::pollForValue(int value) {
DEBUG_ENTER_FUNC();
if (sceKernelPollSema((SceUID)_handle, value) < 0)
return false;
return true;
}
// false: timeout or error
bool PspSemaphore::takeWithTimeOut(uint32 timeOut) {
DEBUG_ENTER_FUNC();
uint32 *pTimeOut = 0;
if (timeOut)
pTimeOut = &timeOut;
if (sceKernelWaitSema(_handle, 1, pTimeOut) < 0) // we always wait for 1
return false;
return true;
}
bool PspSemaphore::give(int num /*=1*/) {
DEBUG_ENTER_FUNC();
if (sceKernelSignalSema((SceUID)_handle, num) < 0)
return false;
return true;
}
// Class PspMutex ------------------------------------------------------------
bool PspMutex::lock() {
DEBUG_ENTER_FUNC();
int threadId = sceKernelGetThreadId();
bool ret = true;
if (_ownerId == threadId) {
_recursiveCount++;
} else {
ret = _semaphore.take();
_ownerId = threadId;
_recursiveCount = 0;
}
return ret;
}
bool PspMutex::unlock() {
DEBUG_ENTER_FUNC();
int threadId = sceKernelGetThreadId();
bool ret = true;
if (_ownerId != threadId) {
PSP_ERROR("attempt to unlock mutex by thread[%x] as opposed to owner[%x]\n",
threadId, _ownerId);
return false;
}
if (_recursiveCount) {
_recursiveCount--;
} else {
_ownerId = 0;
ret = _semaphore.give(1);
}
return ret;
}
// Class PspCondition -------------------------------------------------
// Release all threads waiting on the condition
void PspCondition::releaseAll() {
_mutex.lock();
if (_waitingThreads > _signaledThreads) { // we have signals to issue
int numWaiting = _waitingThreads - _signaledThreads; // threads we haven't signaled
_signaledThreads = _waitingThreads;
_waitSem.give(numWaiting);
_mutex.unlock();
for (int i=0; i<numWaiting; i++) // wait for threads to tell us they're awake
_doneSem.take();
} else {
_mutex.unlock();
}
}
// Mutex must be taken before entering wait
void PspCondition::wait(PspMutex &externalMutex) {
_mutex.lock();
_waitingThreads++;
_mutex.unlock();
externalMutex.unlock(); // must unlock external mutex
_waitSem.take(); // sleep on the wait semaphore
// let the signaling thread know we're done
_mutex.lock();
if (_signaledThreads > 0 ) {
_doneSem.give(); // let the thread know
_signaledThreads--;
}
_waitingThreads--;
_mutex.unlock();
externalMutex.lock(); // must lock external mutex here for continuation
}
//#define __PSP_DEBUG_FUNCS__ /* For debugging function calls */
//#define __PSP_DEBUG_PRINT__ /* For debug printouts */
#include "backends/platform/psp/trace.h"
// Class PspRtc ---------------------------------------------------------------
void PspRtc::init() { // init our starting ticks
uint32 ticks[2];
sceRtcGetCurrentTick((u64 *)ticks);
_startMillis = ticks[0]/1000;
_startMicros = ticks[0];
//_lastMillis = ticks[0]/1000; //debug - only when we don't subtract startMillis
}
#define MS_LOOP_AROUND 4294967 /* We loop every 2^32 / 1000 = 71 minutes */
#define MS_LOOP_CHECK 60000 /* Threading can cause weird mixups without this */
// Note that after we fill up 32 bits ie 50 days we'll loop back to 0, which may cause
// unpredictable results
uint32 PspRtc::getMillis() {
uint32 ticks[2];
sceRtcGetCurrentTick((u64 *)ticks); // can introduce weird thread delays
uint32 millis = ticks[0]/1000;
millis -= _startMillis; // get ms since start of program
if ((int)_lastMillis - (int)millis > MS_LOOP_CHECK) { // we must have looped around
if (_looped == false) { // check to make sure threads do this once
_looped = true;
_milliOffset += MS_LOOP_AROUND; // add the needed offset
PSP_DEBUG_PRINT("looping around. last ms[%d], curr ms[%d]\n", _lastMillis, millis);
}
} else {
_looped = false;
}
_lastMillis = millis;
return millis + _milliOffset;
}
uint32 PspRtc::getMicros() {
uint32 ticks[2];
sceRtcGetCurrentTick((u64 *)ticks);
ticks[0] -= _startMicros;
return ticks[0];
}
|