libsaria: Do locking in queue.py
Now I don't need to worry about forgetting to get the lock somewhere else.
This commit is contained in:
parent
2c185dec83
commit
d99e7a4955
|
@ -1,10 +1,5 @@
|
|||
# Bryan Schumaker (12/12/2010)
|
||||
|
||||
import threading
|
||||
queue_lock = threading.Lock()
|
||||
lock_queue = queue_lock.acquire
|
||||
unlock_queue = queue_lock.release
|
||||
|
||||
import libsaria
|
||||
import queue
|
||||
|
||||
|
@ -14,20 +9,10 @@ source = None
|
|||
filtered = False
|
||||
visible = None
|
||||
|
||||
def add_ids(id_list):
|
||||
lock_queue()
|
||||
queue.add_ids(id_list)
|
||||
unlock_queue()
|
||||
|
||||
def rm_ids(id_list):
|
||||
lock_queue()
|
||||
queue.rm_ids(id_list)
|
||||
unlock_queue()
|
||||
|
||||
def reset():
|
||||
lock_queue()
|
||||
queue.reset()
|
||||
unlock_queue()
|
||||
# Function pointers for convenience
|
||||
add_ids = queue.add_ids
|
||||
rm_ids = queue.rm_ids
|
||||
reset = queue.reset
|
||||
|
||||
def init():
|
||||
global library
|
||||
|
@ -95,9 +80,7 @@ def next():
|
|||
global cur_id
|
||||
if len(queue.queue_list) == 0:
|
||||
return None
|
||||
lock_queue()
|
||||
cur_id = queue.pop()
|
||||
unlock_queue()
|
||||
source.set_current()
|
||||
return library.get_attrs(cur_id, "filepath")
|
||||
|
||||
|
|
|
@ -1,24 +1,35 @@
|
|||
# Bryan Schumaker (5 / 7 / 2011)
|
||||
# WARNING: THESE FUNCTIONS DIRECTLY MANIPULATE THE QUEUE.
|
||||
# YOU SHOULD BE HOLDING THE queue_lock LOCATED IN __init__.py
|
||||
# BEFORE CALLING ANYTHING HERE.
|
||||
|
||||
import threading
|
||||
queue_lock = threading.Lock()
|
||||
lock_queue = queue_lock.acquire
|
||||
unlock_queue = queue_lock.release
|
||||
|
||||
# List of queued songs
|
||||
queue_list = []
|
||||
|
||||
def add_ids(id_list):
|
||||
lock_queue()
|
||||
queue_list.extend(id_list)
|
||||
unlock_queue()
|
||||
|
||||
def rm_ids(id_list):
|
||||
lock_queue()
|
||||
for id in id_list:
|
||||
if id in queue_list:
|
||||
queue_list.remove(id)
|
||||
unlock_queue()
|
||||
|
||||
def pop():
|
||||
lock_queue()
|
||||
id = None
|
||||
if len(queue_list) > 0:
|
||||
return queue_list.pop(0)
|
||||
return None
|
||||
id = queue_list.pop(0)
|
||||
unlock_queue()
|
||||
return id
|
||||
|
||||
def reset():
|
||||
global queue_list
|
||||
lock_queue()
|
||||
queue_list = []
|
||||
unlock_queue()
|
||||
|
|
Loading…
Reference in New Issue