added mutex to lock concurrent writes to ncurses

This commit is contained in:
Christian Burger 2022-04-16 20:27:24 +02:00
parent aa36b3d22d
commit ab0525cc6f
3 changed files with 18 additions and 10 deletions

View file

@ -19,8 +19,10 @@ namespace krikkel::NCursesPtyWindow
int App::run()
{
Window *ptyWindow = new Window(
Root_Window->lines()
std::mutex writeMutex;
Window *ptyWindow = new Window(&writeMutex
, Root_Window->lines()
, Root_Window->cols()
, 0
, 0);

View file

@ -18,8 +18,8 @@ namespace krikkel::NCursesPtyWindow
using gsl::narrow;
using std::lock_guard;
Window::Window(int lines, int columns, int y, int x)
: NCursesWindow(lines, columns, y, x)
Window::Window(std::mutex *writeToNCursesMutex, int lines, int columns, int y, int x)
: writeToNCursesMutex(writeToNCursesMutex), NCursesWindow(lines, columns, y, x)
{
// to get the original terminal we need to shutdown ncurses for a moment
/// @todo maybe try `reset_prog_mode()` and `reset_shell_mode()` instead
@ -245,10 +245,13 @@ namespace krikkel::NCursesPtyWindow
else
character = *vTermCell.chars;
setcchar(&converted, &character, formatting, colorPair, NULL);
move(cellPosition.row, cellPosition.col);
chgat(1, formatting, colorPair, NULL);
add_wch(&converted);
{
lock_guard nCursesLock(*writeToNCursesMutex);
setcchar(&converted, &character, formatting, colorPair, NULL);
move(cellPosition.row, cellPosition.col);
chgat(1, formatting, colorPair, NULL);
add_wch(&converted);
}
}
int Window::add_wch(const cchar_t *character)
@ -263,6 +266,7 @@ namespace krikkel::NCursesPtyWindow
int Window::refresh()
{
lock_guard nCursesLock(*writeToNCursesMutex);
move(cursorY, cursorX);
return NCursesWindow::refresh();
}
@ -271,7 +275,8 @@ namespace krikkel::NCursesPtyWindow
/// resizing?
int Window::wresize(int rows, int cols)
{
std::lock_guard writeLock(writeToPseudoTerminalMutex);
lock_guard nCursesLock(*writeToNCursesMutex);
lock_guard writeLock(writeToPseudoTerminalMutex);
winsize windowSize =
{
.ws_row = narrow<unsigned short>(rows)

View file

@ -29,7 +29,7 @@ namespace krikkel::NCursesPtyWindow
class Window : public NCursesWindow
{
public:
Window(int lines, int columns, int y, int x);
Window(std::mutex *writeToNCursesMutex, int lines, int columns, int y, int x);
~Window();
int getFdPtyClient() const;
@ -49,6 +49,7 @@ namespace krikkel::NCursesPtyWindow
struct termios terminalParameters;
VTerm *pseudoTerminal;
std::mutex writeToPseudoTerminalMutex;
std::mutex *writeToNCursesMutex;
VTermScreen *pseudoTerminalScreen;
static VTermScreenCallbacks screenCallbacks;
/// @todo one line is at most 4096 chars long