<commit_before>

module Cod
  # Describes a queue that stores work items given through #schedule and works
  # through those items in order if #predicate is true. 
  #
  # Synopsis: 
  #   queue = WorkQueue.new
  #   queue.predicate { true }
  #   queue.schedule { 
  #     # some work
  #   }
  # 
  #   # Will try to work through items right now. 
  #   queue.try_work
  # 
  #   # Will cleanly shutdown background threads, but not finish work. 
  #   queue.shutdown
  # 
  class WorkQueue
    def initialize
      # NOTE: This is an array that is protected by careful coding, rather
      # than a mutex. Queue would be right, but Rubys GIL will interfere with
      # that producing more deadlocks than I would like.
      @queue = Array.new
      
      @thread = Thread.start(&method(:thread))
    end
    def predicate
    end
    def schedule(&work)
      @queue << work
    end
    def shutdown
      @shutdown_requested = true
      @thread.join
    end
    def size
      @queue.size
    end
  private
    def thread
      Thread.current.abort_on_exception = true
      
      loop do
        sleep 0.1
        return if @shutdown_requested
      end
    end
  end
end<commit_msg>. lotsa comments<commit_after>

module Cod
  # Describes a queue that stores work items given through #schedule and works
  # through those items in order if #predicate is true. 
  #
  # Synopsis: 
  #   queue = WorkQueue.new
  #   queue.predicate { true }
  #   queue.schedule { 
  #     # some work
  #   }
  # 
  #   # Will try to work through items right now. 
  #   queue.try_work
  # 
  #   # Will cleanly shutdown background threads, but not finish work. 
  #   queue.shutdown
  # 
  class WorkQueue
    def initialize
      # NOTE: This is an array that is protected by careful coding, rather
      # than a mutex. Queue would be right, but Rubys GIL will interfere with
      # that producing more deadlocks than I would like.
      @queue = Array.new
      
      @thread = Thread.start(&method(:thread))
    end
    
    # Before any kind of work is attempted, this predicate must evaluate to 
    # true. It is tested repeatedly. 
    #
    # Example: 
    #   work_queue.predicate { connection.established? }
    #
    def predicate
    end

    # Schedules a piece of work. 
    # Example: 
    #   work_queue.schedule { a_piece_of_work }
    #
    def schedule(&work)
      @queue << work
    end

    # Shuts down the queue properly, without waiting for work to be completed.
    #
    def shutdown
      @shutdown_requested = true
      @thread.join
    end
    
    # Returns the size of the queue. 
    #
    def size
      @queue.size
    end
  private
    def thread
      Thread.current.abort_on_exception = true
      
      loop do
        sleep 0.1
        return if @shutdown_requested
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.06501766784452297, 'fraction_numerical': 0.0014134275618374558, 'mean_word_length': 2.018123667377399, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2603115', 'n_tokens_mistral': 876, 'n_tokens_neox': 833, 'n_words': 301}