Sha256: ec9e7cef500bc1bca910bbeb89ffb783299aab9ade8b08901d2a8836b1f98a7e

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

module Byebug
  module DAP
    class Channel
      def initialize
        @mu = Mutex.new
        @cond = ConditionVariable.new
        @closed = false
        @have = false
      end

      def close
        @mu.synchronize {
          @closed = true
          @cond.broadcast
        }
      end

      def pop
        synchronize_loop {
          return if @closed

          if @have
            @cond.signal
            @have = false
            return @value
          end

          @cond.wait(@mu)
        }
      end

      def push(message, timeout: nil)
        deadline = timeout + Time.now.to_f unless timeout.nil?

        synchronize_loop {
          raise RuntimeError, "Send on closed channel" if @closed

          unless @have
            @cond.signal
            @have = true
            @value = message
            return
          end

          if timeout.nil?
            @cond.wait(@mu)

          else
            remaining = deadline - Time.now.to_f
            return yield if remaining < 0

            @cond.wait(@mu, remaining)
          end
        }
      end

      private

      def synchronize_loop
        @mu.synchronize { loop { yield } }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
byebug-dap-0.1.3 lib/byebug/dap/helpers/channel.rb
byebug-dap-0.1.2 lib/byebug/dap/channel.rb
byebug-dap-0.1.1 lib/byebug/dap/channel.rb
byebug-dap-0.1.0 lib/byebug/dap/channel.rb