Merge pull request #3118 from unknownbrackets/io-async

Prep work for pspFileSystem and two small fixes
This commit is contained in:
Henrik Rydgård 2013-08-10 14:32:59 -07:00
commit 85e17f03cc
11 changed files with 223 additions and 133 deletions

View File

@ -746,6 +746,7 @@ add_library(${CoreLibName} ${CoreLinkType}
Core/CwCheat.h
Core/HDRemaster.cpp
Core/HDRemaster.h
Core/ThreadEventQueue.h
Core/Debugger/Breakpoints.cpp
Core/Debugger/Breakpoints.h
Core/Debugger/DebugInterface.h

View File

@ -486,6 +486,7 @@
<ClInclude Include="Reporting.h" />
<ClInclude Include="SaveState.h" />
<ClInclude Include="System.h" />
<ClInclude Include="ThreadEventQueue.h" />
<ClInclude Include="Util\BlockAllocator.h" />
<ClInclude Include="Util\PPGeDraw.h" />
<ClInclude Include="Util\ppge_atlas.h" />

View File

@ -837,6 +837,9 @@
<ClInclude Include="FileSystems\VirtualDiscFileSystem.h">
<Filter>FileSystems</Filter>
</ClInclude>
<ClInclude Include="ThreadEventQueue.h">
<Filter>Core</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="CMakeLists.txt" />

View File

@ -16,6 +16,7 @@
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <set>
#include <algorithm>
#include "Common/StringUtils.h"
#include "Core/FileSystems/MetaFileSystem.h"
#include "Core/HLE/sceKernelThread.h"
@ -162,6 +163,7 @@ static bool RealPath(const std::string &currentDirectory, const std::string &inP
IFileSystem *MetaFileSystem::GetHandleOwner(u32 handle)
{
lock_guard guard(lock);
for (size_t i = 0; i < fileSystems.size(); i++)
{
if (fileSystems[i].system->OwnsHandle(handle))
@ -171,9 +173,9 @@ IFileSystem *MetaFileSystem::GetHandleOwner(u32 handle)
return 0;
}
extern u32 ioErrorCode;
bool MetaFileSystem::MapFilePath(const std::string &_inpath, std::string &outpath, MountPoint **system)
{
lock_guard guard(lock);
std::string realpath;
// Special handling: host0:command.txt (as seen in Super Monkey Ball Adventures, for example)
@ -193,7 +195,7 @@ bool MetaFileSystem::MapFilePath(const std::string &_inpath, std::string &outpat
//Attempt to emulate SCE_KERNEL_ERROR_NOCWD / 8002032C: may break things requiring fixes elsewhere
if (inpath.find(':') == std::string::npos /* means path is relative */)
{
ioErrorCode = SCE_KERNEL_ERROR_NOCWD;
lastOpenError = SCE_KERNEL_ERROR_NOCWD;
WARN_LOG_REPORT(HLE, "Path is relative, but current directory not set for thread %i. returning 8002032C(SCE_KERNEL_ERROR_NOCWD) instead.", currentThread);
}
}
@ -225,14 +227,25 @@ bool MetaFileSystem::MapFilePath(const std::string &_inpath, std::string &outpat
void MetaFileSystem::Mount(std::string prefix, IFileSystem *system)
{
lock_guard guard(lock);
MountPoint x;
x.prefix=prefix;
x.system=system;
x.prefix = prefix;
x.system = system;
fileSystems.push_back(x);
}
void MetaFileSystem::Unmount(std::string prefix, IFileSystem *system)
{
lock_guard guard(lock);
MountPoint x;
x.prefix = prefix;
x.system = system;
fileSystems.erase(std::remove(fileSystems.begin(), fileSystems.end(), x), fileSystems.end());
}
void MetaFileSystem::Shutdown()
{
lock_guard guard(lock);
current = 6;
// Ownership is a bit convoluted. Let's just delete everything once.
@ -252,8 +265,18 @@ void MetaFileSystem::Shutdown()
startingDirectory = "";
}
u32 MetaFileSystem::OpenWithError(int &error, std::string filename, FileAccess access, const char *devicename)
{
lock_guard guard(lock);
u32 h = OpenFile(filename, access, devicename);
error = lastOpenError;
return h;
}
u32 MetaFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename)
{
lock_guard guard(lock);
lastOpenError = 0;
std::string of;
MountPoint *mount;
if (MapFilePath(filename, of, &mount))
@ -268,6 +291,7 @@ u32 MetaFileSystem::OpenFile(std::string filename, FileAccess access, const char
PSPFileInfo MetaFileSystem::GetFileInfo(std::string filename)
{
lock_guard guard(lock);
std::string of;
IFileSystem *system;
if (MapFilePath(filename, of, &system))
@ -283,6 +307,7 @@ PSPFileInfo MetaFileSystem::GetFileInfo(std::string filename)
bool MetaFileSystem::GetHostPath(const std::string &inpath, std::string &outpath)
{
lock_guard guard(lock);
std::string of;
IFileSystem *system;
if (MapFilePath(inpath, of, &system)) {
@ -294,6 +319,7 @@ bool MetaFileSystem::GetHostPath(const std::string &inpath, std::string &outpath
std::vector<PSPFileInfo> MetaFileSystem::GetDirListing(std::string path)
{
lock_guard guard(lock);
std::string of;
IFileSystem *system;
if (MapFilePath(path, of, &system))
@ -309,11 +335,13 @@ std::vector<PSPFileInfo> MetaFileSystem::GetDirListing(std::string path)
void MetaFileSystem::ThreadEnded(int threadID)
{
lock_guard guard(lock);
currentDir.erase(threadID);
}
int MetaFileSystem::ChDir(const std::string &dir)
{
lock_guard guard(lock);
// Retain the old path and fail if the arg is 1023 bytes or longer.
if (dir.size() >= 1023)
return SCE_KERNEL_ERROR_NAMETOOLONG;
@ -348,6 +376,7 @@ int MetaFileSystem::ChDir(const std::string &dir)
bool MetaFileSystem::MkDir(const std::string &dirname)
{
lock_guard guard(lock);
std::string of;
IFileSystem *system;
if (MapFilePath(dirname, of, &system))
@ -362,6 +391,7 @@ bool MetaFileSystem::MkDir(const std::string &dirname)
bool MetaFileSystem::RmDir(const std::string &dirname)
{
lock_guard guard(lock);
std::string of;
IFileSystem *system;
if (MapFilePath(dirname, of, &system))
@ -376,6 +406,7 @@ bool MetaFileSystem::RmDir(const std::string &dirname)
int MetaFileSystem::RenameFile(const std::string &from, const std::string &to)
{
lock_guard guard(lock);
std::string of;
std::string rf;
IFileSystem *osystem;
@ -407,6 +438,7 @@ int MetaFileSystem::RenameFile(const std::string &from, const std::string &to)
bool MetaFileSystem::RemoveFile(const std::string &filename)
{
lock_guard guard(lock);
std::string of;
IFileSystem *system;
if (MapFilePath(filename, of, &system))
@ -421,6 +453,7 @@ bool MetaFileSystem::RemoveFile(const std::string &filename)
void MetaFileSystem::CloseFile(u32 handle)
{
lock_guard guard(lock);
IFileSystem *sys = GetHandleOwner(handle);
if (sys)
sys->CloseFile(handle);
@ -428,6 +461,7 @@ void MetaFileSystem::CloseFile(u32 handle)
size_t MetaFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size)
{
lock_guard guard(lock);
IFileSystem *sys = GetHandleOwner(handle);
if (sys)
return sys->ReadFile(handle,pointer,size);
@ -437,6 +471,7 @@ size_t MetaFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size)
size_t MetaFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size)
{
lock_guard guard(lock);
IFileSystem *sys = GetHandleOwner(handle);
if (sys)
return sys->WriteFile(handle,pointer,size);
@ -446,6 +481,7 @@ size_t MetaFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size)
size_t MetaFileSystem::SeekFile(u32 handle, s32 position, FileMove type)
{
lock_guard guard(lock);
IFileSystem *sys = GetHandleOwner(handle);
if (sys)
return sys->SeekFile(handle,position,type);
@ -455,6 +491,7 @@ size_t MetaFileSystem::SeekFile(u32 handle, s32 position, FileMove type)
void MetaFileSystem::DoState(PointerWrap &p)
{
lock_guard guard(lock);
p.Do(current);
// Save/load per-thread current directory map

View File

@ -17,7 +17,8 @@
#pragma once
#include "FileSystem.h"
#include "native/base/mutex.h"
#include "Core/FileSystems/FileSystem.h"
class MetaFileSystem : public IHandleAllocator, public IFileSystem
{
@ -27,6 +28,11 @@ private:
{
std::string prefix;
IFileSystem *system;
bool operator == (const MountPoint &other) const
{
return prefix == other.prefix && system == other.system;
}
};
std::vector<MountPoint> fileSystems;
@ -34,6 +40,8 @@ private:
currentDir_t currentDir;
std::string startingDirectory;
int lastOpenError;
recursive_mutex lock;
public:
MetaFileSystem()
@ -42,7 +50,7 @@ public:
}
void Mount(std::string prefix, IFileSystem *system);
void Unmount(IFileSystem *system);
void Unmount(std::string prefix, IFileSystem *system);
void ThreadEnded(int threadID);
@ -72,7 +80,8 @@ public:
bool GetHostPath(const std::string &inpath, std::string &outpath);
std::vector<PSPFileInfo> GetDirListing(std::string path);
u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL);
u32 OpenFile(std::string filename, FileAccess access, const char *devicename = NULL);
u32 OpenWithError(int &error, std::string filename, FileAccess access, const char *devicename = NULL);
void CloseFile(u32 handle);
size_t ReadFile(u32 handle, u8 *pointer, s64 size);
size_t WriteFile(u32 handle, const u8 *pointer, s64 size);
@ -94,6 +103,7 @@ public:
// TODO: void IoCtl(...)
void SetStartingDirectory(const std::string &dir) {
lock_guard guard(lock);
startingDirectory = dir;
}
};

View File

@ -431,6 +431,12 @@ int sceCccIsValidJIS(u32 c)
return c != 0;
}
int sceCccIsValidUnicode(u32 c)
{
WARN_LOG(HLE, "UNIMPL sceCccIsValidUnicode(%08x)", c);
return c != 0;
}
u32 sceCccSetErrorCharUTF8(u32 c)
{
DEBUG_LOG(HLE, "sceCccSetErrorCharUTF8(%08x)", c);
@ -507,6 +513,7 @@ const HLEFunction sceCcc[] =
{0x76e33e9c, WrapI_U<sceCccIsValidUCS2>, "sceCccIsValidUCS2"},
{0xd2b18485, WrapI_U<sceCccIsValidUCS4>, "sceCccIsValidUCS4"},
{0xa2d5d209, WrapI_U<sceCccIsValidJIS>, "sceCccIsValidJIS"},
{0xbd11eef3, WrapI_U<sceCccIsValidUnicode>, "sceCccIsValidUnicode"},
{0x17e1d813, WrapU_U<sceCccSetErrorCharUTF8>, "sceCccSetErrorCharUTF8"},
{0xb8476cf4, WrapU_U<sceCccSetErrorCharUTF16>, "sceCccSetErrorCharUTF16"},
{0xc56949ad, WrapU_U<sceCccSetErrorCharSJIS>, "sceCccSetErrorCharSJIS"},

View File

@ -103,8 +103,6 @@ const int PSP_MIN_FD = 4;
static int asyncNotifyEvent = -1;
static SceUID fds[PSP_COUNT_FDS];
u32 ioErrorCode = 0;
#define SCE_STM_FDIR 0x1000
#define SCE_STM_FREG 0x2000
#define SCE_STM_FLNK 0x4000
@ -294,6 +292,13 @@ void __IoAsyncNotify(u64 userdata, int cyclesLate) {
}
}
static DirectoryFileSystem *memstickSystem = NULL;
#ifdef ANDROID
static VFSFileSystem *flash0System = NULL;
#else
static DirectoryFileSystem *flash0System = NULL;
#endif
void __IoInit() {
INFO_LOG(HLE, "Starting up I/O...");
@ -305,16 +310,16 @@ void __IoInit() {
std::string flash0path;
GetSysDirectories(memstickpath, flash0path);
DirectoryFileSystem *memstick = new DirectoryFileSystem(&pspFileSystem, memstickpath);
memstickSystem = new DirectoryFileSystem(&pspFileSystem, memstickpath);
#ifdef ANDROID
VFSFileSystem *flash0 = new VFSFileSystem(&pspFileSystem, "flash0");
flash0System = new VFSFileSystem(&pspFileSystem, "flash0");
#else
DirectoryFileSystem *flash0 = new DirectoryFileSystem(&pspFileSystem, flash0path);
flash0System = new DirectoryFileSystem(&pspFileSystem, flash0path);
#endif
pspFileSystem.Mount("ms0:", memstick);
pspFileSystem.Mount("fatms0:", memstick);
pspFileSystem.Mount("fatms:", memstick);
pspFileSystem.Mount("flash0:", flash0);
pspFileSystem.Mount("ms0:", memstickSystem);
pspFileSystem.Mount("fatms0:", memstickSystem);
pspFileSystem.Mount("fatms:", memstickSystem);
pspFileSystem.Mount("flash0:", flash0System);
__KernelListenThreadEnd(&TellFsThreadEnded);
@ -328,6 +333,15 @@ void __IoDoState(PointerWrap &p) {
}
void __IoShutdown() {
pspFileSystem.Unmount("ms0:", memstickSystem);
pspFileSystem.Unmount("fatms0:", memstickSystem);
pspFileSystem.Unmount("fatms:", memstickSystem);
pspFileSystem.Unmount("flash0:", flash0System);
delete memstickSystem;
memstickSystem = NULL;
delete flash0System;
flash0System = NULL;
}
u32 __IoGetFileHandleFromId(u32 id, u32 &outError)
@ -782,7 +796,7 @@ u32 sceIoLseek32Async(int id, int offset, int whence) {
return 0;
}
FileNode *__IoOpen(const char* filename, int flags, int mode) {
FileNode *__IoOpen(int &error, const char* filename, int flags, int mode) {
//memory stick filename
int access = FILEACCESS_NONE;
if (flags & O_RDONLY)
@ -796,9 +810,7 @@ FileNode *__IoOpen(const char* filename, int flags, int mode) {
PSPFileInfo info = pspFileSystem.GetFileInfo(filename);
ioErrorCode = 0;
u32 h = pspFileSystem.OpenFile(filename, (FileAccess) access);
u32 h = pspFileSystem.OpenWithError(error, filename, (FileAccess) access);
if (h == 0) {
return NULL;
}
@ -821,11 +833,12 @@ u32 sceIoOpen(const char* filename, int flags, int mode) {
if (!__KernelIsDispatchEnabled())
return -1;
FileNode *f = __IoOpen(filename, flags, mode);
int error;
FileNode *f = __IoOpen(error, filename, flags, mode);
if (f == NULL)
{
//Timing is not accurate, aiming low for now.
if (ioErrorCode == SCE_KERNEL_ERROR_NOCWD)
// Timing is not accurate, aiming low for now.
if (error == SCE_KERNEL_ERROR_NOCWD)
{
ERROR_LOG(HLE, "SCE_KERNEL_ERROR_NOCWD=sceIoOpen(%s, %08x, %08x) - no current working directory", filename, flags, mode);
return hleDelayResult(SCE_KERNEL_ERROR_NOCWD , "no cwd", 10000);
@ -1294,7 +1307,8 @@ u32 sceIoOpenAsync(const char *filename, int flags, int mode)
if (!__KernelIsDispatchEnabled())
sceKernelResumeDispatchThread(1);
FileNode *f = __IoOpen(filename, flags, mode);
int error;
FileNode *f = __IoOpen(error, filename, flags, mode);
int fd;
// We have to return an fd here, which may have been destroyed when we reach Wait if it failed.
@ -1305,7 +1319,7 @@ u32 sceIoOpenAsync(const char *filename, int flags, int mode)
f = new FileNode();
f->handle = kernelObjects.Create(f);
f->fullpath = filename;
f->asyncResult = ERROR_ERRNO_FILE_NOT_FOUND;
f->asyncResult = error == 0 ? ERROR_ERRNO_FILE_NOT_FOUND : error;
f->closePending = true;
fd = __IoAllocFd(f);

View File

@ -88,6 +88,7 @@ namespace SaveState
if (Core_IsInactive() && __KernelIsRunning())
{
// Warning: this may run on a different thread.
needsProcess = true;
Process();
}
else

View File

@ -116,6 +116,8 @@ void CPU_WaitStatus(bool (*pred)()) {
cpuThreadLock.unlock();
}
void CPU_Shutdown();
void CPU_Init() {
currentCPU = &mipsr4k;
numCPUs = 1;
@ -163,12 +165,7 @@ void CPU_Init() {
// Why did we check for CORE_POWERDOWN here?
if (!LoadFile(filename, &coreParameter.errorString)) {
pspFileSystem.Shutdown();
CoreTiming::Shutdown();
__KernelShutdown();
HLEShutdown();
host->ShutdownSound();
Memory::Shutdown();
CPU_Shutdown();
coreParameter.fileToStart = "";
CPU_SetState(CPU_THREAD_NOT_RUNNING);
return;
@ -182,20 +179,18 @@ void CPU_Init() {
}
void CPU_Shutdown() {
pspFileSystem.Shutdown();
CoreTiming::Shutdown();
if (g_Config.bAutoSaveSymbolMap) {
host->SaveSymbolMap();
}
CoreTiming::Shutdown();
__KernelShutdown();
HLEShutdown();
if (coreParameter.enableSound) {
host->ShutdownSound();
mixer = 0; // deleted in ShutdownSound
}
__KernelShutdown();
HLEShutdown();
pspFileSystem.Shutdown();
Memory::Shutdown();
currentCPU = 0;
}

116
Core/ThreadEventQueue.h Normal file
View File

@ -0,0 +1,116 @@
// Copyright (c) 2013- 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
// the Free Software Foundation, version 2.0 or later versions.
// 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/.
#pragma once
#include "native/base/mutex.h"
#include "Core/CoreTiming.h"
#include <deque>
template <typename B, typename Event, typename EventType, EventType EVENT_INVALID, EventType EVENT_SYNC, EventType EVENT_FINISH>
struct ThreadEventQueue : public B {
void SetThreadEnabled(bool threadEnabled) {
threadEnabled_ = threadEnabled;
}
void ScheduleEvent(Event ev) {
{
lock_guard guard(eventsLock_);
events_.push_back(ev);
eventsWait_.notify_one();
}
if (!threadEnabled_) {
RunEventsUntil(0);
}
}
bool HasEvents() {
lock_guard guard(eventsLock_);
return !events_.empty();
}
Event GetNextEvent() {
lock_guard guard(eventsLock_);
if (events_.empty()) {
eventsDrain_.notify_one();
return EVENT_INVALID;
}
Event ev = events_.front();
events_.pop_front();
return ev;
}
void RunEventsUntil(u64 globalticks) {
do {
for (Event ev = GetNextEvent(); EventType(ev) != EVENT_INVALID; ev = GetNextEvent()) {
switch (EventType(ev)) {
case EVENT_FINISH:
// Stop waiting.
globalticks = 0;
break;
case EVENT_SYNC:
break;
default:
ProcessEvent(ev);
}
}
// Quit the loop if the queue is drained and coreState has tripped, or threading is disabled.
if (coreState != CORE_RUNNING || !threadEnabled_) {
return;
}
// coreState changes won't wake us, so recheck periodically.
eventsWait_.wait_for(eventsWaitLock_, 1);
} while (CoreTiming::GetTicks() < globalticks);
}
void SyncThread() {
if (!threadEnabled_) {
return;
}
// While processing the last event, HasEvents() will be false even while not done.
// So we schedule a nothing event and wait for that to finish.
ScheduleEvent(EVENT_SYNC);
while (HasEvents() && coreState == CORE_RUNNING) {
eventsDrain_.wait_for(eventsDrainLock_, 1);
}
}
void FinishEventLoop() {
if (threadEnabled_) {
ScheduleEvent(EVENT_FINISH);
}
}
protected:
virtual void ProcessEvent(Event ev) = 0;
private:
bool threadEnabled_;
std::deque<Event> events_;
recursive_mutex eventsLock_;
recursive_mutex eventsWaitLock_;
recursive_mutex eventsDrainLock_;
condition_variable eventsWait_;
condition_variable eventsDrain_;
};

View File

@ -1,103 +1,8 @@
#pragma once
#include "native/base/mutex.h"
#include "Core/ThreadEventQueue.h"
#include "GPU/GPUInterface.h"
#include "Core/CoreTiming.h"
#include <deque>
template <typename B, typename Event, typename EventType, EventType EVENT_INVALID, EventType EVENT_SYNC, EventType EVENT_FINISH>
struct ThreadEventQueue : public B {
void SetThreadEnabled(bool threadEnabled) {
threadEnabled_ = threadEnabled;
}
void ScheduleEvent(Event ev) {
{
lock_guard guard(eventsLock_);
events_.push_back(ev);
eventsWait_.notify_one();
}
if (!threadEnabled_) {
RunEventsUntil(0);
}
}
bool HasEvents() {
lock_guard guard(eventsLock_);
return !events_.empty();
}
Event GetNextEvent() {
lock_guard guard(eventsLock_);
if (events_.empty()) {
eventsDrain_.notify_one();
return EVENT_INVALID;
}
Event ev = events_.front();
events_.pop_front();
return ev;
}
void RunEventsUntil(u64 globalticks) {
do {
for (Event ev = GetNextEvent(); EventType(ev) != EVENT_INVALID; ev = GetNextEvent()) {
switch (EventType(ev)) {
case EVENT_FINISH:
// Stop waiting.
globalticks = 0;
break;
case EVENT_SYNC:
break;
default:
ProcessEvent(ev);
}
}
// Quit the loop if the queue is drained and coreState has tripped, or threading is disabled.
if (coreState != CORE_RUNNING || !threadEnabled_) {
return;
}
// coreState changes won't wake us, so recheck periodically.
eventsWait_.wait_for(eventsWaitLock_, 1);
} while (CoreTiming::GetTicks() < globalticks);
}
void SyncThread() {
if (!threadEnabled_) {
return;
}
// While processing the last event, HasEvents() will be false even while not done.
// So we schedule a nothing event and wait for that to finish.
ScheduleEvent(EVENT_SYNC);
while (HasEvents() && coreState == CORE_RUNNING) {
eventsDrain_.wait_for(eventsDrainLock_, 1);
}
}
void FinishEventLoop() {
if (threadEnabled_) {
ScheduleEvent(EVENT_FINISH);
}
}
protected:
virtual void ProcessEvent(Event ev) = 0;
private:
bool threadEnabled_;
std::deque<Event> events_;
recursive_mutex eventsLock_;
recursive_mutex eventsWaitLock_;
recursive_mutex eventsDrainLock_;
condition_variable eventsWait_;
condition_variable eventsDrain_;
};
typedef ThreadEventQueue<GPUInterface, GPUEvent, GPUEventType, GPU_EVENT_INVALID, GPU_EVENT_SYNC_THREAD, GPU_EVENT_FINISH_EVENT_LOOP> GPUThreadEventQueue;
class GPUCommon : public GPUThreadEventQueue