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

10
App.cpp
View file

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

View file

@ -16,7 +16,7 @@
namespace krikkel::NCurses
{
using gsl::narrow;
using std::lock_guard;
using std::scoped_lock;
using std::recursive_mutex;
PtyWindow::PtyWindow(recursive_mutex *ncursesMutex, int lines, int columns, int y, int x)
@ -43,10 +43,11 @@ namespace krikkel::NCurses
vterm_output_set_callback(pseudoTerminal, &staticHandlerOutput, this);
vterm_screen_enable_altscreen(pseudoTerminalScreen, true);
//raw(); //— cbreak might suffice
//noecho(); — already set
//nodelay(true); — @todo needs some reprogramming
keypad(true);
//raw(); // — cbreak might suffice
//noecho(); // — already set by NCursesWindow
keypad(true); /// — already set by NCursesWindow
//meta(true); // — already set by NCursesWindow
//nodelay(true); // — @todo would need some programming, not sure if useful
nonl();
/// @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)
{
lock_guard lock(writeToPseudoTerminalMutex);
scoped_lock lock(ptyMutex);
write(fdPtyHost, output, length);
//__debug_log("written to PTY client: '" + __debug_make_bytes_printable(std::string(output, length)) + '\'');
}
void PtyWindow::writeUnicodeCharToClient(wint_t character)
{
lock_guard lock(writeToPseudoTerminalMutex);
scoped_lock lock(ptyMutex);
vterm_keyboard_unichar(pseudoTerminal, character, VTERM_MOD_NONE);
}
void PtyWindow::writeKeyToClient(VTermKey key)
{
lock_guard lock(writeToPseudoTerminalMutex);
scoped_lock lock(ptyMutex);
//__debug_log("writing key: " + std::to_string(key));
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);
if(bytesRead != -1 && bytesRead != 0)
{
lock_guard writeLock(writeToPseudoTerminalMutex);
scoped_lock lock(ptyMutex, *ncursesMutex);
vterm_input_write(pseudoTerminal, 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)
{
scoped_lock lock(ptyMutex, *ncursesMutex);
for(int x = rect.start_col; x < rect.end_col; ++x)
for(int y = rect.start_row; y < rect.end_row; ++y)
copyPtyCellToNCursesWindow(x, y);
@ -156,6 +159,7 @@ namespace krikkel::NCurses
int PtyWindow::handlerMoveCursor(VTermPos pos, VTermPos oldpos, int visible)
{
scoped_lock lock(*ncursesMutex);
/// @todo maybe use `mvcur()` instead?
cursorX = pos.col;
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));
setcchar(&converted, &character, formatting, colorPair, NULL);
{
lock_guard lock(*ncursesMutex);
move(cellPosition.row, cellPosition.col);
chgat(1, formatting, colorPair, NULL);
add_wch(&converted);
@ -254,7 +257,7 @@ namespace krikkel::NCurses
int PtyWindow::refresh()
{
//__debug_log("refreshing");
lock_guard lock(*ncursesMutex);
scoped_lock lock(*ncursesMutex);
move(cursorY, cursorX);
return Window::refresh();
}
@ -263,20 +266,17 @@ namespace krikkel::NCurses
/// resizing?
int PtyWindow::resize(int rows, int cols)
{
scoped_lock lock(ptyMutex, *ncursesMutex);
winsize windowSize =
{
lock_guard writeLock(writeToPseudoTerminalMutex);
winsize windowSize =
{
.ws_row = narrow<unsigned short>(rows)
, .ws_col = narrow<unsigned short>(cols)
};
ioctl(fdPtyHost, TIOCSWINSZ, &windowSize);
vterm_set_size(pseudoTerminal, rows, cols);
}
{
lock_guard lock(*ncursesMutex);
return Window::resize(rows, cols);
}
.ws_row = narrow<unsigned short>(rows)
, .ws_col = narrow<unsigned short>(cols)
};
ioctl(fdPtyHost, TIOCSWINSZ, &windowSize);
vterm_set_size(pseudoTerminal, rows, cols);
return Window::resize(rows, cols);
}
int PtyWindow::staticHandlerDamage(VTermRect rect, void *user)

View file

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

View file

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