2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// 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
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// 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 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
// UNFINISHED
|
|
|
|
|
|
|
|
#include "HLE.h"
|
|
|
|
#include "../MIPS/MIPS.h"
|
|
|
|
#include "sceKernel.h"
|
|
|
|
#include "sceKernelMutex.h"
|
|
|
|
#include "sceKernelThread.h"
|
|
|
|
|
|
|
|
#define PSP_MUTEX_ATTR_FIFO 0
|
|
|
|
#define PSP_MUTEX_ATTR_PRIORITY 0x100
|
|
|
|
#define PSP_MUTEX_ATTR_ALLOW_RECURSIVE 0x200
|
|
|
|
|
|
|
|
// Not sure about the names of these
|
2012-11-15 07:15:33 +00:00
|
|
|
#define PSP_MUTEX_ERROR_NOT_LOCKED 0x800201C5
|
2012-11-01 15:19:01 +00:00
|
|
|
#define PSP_MUTEX_ERROR_NO_SUCH_MUTEX 0x800201C3
|
2012-11-14 16:00:57 +00:00
|
|
|
#define PSP_MUTEX_ERROR_UNLOCK_UNDERFLOW 0x800201C7
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// Guesswork - not exposed anyway
|
|
|
|
struct NativeMutex
|
|
|
|
{
|
|
|
|
SceSize size;
|
|
|
|
char name[32];
|
|
|
|
SceUInt attr;
|
|
|
|
|
|
|
|
int lockLevel;
|
|
|
|
int lockThread; // The thread holding the lock
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Mutex : public KernelObject
|
|
|
|
{
|
|
|
|
const char *GetName() {return nm.name;}
|
|
|
|
const char *GetTypeName() {return "Mutex";}
|
|
|
|
static u32 GetMissingErrorCode() { return SCE_KERNEL_ERROR_UNKNOWN_SEMID; } // Not sure?
|
|
|
|
int GetIDType() const { return SCE_KERNEL_TMID_Mutex; }
|
|
|
|
NativeMutex nm;
|
|
|
|
std::vector<SceUID> waitingThreads;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LWMutex : public KernelObject
|
|
|
|
{
|
|
|
|
const char *GetName() {return nm.name;}
|
|
|
|
const char *GetTypeName() {return "LWMutex";}
|
|
|
|
static u32 GetMissingErrorCode() { return SCE_KERNEL_ERROR_UNKNOWN_SEMID; } // Not sure?
|
|
|
|
int GetIDType() const { return SCE_KERNEL_TMID_LwMutex; }
|
|
|
|
NativeMutex nm;
|
|
|
|
std::vector<SceUID> waitingThreads;
|
|
|
|
};
|
|
|
|
|
2012-11-15 07:15:33 +00:00
|
|
|
u32 sceKernelCreateMutex(const char *name, u32 attr, u32 initial_count, u32 options)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
DEBUG_LOG(HLE,"sceKernelCreateMutex(%s, %08x, %08x)", name, attr, options);
|
|
|
|
|
|
|
|
Mutex *mutex = new Mutex();
|
|
|
|
SceUID id = kernelObjects.Create(mutex);
|
|
|
|
|
|
|
|
mutex->nm.size = sizeof(mutex);
|
|
|
|
mutex->nm.attr = attr;
|
2012-11-15 07:15:33 +00:00
|
|
|
mutex->nm.lockLevel = initial_count;
|
|
|
|
// TODO: Does initial_count > 0 mean lock automatically by the current thread? Would make sense.
|
2012-11-01 15:19:01 +00:00
|
|
|
mutex->nm.lockThread = -1;
|
|
|
|
|
|
|
|
strncpy(mutex->nm.name, name, 32);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 sceKernelDeleteMutex(u32 id)
|
|
|
|
{
|
|
|
|
DEBUG_LOG(HLE,"sceKernelDeleteMutex(%i)", id);
|
|
|
|
u32 error;
|
|
|
|
Mutex *mutex = kernelObjects.Get<Mutex>(id, error);
|
|
|
|
if (!mutex)
|
|
|
|
return PSP_MUTEX_ERROR_NO_SUCH_MUTEX;
|
|
|
|
kernelObjects.Destroy<Mutex>(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 sceKernelLockMutex(u32 id, u32 count, u32 timeoutPtr)
|
|
|
|
{
|
|
|
|
DEBUG_LOG(HLE,"sceKernelLockMutex(%i, %i, %08x)", id, count, timeoutPtr);
|
|
|
|
u32 error;
|
|
|
|
Mutex *mutex = kernelObjects.Get<Mutex>(id, error);
|
|
|
|
if (!mutex)
|
|
|
|
return PSP_MUTEX_ERROR_NO_SUCH_MUTEX;
|
2012-11-14 16:00:57 +00:00
|
|
|
if (count <= 0)
|
|
|
|
return SCE_KERNEL_ERROR_ILLEGAL_COUNT;
|
|
|
|
if (count > 1 && !(mutex->nm.attr & PSP_MUTEX_ATTR_ALLOW_RECURSIVE))
|
|
|
|
return SCE_KERNEL_ERROR_ILLEGAL_COUNT;
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
if (mutex->nm.lockLevel == 0)
|
|
|
|
{
|
|
|
|
mutex->nm.lockLevel += count;
|
|
|
|
mutex->nm.lockThread = __KernelGetCurThread();
|
|
|
|
// Nobody had it locked - no need to block
|
|
|
|
}
|
|
|
|
else if ((mutex->nm.attr & PSP_MUTEX_ATTR_ALLOW_RECURSIVE) && mutex->nm.lockThread == __KernelGetCurThread())
|
|
|
|
{
|
|
|
|
// Recursive mutex, let's just increase the lock count and keep going
|
|
|
|
mutex->nm.lockLevel += count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-14 16:00:57 +00:00
|
|
|
mutex->waitingThreads.push_back(__KernelGetCurThread());
|
|
|
|
__KernelWaitCurThread(WAITTYPE_MUTEX, id, count, 0, false);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 sceKernelLockMutexCB(u32 id, u32 count, u32 timeoutPtr)
|
|
|
|
{
|
2012-11-14 16:00:57 +00:00
|
|
|
DEBUG_LOG(HLE,"sceKernelLockMutexCB(%i, %i, %08x)", id, count, timeoutPtr);
|
|
|
|
u32 error;
|
|
|
|
Mutex *mutex = kernelObjects.Get<Mutex>(id, error);
|
|
|
|
if (!mutex)
|
|
|
|
return PSP_MUTEX_ERROR_NO_SUCH_MUTEX;
|
|
|
|
if (count <= 0)
|
|
|
|
return SCE_KERNEL_ERROR_ILLEGAL_COUNT;
|
|
|
|
if (count > 1 && !(mutex->nm.attr & PSP_MUTEX_ATTR_ALLOW_RECURSIVE))
|
|
|
|
return SCE_KERNEL_ERROR_ILLEGAL_COUNT;
|
|
|
|
|
|
|
|
if (mutex->nm.lockLevel == 0)
|
|
|
|
{
|
|
|
|
mutex->nm.lockLevel += count;
|
|
|
|
mutex->nm.lockThread = __KernelGetCurThread();
|
|
|
|
// Nobody had it locked - no need to block
|
|
|
|
}
|
|
|
|
else if ((mutex->nm.attr & PSP_MUTEX_ATTR_ALLOW_RECURSIVE) && mutex->nm.lockThread == __KernelGetCurThread())
|
|
|
|
{
|
|
|
|
// Recursive mutex, let's just increase the lock count and keep going
|
|
|
|
mutex->nm.lockLevel += count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mutex->waitingThreads.push_back(__KernelGetCurThread());
|
|
|
|
__KernelWaitCurThread(WAITTYPE_MUTEX, id, count, 0, true);
|
|
|
|
__KernelCheckCallbacks();
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-14 16:00:57 +00:00
|
|
|
// int sceKernelUnlockMutex(SceUID id, int count)
|
|
|
|
// void because it changes threads.
|
|
|
|
void sceKernelUnlockMutex(u32 id, u32 count)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2012-11-14 16:00:57 +00:00
|
|
|
DEBUG_LOG(HLE,"sceKernelUnlockMutex(%i, %i)", id, count);
|
2012-11-01 15:19:01 +00:00
|
|
|
u32 error;
|
|
|
|
Mutex *mutex = kernelObjects.Get<Mutex>(id, error);
|
|
|
|
if (!mutex)
|
2012-11-14 16:00:57 +00:00
|
|
|
{
|
|
|
|
RETURN(PSP_MUTEX_ERROR_NO_SUCH_MUTEX);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
if (mutex->nm.lockLevel == 0)
|
2012-11-14 16:00:57 +00:00
|
|
|
{
|
|
|
|
RETURN(PSP_MUTEX_ERROR_NOT_LOCKED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mutex->nm.lockLevel < count)
|
|
|
|
{
|
|
|
|
RETURN(PSP_MUTEX_ERROR_UNLOCK_UNDERFLOW);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
mutex->nm.lockLevel -= count;
|
2012-11-14 16:00:57 +00:00
|
|
|
RETURN(0);
|
|
|
|
|
|
|
|
if (mutex->nm.lockLevel == 0)
|
|
|
|
{
|
|
|
|
mutex->nm.lockThread = -1;
|
|
|
|
|
|
|
|
// TODO: PSP_MUTEX_ATTR_PRIORITY
|
|
|
|
bool wokeThreads = false;
|
2012-11-15 07:14:41 +00:00
|
|
|
std::vector<SceUID>::iterator iter, end;
|
|
|
|
for (iter = mutex->waitingThreads.begin(), end = mutex->waitingThreads.end(); iter != end; ++iter)
|
2012-11-14 16:00:57 +00:00
|
|
|
{
|
|
|
|
SceUID threadID = *iter;
|
|
|
|
int wVal = (int)__KernelGetWaitValue(threadID, error);
|
|
|
|
|
|
|
|
mutex->nm.lockThread = threadID;
|
|
|
|
mutex->nm.lockLevel = wVal;
|
|
|
|
|
|
|
|
__KernelResumeThreadFromWait(threadID);
|
|
|
|
wokeThreads = true;
|
2012-11-15 07:14:41 +00:00
|
|
|
mutex->waitingThreads.erase(iter);
|
2012-11-14 16:00:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not sure if this should actually resched, need to test.
|
|
|
|
if (wokeThreads)
|
|
|
|
__KernelReSchedule("mutex unlocked");
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2012-11-06 19:56:19 +00:00
|
|
|
|
|
|
|
struct NativeLwMutex
|
|
|
|
{
|
|
|
|
SceSize size;
|
|
|
|
char name[32];
|
|
|
|
SceUInt attr;
|
|
|
|
SceUID mutexUid;
|
|
|
|
SceUInt opaqueWorkAreaAddr;
|
|
|
|
int numWaitThreads;
|
|
|
|
int locked;
|
|
|
|
int threadid; // thread holding the lock
|
|
|
|
};
|
|
|
|
|
|
|
|
void sceKernelCreateLwMutex()
|
|
|
|
{
|
2012-11-07 14:44:48 +00:00
|
|
|
ERROR_LOG(HLE,"UNIMPL sceKernelCreateLwMutex()");
|
2012-11-06 19:56:19 +00:00
|
|
|
RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sceKernelDeleteLwMutex()
|
|
|
|
{
|
2012-11-07 14:44:48 +00:00
|
|
|
ERROR_LOG(HLE,"UNIMPL sceKernelDeleteLwMutex()");
|
2012-11-06 19:56:19 +00:00
|
|
|
RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sceKernelTryLockLwMutex()
|
|
|
|
{
|
2012-11-07 14:44:48 +00:00
|
|
|
ERROR_LOG(HLE,"UNIMPL sceKernelTryLockLwMutex()");
|
2012-11-06 19:56:19 +00:00
|
|
|
RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sceKernelLockLwMutex()
|
|
|
|
{
|
2012-11-07 14:44:48 +00:00
|
|
|
ERROR_LOG(HLE,"UNIMPL sceKernelLockLwMutex()");
|
|
|
|
RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sceKernelLockLwMutexCB()
|
|
|
|
{
|
|
|
|
ERROR_LOG(HLE,"UNIMPL sceKernelLockLwMutexCB()");
|
2012-11-06 19:56:19 +00:00
|
|
|
RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sceKernelUnlockLwMutex()
|
|
|
|
{
|
2012-11-07 14:44:48 +00:00
|
|
|
ERROR_LOG(HLE,"UNIMPL void sceKernelUnlockLwMutex()");
|
2012-11-06 19:56:19 +00:00
|
|
|
RETURN(0);
|
|
|
|
}
|