module patch.sync; import core.sys.posix.time : clock_gettime, CLOCK_MONOTONIC, timespec; import patches : isPatchEnabled; // Define TIMEDIFF macro long TIMEDIFF(timespec t1, timespec t2) { return (t1.tv_sec - t2.tv_sec) * 1000 + (t1.tv_nsec - t2.tv_nsec) / 1_000_000; } // Define these globals for sync patch __gshared { int su = 0; int twrite_aborted = 0; } static if (isPatchEnabled!"SYNC_PATCH") { timespec sutv; void tsync_begin() { clock_gettime(CLOCK_MONOTONIC, &sutv); su = 1; } void tsync_end() { su = 0; } int tinsync(uint timeout) { timespec now; if (su && !clock_gettime(CLOCK_MONOTONIC, &now) && TIMEDIFF(now, sutv) >= timeout) su = 0; return su; } int ttyread_pending() { return twrite_aborted; } }