Lock during refresh in order to manage concurrency, fixes #5

This commit is contained in:
Yohann D'ANELLO 2020-12-23 23:51:16 +01:00
parent 39b4157836
commit 597dd658f4
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 16 additions and 1 deletions

View File

@ -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):
"""