added mutex to lock concurrent writes to ncurses

Gentoo
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() int App::run()
{ {
Window *ptyWindow = new Window( std::mutex writeMutex;
Root_Window->lines()
Window *ptyWindow = new Window(&writeMutex
, Root_Window->lines()
, Root_Window->cols() , Root_Window->cols()
, 0 , 0
, 0); , 0);

View File

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

View File

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