<commit_before>require "monitor"

#This class is used to seamlessly use leaky classes without working through 'Ruby_process'.
#===Examples
#  Ruby_process::Cproxy.run do |data|
#    data[:subproc].static(:Object, :require, "rubygems")
#    data[:subproc].static(:Object, :require, "rexml/document")
#    
#    doc = Ruby_process::Cproxy::REXML::Document.new("test")
#    strio = StringIO.new
#    doc.write(strio)
#    puts strio.string #=> "<test/>"
#    raise "REXML shouldnt be defined?" if Kernel.const_defined?(:REXML)
class Ruby_process::Cproxy
  #Lock is used to to create new Ruby-process-instances and not doing double-counts.
  @@lock = Monitor.new
  
  #Counts how many instances are using the Cproxy-module. This way it can be safely unset once no-body is using it again.
  @@instances = 0
  
  #This variable will hold the 'Ruby_process'-object where sub-objects will be created.
  @@subproc = nil
  
  #All use should go through this method to automatically destroy sub-processes and keep track of ressources.
  def self.run
    #Increase count of instances that are using Cproxy and set the subproc-object if not already set.
    @@lock.synchronize do
      @@instances += 1
      
      #Check if the sub-process is alive.
      if @@subproc and (!@@subproc.alive? or @@subproc.destroyed?)
        @@subproc.destroy
        @@subproc = nil
      end
      
      #Start a new subprocess if none is defined and active.
      if !@@subproc
        @@subproc = Ruby_process.new(:title => "ruby_process_cproxy", :debug => false)
        @@subproc.spawn_process
      end
    end
    
    begin
      yield(:subproc => @@subproc)
      raise "'run'-caller destroyed sub-process. This shouldn't happen." if @@subproc.destroyed?
    ensure<commit_msg>Tried to fix various bugs.<commit_after>require "monitor"

#This class is used to seamlessly use leaky classes without working through 'Ruby_process'.
#===Examples
#  Ruby_process::Cproxy.run do |data|
#    data[:subproc].static(:Object, :require, "rubygems")
#    data[:subproc].static(:Object, :require, "rexml/document")
#    
#    doc = Ruby_process::Cproxy::REXML::Document.new("test")
#    strio = StringIO.new
#    doc.write(strio)
#    puts strio.string #=> "<test/>"
#    raise "REXML shouldnt be defined?" if Kernel.const_defined?(:REXML)
class Ruby_process::Cproxy
  #Lock is used to to create new Ruby-process-instances and not doing double-counts.
  @@lock = Monitor.new
  
  #Counts how many instances are using the Cproxy-module. This way it can be safely unset once no-body is using it again.
  @@instances = 0
  
  #This variable will hold the 'Ruby_process'-object where sub-objects will be created.
  @@subproc = nil
  
  #All use should go through this method to automatically destroy sub-processes and keep track of ressources.
  def self.run
    #Increase count of instances that are using Cproxy and set the subproc-object if not already set.
    @@lock.synchronize do
      #Check if the sub-process is alive.
      if @@subproc and (!@@subproc.alive? or @@subproc.destroyed?)
        @@subproc.destroy
        @@subproc = nil
      end
      
      #Start a new subprocess if none is defined and active.
      if !@@subproc
        @@subproc = Ruby_process.new(:title => "ruby_process_cproxy", :debug => false)
        @@subproc.spawn_process
      end
      
      @@instances += 1
    end
    
    begin
      yield(:subproc => @@subproc)
      raise "'run'-caller destroyed sub-process. This shouldn't happen." if @@subproc.destroyed?
    ensure
{'fraction_non_alphanumeric': 0.10953863572035098, 'fraction_numerical': 0.0011321822813472968, 'mean_word_length': 3.197149643705463, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '210481', 'n_tokens_mistral': 1110, 'n_tokens_neox': 1063, 'n_words': 361}