Skip to content

Commit

Permalink
Framework for dealing with blocking MojoInputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed May 7, 2007
1 parent e671e16 commit 336b7ea
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 4 deletions.
16 changes: 16 additions & 0 deletions archive_tar.c
Expand Up @@ -39,6 +39,10 @@ static void initializeZStream(z_stream *pstr)
pstr->zfree = mojoZlibFree;
} // initializeZStream

static boolean MojoInput_gzip_ready(MojoInput *io)
{
return true;
} // MojoInput_gzip_ready

static boolean MojoInput_gzip_seek(MojoInput *io, uint64 offset)
{
Expand Down Expand Up @@ -182,6 +186,7 @@ static MojoInput *make_gzip_input(MojoInput *origio)
info->buffer = (uint8 *) xmalloc(GZIP_READBUFSIZE);

io = (MojoInput *) xmalloc(sizeof (MojoInput));
io->ready = MojoInput_gzip_ready;
io->read = MojoInput_gzip_read;
io->seek = MojoInput_gzip_seek;
io->tell = MojoInput_gzip_tell;
Expand Down Expand Up @@ -229,6 +234,10 @@ static void initializeBZ2Stream(bz_stream *pstr)
pstr->bzfree = mojoBzlib2Free;
} // initializeBZ2Stream

static boolean MojoInput_bzip2_ready(MojoInput *io)
{
return true;
} // MojoInput_bzip2_ready

static boolean MojoInput_bzip2_seek(MojoInput *io, uint64 offset)
{
Expand Down Expand Up @@ -385,6 +394,7 @@ static MojoInput *make_bzip2_input(MojoInput *origio)
info->buffer = (uint8 *) xmalloc(BZIP2_READBUFSIZE);

io = (MojoInput *) xmalloc(sizeof (MojoInput));
io->ready = MojoInput_bzip2_ready;
io->read = MojoInput_bzip2_read;
io->seek = MojoInput_bzip2_seek;
io->tell = MojoInput_bzip2_tell;
Expand Down Expand Up @@ -417,6 +427,11 @@ typedef struct TARinfo
uint64 nextEnumPos;
} TARinfo;

static boolean MojoInput_tar_ready(MojoInput *io)
{
return true;
} // MojoInput_tar_ready

static int64 MojoInput_tar_read(MojoInput *io, void *buf, uint32 bufsize)
{
TARinput *input = (TARinput *) io->opaque;
Expand Down Expand Up @@ -666,6 +681,7 @@ static MojoInput *MojoArchive_tar_openCurrentEntry(MojoArchive *ar)
opaque->offset = info->curFileStart;

io = (MojoInput *) xmalloc(sizeof (MojoInput));
io->ready = MojoInput_tar_ready;
io->read = MojoInput_tar_read;
io->seek = MojoInput_tar_seek;
io->tell = MojoInput_tar_tell;
Expand Down
6 changes: 6 additions & 0 deletions archive_zip.c
Expand Up @@ -1664,6 +1664,11 @@ const PHYSFS_Archiver __PHYSFS_Archiver_ZIP =

// MojoInput implementation...

static boolean MojoInput_zip_ready(MojoInput *io)
{
return true;
} // MojoInput_zip_ready

static int64 MojoInput_zip_read(MojoInput *io, void *buf, uint32 bufsize)
{
return ZIP_read(io->opaque, buf, 1, bufsize);
Expand Down Expand Up @@ -1758,6 +1763,7 @@ static MojoInput *buildZipMojoInput(ZIPinfo *info, const char *fullpath)
return NULL;

io = (MojoInput *) xmalloc(sizeof (MojoInput));
io->ready = MojoInput_zip_ready;
io->read = MojoInput_zip_read;
io->seek = MojoInput_zip_seek;
io->tell = MojoInput_zip_tell;
Expand Down
14 changes: 10 additions & 4 deletions fileio.c
Expand Up @@ -98,10 +98,9 @@ boolean MojoInput_toPhysicalFile(MojoInput *in, const char *fname, uint16 perms,
{
while (!iofailure)
{
// !!! FIXME: to be written
// if (!in->ready(in))
// MojoPlatform_sleep(100);
// else
if (!in->ready(in))
MojoPlatform_sleep(100);
else
{
br = in->read(in, scratchbuf_128k, sizeof (scratchbuf_128k));
if (br == 0) // we're done!
Expand Down Expand Up @@ -171,6 +170,12 @@ typedef struct
char *path;
} MojoInputFileInstance;

static boolean MojoInput_file_ready(MojoInput *io)
{
// !!! FIXME: select()? Does that help with network filesystems?
return true;
} // MojoInput_file_ready

static int64 MojoInput_file_read(MojoInput *io, void *buf, uint32 bufsize)
{
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
Expand Down Expand Up @@ -242,6 +247,7 @@ MojoInput *MojoInput_newFromFile(const char *path)
inst->handle = f;

io = (MojoInput *) xmalloc(sizeof (MojoInput));
io->ready = MojoInput_file_ready;
io->read = MojoInput_file_read;
io->seek = MojoInput_file_seek;
io->tell = MojoInput_file_tell;
Expand Down
1 change: 1 addition & 0 deletions fileio.h
Expand Up @@ -24,6 +24,7 @@ typedef struct MojoInput MojoInput;
struct MojoInput
{
// public
boolean (*ready)(MojoInput *io);
int64 (*read)(MojoInput *io, void *buf, uint32 bufsize);
boolean (*seek)(MojoInput *io, uint64 pos);
int64 (*tell)(MojoInput *io);
Expand Down
5 changes: 5 additions & 0 deletions libfetch/ftp.c
Expand Up @@ -508,6 +508,10 @@ static int _ftp_closefn(void *);


#if __MOJOSETUP__
static boolean MojoInput_ftp_ready(MojoInput *io)
{
return true; // !!! FIXME: select on the socket...
}
static int64 MojoInput_ftp_tell(MojoInput *v)
{
struct ftpio *io = (struct ftpio *)v->opaque;
Expand Down Expand Up @@ -696,6 +700,7 @@ _ftp_setup(conn_t *cconn, conn_t *dconn, int mode)
io->bytes_read = 0;
io->length = -1;
f = (MojoInput *) xmalloc(sizeof (MojoInput));
f->ready = MojoInput_ftp_ready;
f->read = MojoInput_ftp_read;
f->seek = MojoInput_ftp_seek;
f->tell = MojoInput_ftp_tell;
Expand Down
5 changes: 5 additions & 0 deletions libfetch/http.c
Expand Up @@ -137,6 +137,10 @@ struct httpio


#if __MOJOSETUP__
static boolean MojoInput_http_ready(MojoInput *io)
{
return true; // !!! FIXME: select on the socket...
}
static boolean MojoInput_http_seek(MojoInput *v, uint64 pos)
{
return -1;
Expand Down Expand Up @@ -387,6 +391,7 @@ _http_funopen(conn_t *conn, int chunked)
io->bytes_read = 0;
io->length = -1;
f = (MojoInput *) xmalloc(sizeof (MojoInput));
f->ready = MojoInput_http_ready;
f->read = MojoInput_http_read;
f->seek = MojoInput_http_seek;
f->tell = MojoInput_http_tell;
Expand Down
4 changes: 4 additions & 0 deletions platform.h
Expand Up @@ -66,6 +66,10 @@ void *MojoPlatform_dlopen(const uint8 *img, size_t len);
void *MojoPlatform_dlsym(void *lib, const char *sym);
void MojoPlatform_dlclose(void *lib);

// Put the calling process to sleep for at least (ticks) milliseconds.
// This is meant to yield the CPU while spinning in a loop that is polling
// for input, etc. Pumping the GUI event queue happens elsewhere, not here.
void MojoPlatform_sleep(uint32 ticks);

// Put a line of text to the the system log, whatever that might be on a
// given platform. (str) is a complete line, but won't end with any newline
Expand Down
7 changes: 7 additions & 0 deletions platform/beos.c
Expand Up @@ -5,6 +5,7 @@

#include <stdio.h>
#include <be/kernel/image.h>
#include <be/kernel/OS.h>

void *beos_dlopen(const char *fname, int unused)
{
Expand All @@ -30,5 +31,11 @@ void beos_dlclose(void *lib)
unload_add_on((image_id) lib);
} // beos_dlclose


void beos_usleep(unsigned long microseconds)
{
snooze(microseconds);
} // beos_usleep

// end of beos.c ...

8 changes: 8 additions & 0 deletions platform/unix.c
Expand Up @@ -32,9 +32,11 @@
void *beos_dlopen(const char *fname, int unused);
void *beos_dlsym(void *lib, const char *sym);
void beos_dlclose(void *lib);
void beos_usleep(unsigned long ticks);
#define dlopen beos_dlopen
#define dlsym beos_dlsym
#define dlclose beos_dlclose
#define usleep beos_usleep
#else
#include <dlfcn.h>
#define DLOPEN_ARGS (RTLD_NOW | RTLD_GLOBAL)
Expand Down Expand Up @@ -426,6 +428,12 @@ boolean MojoPlatform_osVersion(char *buf, size_t len)
} // MojoPlatform_osversion


void MojoPlatform_sleep(uint32 ticks)
{
usleep(ticks * 1000);
} // MojoPlatform_sleep


uint32 MojoPlatform_ticks(void)
{
uint64 then_ms, now_ms;
Expand Down

0 comments on commit 336b7ea

Please sign in to comment.