lib: Add locking around TagStore add() and remove()

But make sure we don't call the publishers while holding the lock to
avoid deadlocks.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-06-21 13:23:52 -04:00
parent d03a0ff422
commit 7b6b4051ee
2 changed files with 16 additions and 6 deletions

View File

@ -1,19 +1,26 @@
# Copyright 2021 (c) Anna Schumaker.
from . import publisher
from . import tag
import threading
class TagStore:
def __init__(self):
self.store = dict()
self.lock = threading.Lock()
self.Added = publisher.Publisher()
self.Removed = publisher.Publisher()
def add(self, name, track=None):
name = name.strip()
if (t := self.store.get(name)) == None:
def __get_tag__(self, name):
with self.lock:
if (t := self.store.get(name)) != None:
return t
t = tag.Tag(name)
self.store[name] = t
self.Added.publish(t)
self.Added.publish(t)
return t
def add(self, name, track=None):
t = self.__get_tag__(name.strip())
if track:
t.add_track(track)
return t
@ -21,6 +28,7 @@ class TagStore:
def remove(self, t, track=None):
if track:
t.remove_track(track)
if (track==None or len(t) == 0) and t in self.store.values():
self.store.pop(t.name)
if (track == None or len(t) == 0) and t in self.store.values():
with self.lock:
self.store.pop(t.name)
self.Removed.publish(t)

View File

@ -1,6 +1,7 @@
# Copyright 2021 (c) Anna Schumaker.
from . import publisher
from . import tagstore
import threading
import unittest
class TestTagStore(unittest.TestCase):
@ -17,6 +18,7 @@ class TestTagStore(unittest.TestCase):
def test_tag_store(self):
store = tagstore.TagStore()
self.assertEqual(store.store, { })
self.assertIsInstance(store.lock, type(threading.Lock()))
self.assertIsInstance(store.Added, publisher.Publisher)
self.assertIsInstance(store.Removed, publisher.Publisher)