From 597dd658f4881a7e9b71c7072bcd1a257b40bbdc Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Wed, 23 Dec 2020 23:51:16 +0100 Subject: [PATCH] Lock during refresh in order to manage concurrency, fixes #5 --- squinnondation/hazel.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/squinnondation/hazel.py b/squinnondation/hazel.py index 28bdb16..db99db9 100644 --- a/squinnondation/hazel.py +++ b/squinnondation/hazel.py @@ -4,7 +4,7 @@ from datetime import datetime from random import randint from typing import Any, Tuple from ipaddress import IPv6Address -from threading import Thread +from threading import Thread, RLock import curses import re import socket @@ -55,6 +55,9 @@ class Squirrel(Hazelnut): self.input_index = 0 self.last_line = -1 + # Lock the refresh function in order to avoid concurrent refresh + self.refresh_lock = RLock() + self.history = [] self.received_messages = dict() self.history_pad = curses.newpad(curses.LINES - 2, curses.COLS) @@ -371,6 +374,8 @@ class Squirrel(Hazelnut): """ Rewrite the history of the messages. """ + self.refresh_lock.acquire() + y, x = self.squinnondation.screen.getmaxyx() if curses.is_term_resized(curses.LINES, curses.COLS): curses.resizeterm(y, x) @@ -398,10 +403,14 @@ class Squirrel(Hazelnut): y_offset += true_width // (curses.COLS - 1) self.history_pad.refresh(0, 0, 0, 0, curses.LINES - 2, curses.COLS) + self.refresh_lock.release() + def refresh_input(self) -> None: """ Redraw input line. Must not be called while the message is not sent. """ + self.refresh_lock.acquire() + self.input_pad.erase() color_id = sum(ord(c) for c in self.nickname) % 6 + 1 self.input_pad.addstr(0, 0, "<") @@ -416,6 +425,8 @@ class Squirrel(Hazelnut): self.input_pad.addstr(0, self.input_pad.getmaxyx()[1] - 3, "😀") self.input_pad.refresh(0, 0, curses.LINES - 1, 0, curses.LINES - 1, curses.COLS - 1) + self.refresh_lock.release() + def refresh_emoji_pad(self) -> None: """ Display the emoji pad if necessary. @@ -425,6 +436,8 @@ class Squirrel(Hazelnut): from emoji import unicode_codes + self.refresh_lock.acquire() + self.emoji_pad.erase() if self.emoji_panel_page > 0: @@ -455,6 +468,8 @@ class Squirrel(Hazelnut): self.emoji_pad.refresh(0, 0, curses.LINES - height - 2, curses.COLS - width - 2, curses.LINES - 2, curses.COLS - 2) + self.refresh_lock.release() + class Worm(Thread): """