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