Make the Basic/Colored UI objects threadsafe

This commit is contained in:
Mitchell Hashimoto 2013-03-21 18:26:50 -07:00
parent 7b0745abcc
commit 7446b3c4ef
1 changed files with 14 additions and 3 deletions

View File

@ -1,3 +1,5 @@
require "thread"
require "log4r" require "log4r"
require "vagrant/util/safe_puts" require "vagrant/util/safe_puts"
@ -53,6 +55,12 @@ module Vagrant
class Basic < Interface class Basic < Interface
include Util::SafePuts include Util::SafePuts
def initialize
super
@lock = Mutex.new
end
# Use some light meta-programming to create the various methods to # Use some light meta-programming to create the various methods to
# output text to the UI. These all delegate the real functionality # output text to the UI. These all delegate the real functionality
# to `say`. # to `say`.
@ -123,10 +131,13 @@ module Vagrant
# to based on the type of the message # to based on the type of the message
channel = type == :error || opts[:channel] == :error ? $stderr : $stdout channel = type == :error || opts[:channel] == :error ? $stderr : $stdout
# Output! # Output! We wrap this in a lock so that it safely outputs only
# one line at a time.
@lock.synchronize do
safe_puts(format_message(type, message, opts), safe_puts(format_message(type, message, opts),
:io => channel, :printer => printer) :io => channel, :printer => printer)
end end
end
def scope(scope_name) def scope(scope_name)
BasicScope.new(self, scope_name) BasicScope.new(self, scope_name)