Fix thread synchronization errors in Mod_Audio

master
Marrub 2017-09-11 09:25:40 -04:00
parent a9a9961c37
commit 6de154b22e
1 changed files with 12 additions and 28 deletions

View File

@ -103,18 +103,15 @@ class Mod_Audio < Vrobot4::Module::Module
def c_queue m, argv
if argv.empty?
list_queue m
elsif push_queue m, argv and (@queue.size == 1 or not @running)
start_playback m, 0
elsif push_queue m, argv
if @queue_mtx.synchronize {@queue.size == 1 or not @running}
start_playback m, 0
end
end
end
def c_rmqueue m, argv
begin
@queue_mtx.lock
rm_queue @queue[argv.to_i + 1]
ensure
@queue_mtx.unlock
end
@queue_mtx.synchronize {rm_queue @queue[argv.to_i + 1]}
end
def c_volume m, argv
@ -137,8 +134,7 @@ class Mod_Audio < Vrobot4::Module::Module
private
def list_queue m
begin
@queue_mtx.lock
@queue_mtx.synchronize do
text = ""
i = 1
for qi in @queue
@ -149,8 +145,6 @@ class Mod_Audio < Vrobot4::Module::Module
end
if text.empty? then m.reply "No items in queue."
else m.reply_b text end
ensure
@queue_mtx.unlock
end
end
@ -162,15 +156,12 @@ class Mod_Audio < Vrobot4::Module::Module
begin
qi = QueueItem.new(uri, stream)
begin
@queue_mtx.lock
@queue_mtx.synchronize do
if @queue.any?
@queue.last.next = qi
qi.prev = @queue.last
end
@queue.push qi
ensure
@queue_mtx.unlock
end
if qi.is_stream
m.reply "Stream <" + uri.to_s + "> queued"
@ -194,13 +185,11 @@ class Mod_Audio < Vrobot4::Module::Module
end
def start_playback m, start
@running = true
qi = nil
begin
@queue_mtx.lock
@queue_mtx.synchronize do
@running = true
qi = @queue[start]
ensure
@queue_mtx.unlock
end
Thread.new do
@ -213,16 +202,11 @@ class Mod_Audio < Vrobot4::Module::Module
m.reply "Error playing queue item."
end
begin
@queue_mtx.lock
rm_queue qi
ensure
@queue_mtx.unlock
end
@queue_mtx.synchronize {rm_queue qi}
unless (qi = qi.next)
m.reply "End of queue reached."
@running = false
@queue_mtx.synchronize {@running = false}
break
end
end