Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DLL] Implement thread safety of the console #123

Open
gynt opened this issue Sep 4, 2024 · 0 comments
Open

[DLL] Implement thread safety of the console #123

gynt opened this issue Sep 4, 2024 · 0 comments

Comments

@gynt
Copy link
Collaborator

gynt commented Sep 4, 2024

Currently console commands are executed in lua, using the global lua_State object, in a separate thread (waiting for input, processing input). However, there is no thread safety in lua by itself, and implementing that (which isn't difficult per se) would be overkill.

Rather, since the game is single-threaded (music and sound is played in a separate thread, but we aren't modifying that with lua anytime soon), I thought it would be better if the main loop of the game process would check if there are any console events to process.

This code can all be written in the dll and let it be hooked by the dll or by the main.lua code at startup.

The only question is if the main window is out of focus, is the main loop still running?

POC:

// https://stackoverflow.com/questions/15278343/c11-thread-safe-queue
ThreadSafeQueue q;

// Called by the console thread
void enqueue(const std::string& command) {
  q.enqueue(command);
}

int delay = 10;
int step = 0;

// Called by the main thread of the game
void consume() {
  if (delay == 0 || step == 0) {
    // Should return immediately
    std::string &command = q.dequeue();
    handleCommand(command); 
  }
  step += 1;
  if (step >= delay) step = 0;
}

// Called by the main thread of the game
// This is the function that is hooked up to the main loop of the game.
void __declspec(naked) onMainLoopCycle() {
  consume();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant