Skip to content

Commit

Permalink
Merge branches 'lua_api' and 'bugfixes' into feature
Browse files Browse the repository at this point in the history
  • Loading branch information
arch1t3cht committed May 29, 2023
3 parents 5e51c59 + 7e08970 + f3d6eea commit a13233a
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 26 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ jobs:
if: startsWith(matrix.config.os, 'ubuntu-')
run: |
sudo apt-get update
sudo apt-get install ninja-build build-essential libx11-dev libwxgtk3.0-gtk3-dev libfreetype6-dev pkg-config libfontconfig1-dev libass-dev libasound2-dev libffms2-dev intltool libboost-all-dev
sudo apt-get install ninja-build build-essential libx11-dev libwxgtk3.0-gtk3-dev libfreetype6-dev pkg-config libfontconfig1-dev libass-dev libasound2-dev libffms2-dev intltool libboost-all-dev libhunspell-dev libuchardet-dev libpulse-dev libopenal-dev libjansson-dev
- name: Configure
run: meson setup build ${{ matrix.config.args }} -Dbuildtype=${{ matrix.config.buildtype }}
Expand Down Expand Up @@ -188,7 +188,6 @@ jobs:
./linuxdeploy --appdir appdir --desktop-file=appdir/aegisub.desktop
./appimagetool appdir
# ./appimagetool -g -s appdir --comp xz
- name: Upload artifacts - Linux AppImage
uses: actions/upload-artifact@v3
Expand Down
11 changes: 11 additions & 0 deletions libaegisub/common/dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace {
boost::asio::io_service *service;
std::function<void (agi::dispatch::Thunk)> invoke_main;
std::atomic<uint_fast32_t> threads_running;
thread_local bool is_main_thread;

class MainQueue final : public agi::dispatch::Queue {
void DoInvoke(agi::dispatch::Thunk thunk) override {
Expand Down Expand Up @@ -78,10 +79,12 @@ void Init(std::function<void (Thunk)> invoke_main) {
static IOServiceThreadPool thread_pool;
::service = &thread_pool.io_service;
::invoke_main = invoke_main;
::is_main_thread = true;

thread_pool.threads.reserve(std::max<unsigned>(4, std::thread::hardware_concurrency()));
for (size_t i = 0; i < thread_pool.threads.capacity(); ++i) {
thread_pool.threads.emplace_back([]{
::is_main_thread = false;
++threads_running;
agi::util::SetThreadName("Dispatch Worker");
service->run();
Expand Down Expand Up @@ -123,6 +126,14 @@ void Queue::Sync(Thunk thunk) {
if (e) std::rethrow_exception(e);
}

void EnsureMain(Thunk thunk) {
if (::is_main_thread) {
thunk();
} else {
Main().Sync(thunk);
}
}

Queue& Main() {
static MainQueue q;
return q;
Expand Down
5 changes: 5 additions & 0 deletions libaegisub/include/libaegisub/dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ namespace agi {
/// Get the main queue, which runs on the GUI thread
Queue& Main();


/// Ensure that the thunk is run on the main thread, without deadlocking
/// when already on the main thread
void EnsureMain(Thunk thunk);

/// Get the generic background queue, which runs thunks in parallel
Queue& Background();

Expand Down
1 change: 0 additions & 1 deletion libaegisub/include/libaegisub/lua/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ struct LuaStackcheck {
void dump();

LuaStackcheck(lua_State *L) : L(L), startstack(lua_gettop(L)) { }
~LuaStackcheck() { check_stack(0); }
};
#else
struct LuaStackcheck {
Expand Down
3 changes: 2 additions & 1 deletion src/auto4_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,15 @@ namespace Automation4 {

void ProgressSink::ShowDialog(ScriptDialog *config_dialog)
{
agi::dispatch::Main().Sync([=] {
agi::dispatch::EnsureMain([=] {
wxDialog w; // container dialog box
w.SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY);
w.Create(bsr->GetParentWindow(), -1, to_wx(bsr->GetTitle()));
auto s = new wxBoxSizer(wxHORIZONTAL); // sizer for putting contents in
wxWindow *ww = config_dialog->CreateWindow(&w); // generate actual dialog contents
s->Add(ww, 0, wxALL, 5); // add contents to dialog
w.SetSizerAndFit(s);
w.SetLayoutAdaptationMode(wxDIALOG_ADAPTATION_MODE_ENABLED);
w.CenterOnParent();
w.ShowModal();
});
Expand Down
8 changes: 5 additions & 3 deletions src/auto4_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ namespace {
const char *clipboard_get()
{
std::string data;
agi::dispatch::Main().Sync([&] { data = GetClipboard(); });
agi::dispatch::EnsureMain([&] { data = GetClipboard(); });
if (data.empty())
return nullptr;
return strndup(data);
Expand All @@ -138,7 +138,7 @@ namespace {
{
bool succeeded = false;

agi::dispatch::Main().Sync([&] {
agi::dispatch::EnsureMain([&] {
wxClipboard &cb = *wxTheClipboard;
if (cb.Open()) {
succeeded = cb.SetData(new wxTextDataObject(wxString::FromUTF8(str)));
Expand Down Expand Up @@ -677,6 +677,7 @@ namespace {
if (lua_isnumber(L, -1) && lua_tointeger(L, -1) == 3) {
lua_pop(L, 1); // just to avoid tripping the stackcheck in debug
description = "Attempted to load an Automation 3 script as an Automation 4 Lua script. Automation 3 is no longer supported.";
stackcheck.check_stack(0);
return;
}

Expand All @@ -689,6 +690,7 @@ namespace {
name = GetPrettyFilename().string();

lua_pop(L, 1);
stackcheck.check_stack(0);
// if we got this far, the script should be ready
loaded = true;
}
Expand Down Expand Up @@ -1136,7 +1138,7 @@ namespace {

// config
if (has_config && config_dialog) {
int results_produced = config_dialog->LuaReadBack(L);
int results_produced = config_dialog->LuaReadBack();
assert(results_produced == 1);
(void) results_produced; // avoid warning on release builds
// TODO, write back stored options here
Expand Down
4 changes: 4 additions & 0 deletions src/auto4_lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ namespace Automation4 {
std::deque<PendingCommit> pending_commits;
/// Lines to delete once processing complete successfully
std::vector<std::unique_ptr<AssEntry>> lines_to_delete;
/// Lines that were allocated here and need to be deleted if the script is cancelled.
std::vector<AssEntry *> allocated_lines;

/// Create copies of all of the lines in the script info section if it
/// hasn't already happened. This is done lazily, since it only needs
Expand Down Expand Up @@ -118,6 +120,8 @@ namespace Automation4 {
/// assumes a Lua representation of AssEntry on the top of the stack, and creates an AssEntry object of it
static std::unique_ptr<AssEntry> LuaToAssEntry(lua_State *L, AssFile *ass=nullptr);

std::unique_ptr<AssEntry> LuaToTrackedAssEntry(lua_State *L);

/// @brief Signal that the script using this file is now done running
/// @param set_undo If there's any uncommitted changes to the file,
/// they will be automatically committed with this
Expand Down
15 changes: 11 additions & 4 deletions src/auto4_lua_assfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ namespace Automation4 {
return result;
}

std::unique_ptr<AssEntry> LuaAssFile::LuaToTrackedAssEntry(lua_State *L) {
std::unique_ptr<AssEntry> e = LuaToAssEntry(L, ass);
allocated_lines.push_back(e.get());
return e;
}

int LuaAssFile::ObjectIndexRead(lua_State *L)
{
switch (lua_type(L, 2)) {
Expand Down Expand Up @@ -453,7 +459,7 @@ namespace Automation4 {
// insert
CheckBounds(n);

auto e = LuaToAssEntry(L, ass);
auto e = LuaToTrackedAssEntry(L);
modification_type |= modification_mask(e.get());
QueueLineForDeletion(n - 1);
AssignLine(n - 1, std::move(e));
Expand Down Expand Up @@ -542,7 +548,7 @@ namespace Automation4 {

for (int i = 1; i <= n; i++) {
lua_pushvalue(L, i);
auto e = LuaToAssEntry(L, ass);
auto e = LuaToTrackedAssEntry(L);
modification_type |= modification_mask(e.get());

if (lines.empty()) {
Expand Down Expand Up @@ -586,7 +592,7 @@ namespace Automation4 {
new_entries.reserve(n - 1);
for (int i = 2; i <= n; i++) {
lua_pushvalue(L, i);
auto e = LuaToAssEntry(L, ass);
auto e = LuaToTrackedAssEntry(L);
modification_type |= modification_mask(e.get());
InsertLine(new_entries, i - 2, std::move(e));
lua_pop(L, 1);
Expand Down Expand Up @@ -625,7 +631,7 @@ namespace Automation4 {

int LuaAssFile::LuaParseKaraokeData(lua_State *L)
{
auto e = LuaToAssEntry(L, ass);
auto e = LuaToTrackedAssEntry(L);
auto dia = check_cast_constptr<AssDialogue>(e.get());
argcheck(L, !!dia, 1, "Subtitle line must be a dialogue line");

Expand Down Expand Up @@ -734,6 +740,7 @@ namespace Automation4 {
void LuaAssFile::Cancel()
{
for (auto& line : lines_to_delete) line.release();
for (AssEntry *line : allocated_lines) delete line;
references--;
if (!references) delete this;
}
Expand Down
Loading

0 comments on commit a13233a

Please sign in to comment.