Sha256: 6600aa9b67d243e19d4f0396eb771472b63da96480ba6d0d26876962b2c33fd6

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

require 'thread'

module Skylight
  class GC
    METHODS = [ :enable, :total_time ]
    TH_KEY  = :SK_GC_CURR_WINDOW

    def self.update
      if win = Thread.current[TH_KEY]
        win.update
      end
    end

    def self.time
      if win = Thread.current[TH_KEY]
        win.time
      else
        0.0
      end
    end

    def initialize(profiler)
      @listeners = []
      @lock      = Mutex.new

      if METHODS.all? { |m| profiler.respond_to?(m) }
        @profiler = profiler
      end
    end

    def start_track
      return if Thread.current[TH_KEY]

      unless @profiler
        win = Window.new(nil)
      else
        win = Window.new(self)

        @lock.synchronize do
          @listeners << win
        end
      end

      Thread.current[TH_KEY] = win
    end

    def stop_track
      if win = Thread.current[TH_KEY]
        Thread.current[TH_KEY] = nil
        win.release
      end
    end

    def track
      return unless block_given?

      start_track

      begin
        yield
      ensure
        stop_track
      end
    end

    def release(win)
      @lock.synchronize do
        @listeners.delete(win)
      end
    end

    def update
      @lock.synchronize do
        time = @profiler.total_time

        if time > 0
          @profiler.clear
          @listeners.each do |l|
            l.add(time)
          end
        end
      end

      nil
    end

    class Window
      attr_reader :time

      def initialize(global)
        @global = global
        @time   = 0.0
      end

      def update
        @global.update if @global
      end

      def add(time)
        @time += time
      end

      def release
        @global.release(self) if @global
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
skylight-0.1.0 lib/skylight/gc.rb
skylight-0.1.0.alpha2 lib/skylight/gc.rb
skylight-0.1.0.alpha1 lib/skylight/gc.rb