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
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp 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.
**
** mkxp 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 mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
** filesystem-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp 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.
**
** mkxp 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 mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "src/config.h"
@ -74,6 +74,18 @@ static VALUE fileIntForPath(const char *path, bool rubyExc) {
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) {
int length = -1;
@ -100,7 +112,17 @@ RB_METHOD(fileIntRead) {
VALUE data = rb_str_new(0, length);
#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;
}
@ -161,17 +183,6 @@ kernelLoadDataInt(const char *filename, bool rubyExc, bool raw) {
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_UNUSED_PARAM;
@ -184,14 +195,7 @@ RB_METHOD(kernelLoadData) {
if (raw != Qnil && raw != Qtrue && raw != Qfalse) {
rb_raise(rb_eTypeError, "load_data: second argument must be Boolean");
}
#if RAPI_MAJOR >= 2
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) {