a mutex to lock buffer hoping to make it kinda usable from another thread

This commit is contained in:
Adam D. Ruppe 2020-09-30 22:41:54 -04:00
parent ad43305b30
commit 97e36961db
1 changed files with 24 additions and 0 deletions

View File

@ -1035,6 +1035,7 @@ struct Terminal {
/++ /++
+/ +/
this(ConsoleOutputType type) { this(ConsoleOutputType type) {
createLock();
this.type = type; this.type = type;
if(type == ConsoleOutputType.minimalProcessing) { if(type == ConsoleOutputType.minimalProcessing) {
@ -1076,6 +1077,7 @@ struct Terminal {
* ditto on getSizeOverride. That's there so you can do something instead of ioctl. * ditto on getSizeOverride. That's there so you can do something instead of ioctl.
*/ */
this(ConsoleOutputType type, int fdIn = 0, int fdOut = 1, int[] delegate() getSizeOverride = null) { this(ConsoleOutputType type, int fdIn = 0, int fdOut = 1, int[] delegate() getSizeOverride = null) {
createLock();
this.fdIn = fdIn; this.fdIn = fdIn;
this.fdOut = fdOut; this.fdOut = fdOut;
this.getSizeOverride = getSizeOverride; this.getSizeOverride = getSizeOverride;
@ -1124,6 +1126,7 @@ struct Terminal {
version(Win32Console) version(Win32Console)
/// ditto /// ditto
this(ConsoleOutputType type) { this(ConsoleOutputType type) {
createLock();
if(UseVtSequences) { if(UseVtSequences) {
hConsole = GetStdHandle(STD_OUTPUT_HANDLE); hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
initializeVt(); initializeVt();
@ -1557,6 +1560,7 @@ struct Terminal {
hideCursor(); hideCursor();
else if(UseVtSequences) { else if(UseVtSequences) {
// prepend the hide cursor command so it is the first thing flushed // prepend the hide cursor command so it is the first thing flushed
lock(); scope(exit) unlock();
writeBuffer = "\033[?25l" ~ writeBuffer; writeBuffer = "\033[?25l" ~ writeBuffer;
} }
@ -1610,6 +1614,7 @@ struct Terminal {
void flush() { void flush() {
if(writeBuffer.length == 0) if(writeBuffer.length == 0)
return; return;
lock(); scope(exit) unlock();
version(TerminalDirectToEmulator) { version(TerminalDirectToEmulator) {
tew.sendRawInput(cast(ubyte[]) writeBuffer); tew.sendRawInput(cast(ubyte[]) writeBuffer);
@ -1745,6 +1750,7 @@ struct Terminal {
+/ +/
void writePrintableString(in char[] s, ForceOption force = ForceOption.automatic) { void writePrintableString(in char[] s, ForceOption force = ForceOption.automatic) {
lock(); scope(exit) unlock();
// an escape character is going to mess things up. Actually any non-printable character could, but meh // an escape character is going to mess things up. Actually any non-printable character could, but meh
// assert(s.indexOf("\033") == -1); // assert(s.indexOf("\033") == -1);
@ -1794,9 +1800,27 @@ struct Terminal {
// you really, really shouldn't use this unless you know what you are doing // you really, really shouldn't use this unless you know what you are doing
/*private*/ void writeStringRaw(in char[] s) { /*private*/ void writeStringRaw(in char[] s) {
lock(); scope(exit) unlock();
writeBuffer ~= s; // buffer it to do everything at once in flush() calls writeBuffer ~= s; // buffer it to do everything at once in flush() calls
} }
import core.sync.mutex;
private shared(Mutex) mutex;
private void createLock() {
if(mutex is null)
mutex = new shared Mutex;
}
void lock() {
if(mutex)
mutex.lock();
}
void unlock() {
if(mutex)
mutex.unlock();
}
/// Clears the screen. /// Clears the screen.
void clear() { void clear() {
if(UseVtSequences) { if(UseVtSequences) {