How GCode Adding to Queue Works?

Hello everyone!
A plugin was written to register a new command in klipper - parallel_gcode.

After calling this command, gcodes are listed that need to be executed in parallel, but a problem arose - the main thread, which is responsible for the sequential execution of gcode in klipper, is engaged, gcodes are not executed in parallel, and when calling any other, it also does not start to execute.

An idea arose, the basis of which is that it is necessary to modify the addition of gcode to the queue, but it is a little difficult to understand how and where this logic is implemented, can you tell me please what logic is responsible for forming the gcode queue in klipper? And how, for example, it is better to change it to add logic for executing gcode in parallel, without causing critical damage to the logic of sequential execution.

I think the bigger issue that you are going to have is not how to add the gcode to the queue but how to execute it.

Klipper executes a single gcode command at a time. To ensure this, it uses a lock. When a gcode command is executing, the lock is locked and you can’t execute another one until the first one is finished.

In order to execute gcode commands in parallel, you’d have to work around that lock. However, the most likely result would be large unpredictability and potential crashes.

For example, how would you deal with two gcode commands that more the same stepper executed in parallel?