switched to scoped_lock

This commit is contained in:
Christian Burger 2022-05-06 18:37:48 +02:00
parent b9e32941fb
commit e42711f123
4 changed files with 35 additions and 31 deletions

View file

@ -15,6 +15,8 @@
namespace krikkel::NCurses namespace krikkel::NCurses
{ {
using std::scoped_lock;
DemoApp::DemoApp() : NCursesApplication(false) DemoApp::DemoApp() : NCursesApplication(false)
{ {
@ -55,11 +57,13 @@ namespace krikkel::NCurses
SingleUserInput input = windowManager.readSingleUserInput(); SingleUserInput input = windowManager.readSingleUserInput();
if(input.isNormalKey()) if(input.isNormalKey())
{ {
std::lock_guard lock(ncursesMutex);
ptyWindow->writeUnicodeCharToClient(input.getRawInput()); ptyWindow->writeUnicodeCharToClient(input.getRawInput());
{
scoped_lock lock(ncursesMutex);
dummyWindow->addch(input.getRawInput()); dummyWindow->addch(input.getRawInput());
dummyWindow->refresh(); dummyWindow->refresh();
} }
}
if(input.isFunctionKey()) if(input.isFunctionKey())
{ {
switch(input.getRawInput()) switch(input.getRawInput())

View file

@ -16,7 +16,7 @@
namespace krikkel::NCurses namespace krikkel::NCurses
{ {
using gsl::narrow; using gsl::narrow;
using std::lock_guard; using std::scoped_lock;
using std::recursive_mutex; using std::recursive_mutex;
PtyWindow::PtyWindow(recursive_mutex *ncursesMutex, int lines, int columns, int y, int x) PtyWindow::PtyWindow(recursive_mutex *ncursesMutex, int lines, int columns, int y, int x)
@ -44,9 +44,10 @@ namespace krikkel::NCurses
vterm_screen_enable_altscreen(pseudoTerminalScreen, true); vterm_screen_enable_altscreen(pseudoTerminalScreen, true);
//raw(); // — cbreak might suffice //raw(); // — cbreak might suffice
//noecho(); — already set //noecho(); // — already set by NCursesWindow
//nodelay(true); — @todo needs some reprogramming keypad(true); /// — already set by NCursesWindow
keypad(true); //meta(true); // — already set by NCursesWindow
//nodelay(true); // — @todo would need some programming, not sure if useful
nonl(); nonl();
/// @todo block all signals, this thread does not handle any /// @todo block all signals, this thread does not handle any
@ -68,20 +69,20 @@ namespace krikkel::NCurses
void PtyWindow::writeToClient(const char *output, size_t length) void PtyWindow::writeToClient(const char *output, size_t length)
{ {
lock_guard lock(writeToPseudoTerminalMutex); scoped_lock lock(ptyMutex);
write(fdPtyHost, output, length); write(fdPtyHost, output, length);
//__debug_log("written to PTY client: '" + __debug_make_bytes_printable(std::string(output, length)) + '\''); //__debug_log("written to PTY client: '" + __debug_make_bytes_printable(std::string(output, length)) + '\'');
} }
void PtyWindow::writeUnicodeCharToClient(wint_t character) void PtyWindow::writeUnicodeCharToClient(wint_t character)
{ {
lock_guard lock(writeToPseudoTerminalMutex); scoped_lock lock(ptyMutex);
vterm_keyboard_unichar(pseudoTerminal, character, VTERM_MOD_NONE); vterm_keyboard_unichar(pseudoTerminal, character, VTERM_MOD_NONE);
} }
void PtyWindow::writeKeyToClient(VTermKey key) void PtyWindow::writeKeyToClient(VTermKey key)
{ {
lock_guard lock(writeToPseudoTerminalMutex); scoped_lock lock(ptyMutex);
//__debug_log("writing key: " + std::to_string(key)); //__debug_log("writing key: " + std::to_string(key));
vterm_keyboard_key(pseudoTerminal, key, VTERM_MOD_NONE); vterm_keyboard_key(pseudoTerminal, key, VTERM_MOD_NONE);
} }
@ -108,7 +109,7 @@ namespace krikkel::NCurses
size_t bytesRead = read(fdPtyHost, ptyClientOutputBuffer, PTY_CLIENT_OUTPUT_BUFFER_SIZE); size_t bytesRead = read(fdPtyHost, ptyClientOutputBuffer, PTY_CLIENT_OUTPUT_BUFFER_SIZE);
if(bytesRead != -1 && bytesRead != 0) if(bytesRead != -1 && bytesRead != 0)
{ {
lock_guard writeLock(writeToPseudoTerminalMutex); scoped_lock lock(ptyMutex, *ncursesMutex);
vterm_input_write(pseudoTerminal, ptyClientOutputBuffer, bytesRead); vterm_input_write(pseudoTerminal, ptyClientOutputBuffer, bytesRead);
} }
//__debug_log("read from PTY client: '" + __debug_make_bytes_printable(std::string(ptyClientOutputBuffer, bytesRead)) + '\''); //__debug_log("read from PTY client: '" + __debug_make_bytes_printable(std::string(ptyClientOutputBuffer, bytesRead)) + '\'');
@ -128,6 +129,8 @@ namespace krikkel::NCurses
int PtyWindow::handlerDamage(VTermRect rect) int PtyWindow::handlerDamage(VTermRect rect)
{ {
scoped_lock lock(ptyMutex, *ncursesMutex);
for(int x = rect.start_col; x < rect.end_col; ++x) for(int x = rect.start_col; x < rect.end_col; ++x)
for(int y = rect.start_row; y < rect.end_row; ++y) for(int y = rect.start_row; y < rect.end_row; ++y)
copyPtyCellToNCursesWindow(x, y); copyPtyCellToNCursesWindow(x, y);
@ -156,6 +159,7 @@ namespace krikkel::NCurses
int PtyWindow::handlerMoveCursor(VTermPos pos, VTermPos oldpos, int visible) int PtyWindow::handlerMoveCursor(VTermPos pos, VTermPos oldpos, int visible)
{ {
scoped_lock lock(*ncursesMutex);
/// @todo maybe use `mvcur()` instead? /// @todo maybe use `mvcur()` instead?
cursorX = pos.col; cursorX = pos.col;
cursorY = pos.row; cursorY = pos.row;
@ -244,7 +248,6 @@ namespace krikkel::NCurses
//__debug_log(std::string("written '") + (char) character + std::string("' ") + std::to_string(x) + ", " + std::to_string(y)); //__debug_log(std::string("written '") + (char) character + std::string("' ") + std::to_string(x) + ", " + std::to_string(y));
setcchar(&converted, &character, formatting, colorPair, NULL); setcchar(&converted, &character, formatting, colorPair, NULL);
{ {
lock_guard lock(*ncursesMutex);
move(cellPosition.row, cellPosition.col); move(cellPosition.row, cellPosition.col);
chgat(1, formatting, colorPair, NULL); chgat(1, formatting, colorPair, NULL);
add_wch(&converted); add_wch(&converted);
@ -254,7 +257,7 @@ namespace krikkel::NCurses
int PtyWindow::refresh() int PtyWindow::refresh()
{ {
//__debug_log("refreshing"); //__debug_log("refreshing");
lock_guard lock(*ncursesMutex); scoped_lock lock(*ncursesMutex);
move(cursorY, cursorX); move(cursorY, cursorX);
return Window::refresh(); return Window::refresh();
} }
@ -263,8 +266,8 @@ namespace krikkel::NCurses
/// resizing? /// resizing?
int PtyWindow::resize(int rows, int cols) int PtyWindow::resize(int rows, int cols)
{ {
{ scoped_lock lock(ptyMutex, *ncursesMutex);
lock_guard writeLock(writeToPseudoTerminalMutex);
winsize windowSize = winsize windowSize =
{ {
.ws_row = narrow<unsigned short>(rows) .ws_row = narrow<unsigned short>(rows)
@ -272,12 +275,9 @@ namespace krikkel::NCurses
}; };
ioctl(fdPtyHost, TIOCSWINSZ, &windowSize); ioctl(fdPtyHost, TIOCSWINSZ, &windowSize);
vterm_set_size(pseudoTerminal, rows, cols); vterm_set_size(pseudoTerminal, rows, cols);
}
{
lock_guard lock(*ncursesMutex);
return Window::resize(rows, cols); return Window::resize(rows, cols);
} }
}
int PtyWindow::staticHandlerDamage(VTermRect rect, void *user) int PtyWindow::staticHandlerDamage(VTermRect rect, void *user)
{ {

View file

@ -11,7 +11,7 @@ namespace krikkel::NCurses
{ {
using std::list; using std::list;
using std::recursive_mutex; using std::recursive_mutex;
using std::lock_guard; using std::scoped_lock;
using std::find; using std::find;
VerticalTilingWindowManager::VerticalTilingWindowManager(NCursesWindow *rootWindow, recursive_mutex *ncursesMutex) VerticalTilingWindowManager::VerticalTilingWindowManager(NCursesWindow *rootWindow, recursive_mutex *ncursesMutex)
@ -52,7 +52,7 @@ namespace krikkel::NCurses
void VerticalTilingWindowManager::refresh() void VerticalTilingWindowManager::refresh()
{ {
lock_guard lock(*ncursesMutex); scoped_lock lock(*ncursesMutex);
rootWindow->refresh(); rootWindow->refresh();
for(Window *window : visibleStack) for(Window *window : visibleStack)
@ -66,7 +66,7 @@ namespace krikkel::NCurses
void VerticalTilingWindowManager::updateLayout() void VerticalTilingWindowManager::updateLayout()
{ {
lock_guard lock(*ncursesMutex); scoped_lock lock(*ncursesMutex);
int availableWidth = rootWindow->width(); int availableWidth = rootWindow->width();
int availableHeight = rootWindow->height(); int availableHeight = rootWindow->height();
size_t stackSize = visibleStack.size(); size_t stackSize = visibleStack.size();

View file

@ -36,7 +36,7 @@ namespace krikkel::NCurses
int fdPtyHost, fdPtyClient; int fdPtyHost, fdPtyClient;
struct termios terminalParameters; struct termios terminalParameters;
VTerm *pseudoTerminal; VTerm *pseudoTerminal;
std::recursive_mutex writeToPseudoTerminalMutex; std::recursive_mutex ptyMutex;
std::recursive_mutex *ncursesMutex; std::recursive_mutex *ncursesMutex;
VTermScreen *pseudoTerminalScreen; VTermScreen *pseudoTerminalScreen;
static VTermScreenCallbacks screenCallbacks; static VTermScreenCallbacks screenCallbacks;