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
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #define FORBIDDEN_SYMBOL_EXCEPTION_printf
24 #define FORBIDDEN_SYMBOL_EXCEPTION_abort
25 #define FORBIDDEN_SYMBOL_EXCEPTION_exit
26
27 #include "backends/platform/ps2/systemps2.h"
28 #include <kernel.h>
29
initMutexes(void)30 void OSystem_PS2::initMutexes(void) {
31 ee_sema_t newSema;
32 newSema.init_count = 1;
33 newSema.max_count = 1;
34 _mutexSema = CreateSema(&newSema);
35 for (int i = 0; i < MAX_MUTEXES; i++) {
36 _mutex[i].sema = -1;
37 _mutex[i].count = _mutex[i].owner = 0;
38 }
39 }
40
createMutex(void)41 OSystem::MutexRef OSystem_PS2::createMutex(void) {
42 WaitSema(_mutexSema);
43 Ps2Mutex *mutex = NULL;
44 for (int i = 0; i < MAX_MUTEXES; i++)
45 if (_mutex[i].sema < 0) {
46 mutex = _mutex + i;
47 break;
48 }
49 if (mutex) {
50 ee_sema_t newSema;
51 newSema.init_count = 1;
52 newSema.max_count = 1;
53 mutex->sema = CreateSema(&newSema);
54 mutex->owner = mutex->count = 0;
55 } else
56 printf("OSystem_PS2::createMutex: ran out of Mutex slots!\n");
57 SignalSema(_mutexSema);
58 return (OSystem::MutexRef)mutex;
59 }
60
lockMutex(MutexRef mutex)61 void OSystem_PS2::lockMutex(MutexRef mutex) {
62 WaitSema(_mutexSema);
63 Ps2Mutex *sysMutex = (Ps2Mutex *)mutex;
64 int tid = GetThreadId();
65
66 assert(tid != 0);
67 if (sysMutex->owner && (sysMutex->owner == tid))
68 sysMutex->count++;
69 else {
70 SignalSema(_mutexSema);
71 WaitSema(sysMutex->sema);
72 WaitSema(_mutexSema);
73 sysMutex->owner = tid;
74 sysMutex->count = 0;
75 }
76 SignalSema(_mutexSema);
77 }
78
unlockMutex(MutexRef mutex)79 void OSystem_PS2::unlockMutex(MutexRef mutex) {
80 WaitSema(_mutexSema);
81 Ps2Mutex *sysMutex = (Ps2Mutex *)mutex;
82 int tid = GetThreadId();
83
84 if (sysMutex->owner && sysMutex->count && (sysMutex->owner == tid))
85 sysMutex->count--;
86 else {
87 assert(sysMutex->count == 0);
88 SignalSema(sysMutex->sema);
89 sysMutex->owner = 0;
90 }
91 SignalSema(_mutexSema);
92 }
93
deleteMutex(MutexRef mutex)94 void OSystem_PS2::deleteMutex(MutexRef mutex) {
95 WaitSema(_mutexSema);
96 Ps2Mutex *sysMutex = (Ps2Mutex *)mutex;
97 if (sysMutex->owner || sysMutex->count)
98 printf("WARNING: Deleting LOCKED mutex!\n");
99 DeleteSema(sysMutex->sema);
100 sysMutex->sema = -1;
101 SignalSema(_mutexSema);
102 }
103