crosire / blink

A tool which allows you to edit source code of any MSVC C++ project live at runtime
BSD 2-Clause "Simplified" License
1.1k stars 80 forks source link

Function to sync patching time #22

Closed andorxornot closed 5 years ago

andorxornot commented 5 years ago

First if all, I like this project a lot. I have been testing for two weeks against live++, and I like your approach when no changes are made to the program, it is very convenient, but there are moments when patching seems occurs at the moment of program execution and the program crashes.

Maybe you could add some agreement concerning the name of a certain global static function (___sync or something like that) which you can hook and make patching only when there is a call to this function. if such a function is not found in the program, then run patching at any time as original behavior

And I would also like to have a global callback that would be called upon successful patching, but in any case, it can be combined with the sync function if it can return a bool.

Thank you!

crosire commented 5 years ago

This is now implemented. The following is an example how this can be used to synchronize blink with some other place in your code:

#include <mutex>
#include <condition_variable>
std::mutex blink_mutex;
std::condition_variable blink_cv;
bool blink_wants_to_work = false;

extern "C" void __blink_sync(const char *source_file)
{
    std::unique_lock<std::mutex> lock(blink_mutex);
    blink_wants_to_work = true;
    blink_cv.wait(lock); // Wait for app to allow us to continue
}
extern "C" void __blink_release(const char *source_file, bool success)
{
    blink_wants_to_work = false;
    blink_cv.notify_one(); // Signal app thread that it may continue
}

// Somewhere else in your code where it is safe for blink to execute
{
    std::unique_lock<std::mutex> lock(blink_mutex);
    if (blink_wants_to_work)
    {
        blink_cv.notify_one(); // Notify "__blink_sync" to continue
        blink_cv.wait(lock); // Wait for "__blink_release" to allow continuation
    }
}