Make load_data properly work in threads

This commit is contained in:
Struma 2021-08-27 05:08:34 -04:00 committed by Roza
parent 35d776abd7
commit 8b0bb010ba

View file

@ -1,23 +1,23 @@
/* /*
** filesystem-binding.cpp ** filesystem-binding.cpp
** **
** This file is part of mkxp. ** This file is part of mkxp.
** **
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com> ** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
** **
** mkxp is free software: you can redistribute it and/or modify ** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by ** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or ** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version. ** (at your option) any later version.
** **
** mkxp is distributed in the hope that it will be useful, ** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of ** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details. ** GNU General Public License for more details.
** **
** You should have received a copy of the GNU General Public License ** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>. ** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "src/config.h" #include "src/config.h"
@ -39,10 +39,10 @@
#endif #endif
static void fileIntFreeInstance(void *inst) { static void fileIntFreeInstance(void *inst) {
SDL_RWops *ops = static_cast<SDL_RWops *>(inst); SDL_RWops *ops = static_cast<SDL_RWops *>(inst);
SDL_RWclose(ops); SDL_RWclose(ops);
SDL_FreeRW(ops); SDL_FreeRW(ops);
} }
#if RAPI_FULL > 187 #if RAPI_FULL > 187
@ -52,166 +52,170 @@ DEF_ALLOCFUNC_CUSTOMFREE(FileInt, fileIntFreeInstance);
#endif #endif
static VALUE fileIntForPath(const char *path, bool rubyExc) { static VALUE fileIntForPath(const char *path, bool rubyExc) {
SDL_RWops *ops = SDL_AllocRW(); SDL_RWops *ops = SDL_AllocRW();
try { try {
shState->fileSystem().openReadRaw(*ops, path); shState->fileSystem().openReadRaw(*ops, path);
} catch (const Exception &e) { } catch (const Exception &e) {
SDL_FreeRW(ops); SDL_FreeRW(ops);
if (rubyExc) if (rubyExc)
raiseRbExc(e); raiseRbExc(e);
else else
throw e; throw e;
} }
VALUE klass = rb_const_get(rb_cObject, rb_intern("FileInt")); VALUE klass = rb_const_get(rb_cObject, rb_intern("FileInt"));
VALUE obj = rb_obj_alloc(klass); VALUE obj = rb_obj_alloc(klass);
setPrivateData(obj, ops); setPrivateData(obj, ops);
return obj; return obj;
} }
#if RAPI_MAJOR > 2
typedef struct {
SDL_RWops *ops;
void *dst;
int length;
} fileIntReadCbArgs;
void call_RWread_cb(fileIntReadCbArgs *args) {
SDL_RWread(args->ops, args->dst, 1, args->length);
}
#endif
RB_METHOD(fileIntRead) { RB_METHOD(fileIntRead) {
int length = -1; int length = -1;
rb_get_args(argc, argv, "|i", &length RB_ARG_END); rb_get_args(argc, argv, "|i", &length RB_ARG_END);
SDL_RWops *ops = getPrivateData<SDL_RWops>(self); SDL_RWops *ops = getPrivateData<SDL_RWops>(self);
if (length == -1) { if (length == -1) {
Sint64 cur = SDL_RWtell(ops); Sint64 cur = SDL_RWtell(ops);
Sint64 end = SDL_RWseek(ops, 0, SEEK_END); Sint64 end = SDL_RWseek(ops, 0, SEEK_END);
// Sometimes SDL_RWseek will fail for no reason // Sometimes SDL_RWseek will fail for no reason
// with encrypted archives, so let's just ask // with encrypted archives, so let's just ask
// for the size up front // for the size up front
if (end < 0) if (end < 0)
end = ops->size(ops); end = ops->size(ops);
length = end - cur; length = end - cur;
SDL_RWseek(ops, cur, SEEK_SET); SDL_RWseek(ops, cur, SEEK_SET);
} }
if (length == 0) if (length == 0)
return Qnil; return Qnil;
VALUE data = rb_str_new(0, length); VALUE data = rb_str_new(0, length);
SDL_RWread(ops, RSTRING_PTR(data), 1, length);
return data; #if RAPI_MAJOR > 2
fileIntReadCbArgs cbargs {ops, RSTRING_PTR(data), length};
rb_thread_call_without_gvl([](void* args) -> void* {
call_RWread_cb((fileIntReadCbArgs*)args);
return 0;
}, (void*)&cbargs, 0, 0);
#elif
SDL_RWread(ops, RSTRING_PTR(data), 1, length);
#endif
return data;
} }
RB_METHOD(fileIntClose) { RB_METHOD(fileIntClose) {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
SDL_RWops *ops = getPrivateData<SDL_RWops>(self); SDL_RWops *ops = getPrivateData<SDL_RWops>(self);
SDL_RWclose(ops); SDL_RWclose(ops);
return Qnil; return Qnil;
} }
RB_METHOD(fileIntGetByte) { RB_METHOD(fileIntGetByte) {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
SDL_RWops *ops = getPrivateData<SDL_RWops>(self); SDL_RWops *ops = getPrivateData<SDL_RWops>(self);
unsigned char byte; unsigned char byte;
size_t result = SDL_RWread(ops, &byte, 1, 1); size_t result = SDL_RWread(ops, &byte, 1, 1);
return (result == 1) ? rb_fix_new(byte) : Qnil; return (result == 1) ? rb_fix_new(byte) : Qnil;
} }
RB_METHOD(fileIntBinmode) { RB_METHOD(fileIntBinmode) {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
return Qnil; return Qnil;
} }
#if RAPI_FULL <= 187 #if RAPI_FULL <= 187
RB_METHOD(fileIntPos) { RB_METHOD(fileIntPos) {
SDL_RWops *ops = getPrivateData<SDL_RWops>(self); SDL_RWops *ops = getPrivateData<SDL_RWops>(self);
long long pos = SDL_RWtell(ops); // Will return -1 if it doesn't work long long pos = SDL_RWtell(ops); // Will return -1 if it doesn't work
return LL2NUM(pos); return LL2NUM(pos);
} }
#endif #endif
VALUE VALUE
kernelLoadDataInt(const char *filename, bool rubyExc, bool raw) { kernelLoadDataInt(const char *filename, bool rubyExc, bool raw) {
//rb_gc_start(); //rb_gc_start();
VALUE port = fileIntForPath(filename, rubyExc); VALUE port = fileIntForPath(filename, rubyExc);
VALUE result; VALUE result;
if (!raw) { if (!raw) {
VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal")); VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal"));
// FIXME need to catch exceptions here with begin rescue // FIXME need to catch exceptions here with begin rescue
VALUE data = fileIntRead(0, 0, port); VALUE data = fileIntRead(0, 0, port);
result = rb_funcall2(marsh, rb_intern("load"), 1, &data); result = rb_funcall2(marsh, rb_intern("load"), 1, &data);
} else { } else {
result = fileIntRead(0, 0, port); result = fileIntRead(0, 0, port);
} }
rb_funcall2(port, rb_intern("close"), 0, NULL); rb_funcall2(port, rb_intern("close"), 0, NULL);
return result; return result;
} }
#if RAPI_MAJOR >= 2
typedef struct {
const char *filename;
bool loadRaw;
} loadDataRubyArgs;
VALUE call_kernelLoadDataInt_cb(loadDataRubyArgs *args) {
return kernelLoadDataInt(args->filename, true, args->loadRaw);
}
#endif
RB_METHOD(kernelLoadData) { RB_METHOD(kernelLoadData) {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
VALUE filename; VALUE filename;
VALUE raw; VALUE raw;
rb_scan_args(argc, argv, "11", &filename, &raw); rb_scan_args(argc, argv, "11", &filename, &raw);
SafeStringValue(filename); SafeStringValue(filename);
// There's gotta be an easier way to do this // There's gotta be an easier way to do this
if (raw != Qnil && raw != Qtrue && raw != Qfalse) { if (raw != Qnil && raw != Qtrue && raw != Qfalse) {
rb_raise(rb_eTypeError, "load_data: second argument must be Boolean"); rb_raise(rb_eTypeError, "load_data: second argument must be Boolean");
} }
#if RAPI_MAJOR >= 2 return kernelLoadDataInt(RSTRING_PTR(filename), true, RTEST(raw));
loadDataRubyArgs ldargs {RSTRING_PTR(filename), RTEST(raw)};
return (VALUE)rb_thread_call_without_gvl([](void* args) -> void* {
return ((void*)call_kernelLoadDataInt_cb((loadDataRubyArgs*)args));
}, (void*)&ldargs, 0, 0);
#else
return kernelLoadDataInt(RSTRING_PTR(filename), true, RTEST(raw));
#endif
} }
RB_METHOD(kernelSaveData) { RB_METHOD(kernelSaveData) {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
VALUE obj; VALUE obj;
VALUE filename; VALUE filename;
rb_get_args(argc, argv, "oS", &obj, &filename RB_ARG_END); rb_get_args(argc, argv, "oS", &obj, &filename RB_ARG_END);
VALUE file = rb_file_open_str(filename, "wb"); VALUE file = rb_file_open_str(filename, "wb");
VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal")); VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal"));
VALUE v[] = {obj, file}; VALUE v[] = {obj, file};
rb_funcall2(marsh, rb_intern("dump"), ARRAY_SIZE(v), v); rb_funcall2(marsh, rb_intern("dump"), ARRAY_SIZE(v), v);
rb_io_close(file); rb_io_close(file);
return Qnil; return Qnil;
} }
#if RAPI_FULL > 187 #if RAPI_FULL > 187
#if RAPI_FULL < 270 #if RAPI_FULL < 270
@ -220,78 +224,78 @@ static VALUE stringForceUTF8(VALUE arg)
static VALUE stringForceUTF8(RB_BLOCK_CALL_FUNC_ARGLIST(arg, callback_arg)) static VALUE stringForceUTF8(RB_BLOCK_CALL_FUNC_ARGLIST(arg, callback_arg))
#endif #endif
{ {
if (RB_TYPE_P(arg, RUBY_T_STRING) && ENCODING_IS_ASCII8BIT(arg)) if (RB_TYPE_P(arg, RUBY_T_STRING) && ENCODING_IS_ASCII8BIT(arg))
rb_enc_associate_index(arg, rb_utf8_encindex()); rb_enc_associate_index(arg, rb_utf8_encindex());
return arg; return arg;
} }
#if RAPI_FULL < 270 #if RAPI_FULL < 270
static VALUE customProc(VALUE arg, VALUE proc) { static VALUE customProc(VALUE arg, VALUE proc) {
VALUE obj = stringForceUTF8(arg); VALUE obj = stringForceUTF8(arg);
obj = rb_funcall2(proc, rb_intern("call"), 1, &obj); obj = rb_funcall2(proc, rb_intern("call"), 1, &obj);
return obj; return obj;
} }
#endif #endif
RB_METHOD(_marshalLoad) { RB_METHOD(_marshalLoad) {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
#if RAPI_FULL < 270 #if RAPI_FULL < 270
VALUE port, proc = Qnil; VALUE port, proc = Qnil;
rb_get_args(argc, argv, "o|o", &port, &proc RB_ARG_END); rb_get_args(argc, argv, "o|o", &port, &proc RB_ARG_END);
#else #else
VALUE port; VALUE port;
rb_get_args(argc, argv, "o", &port RB_ARG_END); rb_get_args(argc, argv, "o", &port RB_ARG_END);
#endif #endif
VALUE utf8Proc; VALUE utf8Proc;
#if RAPI_FULL < 270 #if RAPI_FULL < 270
if (NIL_P(proc)) if (NIL_P(proc))
utf8Proc = rb_proc_new(RUBY_METHOD_FUNC(stringForceUTF8), Qnil); utf8Proc = rb_proc_new(RUBY_METHOD_FUNC(stringForceUTF8), Qnil);
else else
utf8Proc = rb_proc_new(RUBY_METHOD_FUNC(customProc), proc); utf8Proc = rb_proc_new(RUBY_METHOD_FUNC(customProc), proc);
#else #else
utf8Proc = rb_proc_new(stringForceUTF8, Qnil); utf8Proc = rb_proc_new(stringForceUTF8, Qnil);
#endif #endif
VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal")); VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal"));
VALUE v[] = {port, utf8Proc}; VALUE v[] = {port, utf8Proc};
return rb_funcall2(marsh, rb_intern("_mkxp_load_alias"), ARRAY_SIZE(v), v); return rb_funcall2(marsh, rb_intern("_mkxp_load_alias"), ARRAY_SIZE(v), v);
} }
#endif #endif
void fileIntBindingInit() { void fileIntBindingInit() {
VALUE klass = rb_define_class("FileInt", rb_cIO); VALUE klass = rb_define_class("FileInt", rb_cIO);
#if RAPI_FULL > 187 #if RAPI_FULL > 187
rb_define_alloc_func(klass, classAllocate<&FileIntType>); rb_define_alloc_func(klass, classAllocate<&FileIntType>);
#else #else
rb_define_alloc_func(klass, FileIntAllocate); rb_define_alloc_func(klass, FileIntAllocate);
#endif #endif
_rb_define_method(klass, "read", fileIntRead); _rb_define_method(klass, "read", fileIntRead);
_rb_define_method(klass, "getbyte", fileIntGetByte); _rb_define_method(klass, "getbyte", fileIntGetByte);
#if RAPI_FULL <= 187 #if RAPI_FULL <= 187
// Ruby doesn't see this as an initialized stream, // Ruby doesn't see this as an initialized stream,
// so either that has to be fixed or necessary // so either that has to be fixed or necessary
// IO functions have to be overridden // IO functions have to be overridden
rb_define_alias(klass, "getc", "getbyte"); rb_define_alias(klass, "getc", "getbyte");
_rb_define_method(klass, "pos", fileIntPos); _rb_define_method(klass, "pos", fileIntPos);
#endif #endif
_rb_define_method(klass, "binmode", fileIntBinmode); _rb_define_method(klass, "binmode", fileIntBinmode);
_rb_define_method(klass, "close", fileIntClose); _rb_define_method(klass, "close", fileIntClose);
_rb_define_module_function(rb_mKernel, "load_data", kernelLoadData); _rb_define_module_function(rb_mKernel, "load_data", kernelLoadData);
_rb_define_module_function(rb_mKernel, "save_data", kernelSaveData); _rb_define_module_function(rb_mKernel, "save_data", kernelSaveData);
#if RAPI_FULL > 187 #if RAPI_FULL > 187
/* We overload the built-in 'Marshal::load()' function to silently /* We overload the built-in 'Marshal::load()' function to silently
* insert our utf8proc that ensures all read strings will be * insert our utf8proc that ensures all read strings will be
* UTF-8 encoded */ * UTF-8 encoded */
VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal")); VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal"));
rb_define_alias(rb_singleton_class(marsh), "_mkxp_load_alias", "load"); rb_define_alias(rb_singleton_class(marsh), "_mkxp_load_alias", "load");
_rb_define_module_function(marsh, "load", _marshalLoad); _rb_define_module_function(marsh, "load", _marshalLoad);
#endif #endif
} }