2007-05-30 21:56:52 +00:00
|
|
|
/* 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.
|
2005-03-31 05:35:04 +00:00
|
|
|
*
|
|
|
|
* 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
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-03-31 05:35:04 +00:00
|
|
|
*
|
2006-02-11 12:47:47 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2005-03-31 05:35:04 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "asyncfio.h"
|
|
|
|
#include <tamtypes.h>
|
|
|
|
#include <kernel.h>
|
|
|
|
#include <fileio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2005-05-05 03:06:32 +00:00
|
|
|
#include <fileXio_rpc.h>
|
2008-03-03 00:41:59 +00:00
|
|
|
#include "backends/platform/ps2/ps2debug.h"
|
2005-05-05 03:06:32 +00:00
|
|
|
|
|
|
|
#define DEFAULT_MODE (FIO_S_IRUSR | FIO_S_IWUSR | FIO_S_IRGRP | FIO_S_IWGRP | FIO_S_IROTH | FIO_S_IWOTH)
|
2005-03-31 05:35:04 +00:00
|
|
|
|
|
|
|
AsyncFio::AsyncFio(void) {
|
|
|
|
_runningOp = NULL;
|
2005-05-05 03:06:32 +00:00
|
|
|
memset((int *)_ioSlots, 0, MAX_HANDLES * sizeof(int));
|
2005-03-31 05:35:04 +00:00
|
|
|
ee_sema_t newSema;
|
|
|
|
newSema.init_count = 1;
|
|
|
|
newSema.max_count = 1;
|
|
|
|
_ioSema = CreateSema(&newSema);
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncFio::~AsyncFio(void) {
|
|
|
|
DeleteSema(_ioSema);
|
|
|
|
}
|
|
|
|
|
|
|
|
int AsyncFio::open(const char *name, int ioMode) {
|
|
|
|
WaitSema(_ioSema);
|
|
|
|
checkSync();
|
|
|
|
int res;
|
2005-05-05 03:06:32 +00:00
|
|
|
fileXioOpen(name, ioMode, DEFAULT_MODE);
|
2005-05-11 07:31:44 +00:00
|
|
|
fileXioWaitAsync(FXIO_WAIT, &res);
|
2005-03-31 05:35:04 +00:00
|
|
|
SignalSema(_ioSema);
|
2008-03-03 00:41:59 +00:00
|
|
|
//dbg_printf("FIO: open(%s, %d) => %d\n", name, ioMode, res);
|
2005-03-31 05:35:04 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:55:00 +00:00
|
|
|
int AsyncFio::open(const char *name, int ioMode, int mode) {
|
|
|
|
WaitSema(_ioSema);
|
|
|
|
checkSync();
|
|
|
|
int res;
|
|
|
|
fileXioOpen(name, ioMode, mode);
|
|
|
|
fileXioWaitAsync(FXIO_WAIT, &res);
|
|
|
|
SignalSema(_ioSema);
|
|
|
|
// dbg_printf("FIO: open ext(%s, %d, %d) => %d", name, ioMode, mode, res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-03-31 05:35:04 +00:00
|
|
|
void AsyncFio::close(int handle) {
|
|
|
|
WaitSema(_ioSema);
|
|
|
|
checkSync();
|
2005-05-05 03:06:32 +00:00
|
|
|
fileXioClose(handle);
|
|
|
|
int res;
|
2005-05-11 07:31:44 +00:00
|
|
|
fileXioWaitAsync(FXIO_WAIT, &res);
|
2005-11-09 19:51:57 +00:00
|
|
|
if (res != 0)
|
2008-03-03 00:41:59 +00:00
|
|
|
sioprintf("ERROR: fileXioClose failed, EC %d\n", res);
|
2005-05-05 03:06:32 +00:00
|
|
|
_ioSlots[handle] = 0;
|
2005-03-31 05:35:04 +00:00
|
|
|
SignalSema(_ioSema);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncFio::checkSync(void) {
|
|
|
|
if (_runningOp) {
|
2005-05-11 07:31:44 +00:00
|
|
|
fileXioWaitAsync(FXIO_WAIT, (int *)_runningOp);
|
2005-03-31 05:35:04 +00:00
|
|
|
_runningOp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncFio::read(int fd, void *dest, unsigned int len) {
|
|
|
|
WaitSema(_ioSema);
|
|
|
|
checkSync();
|
|
|
|
assert(fd < MAX_HANDLES);
|
|
|
|
_runningOp = _ioSlots + fd;
|
2005-05-05 03:06:32 +00:00
|
|
|
fileXioRead(fd, (unsigned char*)dest, len);
|
2005-03-31 05:35:04 +00:00
|
|
|
SignalSema(_ioSema);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncFio::write(int fd, const void *src, unsigned int len) {
|
|
|
|
WaitSema(_ioSema);
|
|
|
|
checkSync();
|
|
|
|
assert(fd < MAX_HANDLES);
|
|
|
|
_runningOp = _ioSlots + fd;
|
2005-05-05 03:06:32 +00:00
|
|
|
fileXioWrite(fd, (unsigned char*)src, len);
|
2005-03-31 05:35:04 +00:00
|
|
|
SignalSema(_ioSema);
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:55:00 +00:00
|
|
|
void AsyncFio::remove(const char *path) {
|
|
|
|
int res;
|
|
|
|
WaitSema(_ioSema);
|
|
|
|
checkSync();
|
|
|
|
fileXioRemove(path);
|
|
|
|
fileXioWaitAsync(FXIO_WAIT, &res);
|
|
|
|
SignalSema(_ioSema);
|
|
|
|
}
|
|
|
|
|
2005-03-31 05:35:04 +00:00
|
|
|
int AsyncFio::seek(int fd, int offset, int whence) {
|
2005-05-11 07:31:44 +00:00
|
|
|
int res;
|
2005-03-31 05:35:04 +00:00
|
|
|
WaitSema(_ioSema);
|
|
|
|
checkSync();
|
2005-05-05 03:06:32 +00:00
|
|
|
fileXioLseek(fd, offset, whence);
|
2005-05-11 07:31:44 +00:00
|
|
|
fileXioWaitAsync(FXIO_WAIT, &res);
|
2005-05-05 03:06:32 +00:00
|
|
|
SignalSema(_ioSema);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AsyncFio::mkdir(const char *name) {
|
2005-05-11 07:31:44 +00:00
|
|
|
int res;
|
2005-05-05 03:06:32 +00:00
|
|
|
WaitSema(_ioSema);
|
|
|
|
checkSync();
|
|
|
|
fileXioMkdir(name, DEFAULT_MODE);
|
2005-05-11 07:31:44 +00:00
|
|
|
fileXioWaitAsync(FXIO_WAIT, &res);
|
|
|
|
SignalSema(_ioSema);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AsyncFio::dopen(const char *name) {
|
2005-05-05 03:06:32 +00:00
|
|
|
int res;
|
2005-05-11 07:31:44 +00:00
|
|
|
WaitSema(_ioSema);
|
|
|
|
checkSync();
|
2005-07-30 21:11:48 +00:00
|
|
|
fileXioDopen(name);
|
2005-05-11 07:31:44 +00:00
|
|
|
fileXioWaitAsync(FXIO_WAIT, &res);
|
2005-03-31 05:35:04 +00:00
|
|
|
SignalSema(_ioSema);
|
2008-03-03 00:41:59 +00:00
|
|
|
dbg_printf("FIO: dopen(%s) => %d\n", name, res);
|
2005-03-31 05:35:04 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-05-11 07:31:44 +00:00
|
|
|
int AsyncFio::dread(int fd, iox_dirent_t *dest) {
|
|
|
|
int res;
|
|
|
|
WaitSema(_ioSema);
|
|
|
|
checkSync();
|
|
|
|
fileXioDread(fd, dest);
|
|
|
|
fileXioWaitAsync(FXIO_WAIT, &res);
|
|
|
|
SignalSema(_ioSema);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncFio::dclose(int fd) {
|
|
|
|
int res;
|
|
|
|
WaitSema(_ioSema);
|
|
|
|
checkSync();
|
|
|
|
fileXioDclose(fd);
|
|
|
|
fileXioWaitAsync(FXIO_WAIT, &res);
|
2008-03-03 00:41:59 +00:00
|
|
|
//assert(res == 0);
|
|
|
|
dbg_printf("FIO: dclose(%d) => %d\n", fd, res);
|
|
|
|
if (res != 0)
|
|
|
|
sioprintf("ERROR: fileXioDclose failed, EC %d\n", res);
|
2005-05-11 07:31:44 +00:00
|
|
|
SignalSema(_ioSema);
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:55:00 +00:00
|
|
|
int AsyncFio::chdir(const char *name) {
|
|
|
|
int res;
|
|
|
|
WaitSema(_ioSema);
|
|
|
|
checkSync();
|
|
|
|
fileXioChdir(name);
|
|
|
|
fileXioWaitAsync(FXIO_WAIT, &res);
|
|
|
|
SignalSema(_ioSema);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-06-07 06:07:05 +00:00
|
|
|
int AsyncFio::mount(const char *mountpoint, const char *mountstring, int flag) {
|
|
|
|
int res;
|
|
|
|
WaitSema(_ioSema);
|
|
|
|
checkSync();
|
|
|
|
fileXioMount(mountpoint, mountstring, flag);
|
|
|
|
fileXioWaitAsync(FXIO_WAIT, &res);
|
|
|
|
SignalSema(_ioSema);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AsyncFio::umount(const char *mountpoint) {
|
|
|
|
int res;
|
|
|
|
WaitSema(_ioSema);
|
|
|
|
checkSync();
|
|
|
|
fileXioUmount(mountpoint);
|
|
|
|
fileXioWaitAsync(FXIO_WAIT, &res);
|
|
|
|
SignalSema(_ioSema);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-03-31 05:35:04 +00:00
|
|
|
int AsyncFio::sync(int fd) {
|
|
|
|
WaitSema(_ioSema);
|
|
|
|
if (_runningOp == _ioSlots + fd)
|
|
|
|
checkSync();
|
2005-05-05 03:06:32 +00:00
|
|
|
int res = _ioSlots[fd];
|
|
|
|
_ioSlots[fd] = 0;
|
2005-03-31 05:35:04 +00:00
|
|
|
SignalSema(_ioSema);
|
2005-05-05 03:06:32 +00:00
|
|
|
return res;
|
2005-03-31 05:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AsyncFio::poll(int fd) {
|
|
|
|
bool retVal = false;
|
2005-05-05 03:06:32 +00:00
|
|
|
if (PollSema(_ioSema) >= 0) {
|
2005-03-31 05:35:04 +00:00
|
|
|
if (_runningOp == _ioSlots + fd) {
|
2005-05-05 03:06:32 +00:00
|
|
|
if (fileXioWaitAsync(FXIO_NOWAIT, (int *)_runningOp) == FXIO_COMPLETE) {
|
2005-03-31 05:35:04 +00:00
|
|
|
_runningOp = NULL;
|
|
|
|
retVal = true;
|
|
|
|
} else
|
|
|
|
retVal = false;
|
|
|
|
} else
|
|
|
|
retVal = true;
|
|
|
|
SignalSema(_ioSema);
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AsyncFio::fioAvail(void) {
|
|
|
|
bool retVal = false;
|
|
|
|
if (PollSema(_ioSema) > 0) {
|
|
|
|
if (_runningOp) {
|
2005-05-05 03:06:32 +00:00
|
|
|
if (fileXioWaitAsync(FXIO_NOWAIT, (int *)_runningOp) == FXIO_COMPLETE) {
|
2005-03-31 05:35:04 +00:00
|
|
|
_runningOp = NULL;
|
|
|
|
retVal = true;
|
|
|
|
} else
|
|
|
|
retVal = false;
|
|
|
|
} else
|
|
|
|
retVal = true;
|
|
|
|
SignalSema(_ioSema);
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|