]> git.cworth.org Git - sup/commitdiff
exception cleanup: synchronize access, and require fastthread
authorWilliam Morgan <wmorgan-sup@masanjin.net>
Wed, 4 Jun 2008 18:53:15 +0000 (11:53 -0700)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Wed, 4 Jun 2008 18:53:15 +0000 (11:53 -0700)
bin/sup
lib/sup.rb

diff --git a/bin/sup b/bin/sup
index 723b1ed1e850b5eebadaf5804484793cc0a07b3f..eb45461ee95e437293e05134033b5079d8088164 100644 (file)
--- a/bin/sup
+++ b/bin/sup
@@ -5,6 +5,7 @@ require 'ncurses'
 require 'curses'
 require 'fileutils'
 require 'trollop'
+require 'fastthread'
 require "sup"
 
 BIN_VERSION = "git"
@@ -21,7 +22,6 @@ EOS
   exit(-1)
 end
 
-$exceptions = []
 $opts = Trollop::options do
   version "sup v#{Redwood::VERSION}"
   banner <<EOS
@@ -224,7 +224,7 @@ begin
     SearchResultsMode.spawn_from_query $opts[:search]
   end
 
-  until $exceptions.nonempty? || SuicideManager.die?
+  until Redwood::exceptions.nonempty? || SuicideManager.die?
     c = Ncurses.nonblocking_getch
     next unless c
     bm.erase_flash
@@ -305,7 +305,7 @@ begin
 
   bm.kill_all_buffers if SuicideManager.die?
 rescue Exception => e
-  $exceptions << [e, "main"]
+  Redwood::record_exception e, "main"
 ensure
   unless $opts[:no_threads]
     PollManager.stop if PollManager.instantiated?
@@ -321,7 +321,7 @@ ensure
     Redwood::log "I've been ordered to commit seppuku. I obey!"
   end
 
-  if $exceptions.empty?
+  if Redwood::exceptions.empty?
     Redwood::log "no fatal errors. good job, william."
     Index.save
   else
@@ -331,9 +331,9 @@ ensure
   Index.unlock
 end
 
-unless $exceptions.empty?
+unless Redwood::exceptions.empty?
   File.open(File.join(BASE_DIR, "exception-log.txt"), "w") do |f|
-    $exceptions.each do |e, name|
+    Redwood::exceptions.each do |e, name|
       f.puts "--- #{e.class.name} from thread: #{name}"
       f.puts e.message, e.backtrace
     end
@@ -350,7 +350,7 @@ Sincerely,
 William
 ----------------------------------------------------------------
 EOS
-  $exceptions.each do |e, name|
+  Redwood::exceptions.each do |e, name|
     puts "--- #{e.class.name} from thread: #{name}"
     puts e.message, e.backtrace
   end
index 9e9026747bca29324fe44cf4b4df12c1d8e54ccc..5211eec291bbc2750c79f88c9e2e5248483bfd9b 100644 (file)
@@ -50,7 +50,18 @@ module Redwood
   YAML_DOMAIN = "masanjin.net"
   YAML_DATE = "2006-10-01"
 
-## record exceptions thrown in threads nicely
+  ## record exceptions thrown in threads nicely
+  @exceptions = []
+  @exception_mutex = Mutex.new
+
+  attr_reader :exceptions
+  def record_exception e, name
+    @exception_mutex.synchronize do
+      @exceptions ||= []
+      @exceptions << [e, name]
+    end
+  end
+
   def reporting_thread name
     if $opts[:no_threads]
       yield
@@ -59,14 +70,13 @@ module Redwood
         begin
           yield
         rescue Exception => e
-          $exceptions ||= []
-          $exceptions << [e, name]
-          raise
+          record_exception e, name
         end
       end
     end
   end
-  module_function :reporting_thread
+
+  module_function :reporting_thread, :record_exception, :exceptions
 
 ## one-stop shop for yamliciousness
   def save_yaml_obj object, fn, safe=false