Skip to content

Commit

Permalink
Async IO: return ERROR_KERNEL_NO_MEMORY when the stack for the AsyncIO
Browse files Browse the repository at this point in the history
thread could not be allocated.
  • Loading branch information
gid15 committed May 10, 2015
1 parent d848e5f commit 1697d3d
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/jpcsp/HLE/modules150/IoFileMgrForUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1165,8 +1165,7 @@ private long updateResult(IoInfo info, long result, boolean async, boolean resul
if (info != null && result != ERROR_KERNEL_ASYNC_BUSY) {
if (async) {
if (!info.asyncPending) {
startIoAsync(info, result, ioOperation, asyncAction, size);
result = 0;
result = startIoAsync(info, result, ioOperation, asyncAction, size);
}
} else {
info.result = ERROR_KERNEL_NO_ASYNC_OP;
Expand Down Expand Up @@ -1292,9 +1291,10 @@ private void triggerAsyncThread(IoInfo info) {
* @param info the file
* @param result the result the async IO should return
*/
private void startIoAsync(IoInfo info, long result, IoOperation ioOperation, IAction asyncAction, int size) {
private int startIoAsync(IoInfo info, long result, IoOperation ioOperation, IAction asyncAction, int size) {
int startResult = 0;
if (info == null) {
return;
return startResult;
}
info.asyncPending = true;
info.asyncResultPending = false;
Expand Down Expand Up @@ -1322,19 +1322,29 @@ private void startIoAsync(IoInfo info, long result, IoOperation ioOperation, IAc
info.asyncThread = threadMan.hleKernelCreateThread("SceIofileAsync",
ThreadManForUser.ASYNC_LOOP_ADDRESS, asyncPriority, stackSize,
threadMan.getCurrentThread().attr, 0, SysMemUserForUser.USER_PARTITION_ID);
if (log.isDebugEnabled()) {
log.debug(String.format("Starting Async IO thread %s", info.asyncThread));
}
// This must be the last action of the hleIoXXX call because it can context-switch
// Inherit $gp from this process ($gp can be used by interrupts)
threadMan.hleKernelStartThread(info.asyncThread, 0, 0, info.asyncThread.gpReg_addr);

// Copy uid to Async Thread argument register after starting the thread
// (all registers are reset when starting the thread).
info.asyncThread.cpuContext.setRegister(asyncThreadRegisterArgument, info.uid);
if (info.asyncThread.getStackAddr() == 0) {
log.warn(String.format("Cannot start the Async IO thread, not enough memory to create its stack"));
threadMan.hleDeleteThread(info.asyncThread);
info.asyncThread = null;
startResult = SceKernelErrors.ERROR_KERNEL_NO_MEMORY;
} else {
if (log.isDebugEnabled()) {
log.debug(String.format("Starting Async IO thread %s", info.asyncThread));
}
// This must be the last action of the hleIoXXX call because it can context-switch
// Inherit $gp from this process ($gp can be used by interrupts)
threadMan.hleKernelStartThread(info.asyncThread, 0, 0, info.asyncThread.gpReg_addr);

// Copy uid to Async Thread argument register after starting the thread
// (all registers are reset when starting the thread).
info.asyncThread.cpuContext.setRegister(asyncThreadRegisterArgument, info.uid);
}
} else {
triggerAsyncThread(info);
}

return startResult;
}

private boolean doStepAsync(IoInfo info) {
Expand Down Expand Up @@ -1517,7 +1527,7 @@ public int hleIoWaitAsync(int id, int res_addr, boolean wait, boolean callbacks)
return ERROR_KERNEL_BAD_FILE_DESCRIPTOR;
}

if (info.result == ERROR_KERNEL_NO_ASYNC_OP) {
if (info.result == ERROR_KERNEL_NO_ASYNC_OP || info.asyncThread == null) {
log.debug("hleIoWaitAsync - PSP_ERROR_NO_ASYNC_OP");
return ERROR_KERNEL_NO_ASYNC_OP;
}
Expand Down Expand Up @@ -1807,7 +1817,10 @@ public int hleIoOpen(int filename_addr, String filename, int flags, int permissi
result = info.id;
}

startIoAsync(info, realResult, IoOperation.open, null, 0);
int startResult = startIoAsync(info, realResult, IoOperation.open, null, 0);
if (startResult < 0) {
result = startResult;
}
}

return result;
Expand Down

2 comments on commit 1697d3d

@sum2012
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sum2012
Copy link
Contributor

@sum2012 sum2012 commented on 1697d3d Nov 2, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0ad1fc8

Please sign in to comment.