<commit_before>#
#		thread.rb - thread support classes
#			$Date: 2006-12-31 07:02:22 -0800 (Sun, 31 Dec 2006) $
#			by <NAME> <<EMAIL>>
#
# Copyright (C) 2001  <NAME>
# Copyright (C) 2000  Network Applied Communication Laboratory, Inc.
# Copyright (C) 2000  Information-technology Promotion Agency, Japan
#

unless defined? Thread
  raise "Thread not available for this ruby interpreter"
end

unless defined? ThreadError
  class ThreadError < StandardError
  end
end

if $DEBUG
  Thread.abort_on_exception = true
end

class Thread
  #
  # Wraps a block in Thread.critical, restoring the original value upon exit
  # from the critical section.
  #
  def Thread.exclusive
    _old = Thread.critical
    begin
      Thread.critical = true
      return yield
    ensure
      Thread.critical = _old
    end
  end
end

class Mutex
  def initialize
    @lock = Channel.new
    @available = Channel.new
    @lock << nil
  end

  def locked?
    owner = @lock.receive
    !!owner
  ensure
    @lock << owner
  end

  def try_lock
    owner = @lock.receive
    unless owner
      owner = Thread.current
      true
    else
      false
    end
  ensure
    @lock << owner
  end

  def lock
    owner = @lock.receive
    while owner
      @lock << owner
      @available.receive
      owner = @lock.receive
    end
    owner = Thread.current
    self
  ensure
    @lock << owner
  end

  def unlock
    owner = @lock.receive
    if owner == Thread.current
      owner = nil 
      @available << nil
    else
      raise ThreadError, "Not owner"
    end
    self
  ensure
    @lock << owner
  end

  def synchronize
    lock
    yield
  ensure
    unlock
  end
end<commit_msg>still not entirely right, but better; I should not be trying to do this stuff when I'm sick as a dog<commit_after>#
#		thread.rb - thread support classes
#			$Date: 2006-12-31 07:02:22 -0800 (Sun, 31 Dec 2006) $
#			by <NAME> <<EMAIL>>
#
# Copyright (C) 2001  <NAME>
# Copyright (C) 2000  Network Applied Communication Laboratory, Inc.
# Copyright (C) 2000  Information-technology Promotion Agency, Japan
#

unless defined? Thread
  raise "Thread not available for this ruby interpreter"
end

unless defined? ThreadError
  class ThreadError < StandardError
  end
end

if $DEBUG
  Thread.abort_on_exception = true
end

class Thread
  #
  # Wraps a block in Thread.critical, restoring the original value upon exit
  # from the critical section.
  #
  def Thread.exclusive
    _old = Thread.critical
    begin
      Thread.critical = true
      return yield
    ensure
      Thread.critical = _old
    end
  end
end

class Mutex
  def initialize
    @lock = Channel.new
    @n_waiters = 0
    @unlocked = Channel.new
    @lock << nil
  end

  def locked?
    owner = @lock.receive
    !!owner
  ensure
    @lock << owner
  end

  def try_lock
    owner = @lock.receive
    unless owner
      owner = Thread.current
      true
    else
      false
    end
  ensure
    @lock << owner
  end

  def lock
    owner = @lock.receive
    while owner
      @n_waiters += 1
      @lock << owner
      @unlocked.receive
      owner = @lock.receive
      @n_waiters -= 1
    end
    owner = Thread.current
    self
  ensure
    @lock << owner
  end

  def unlock
    owner = @lock.receive
    raise ThreadError, "Not owner" unless owner == Thread.current
    owner = nil 
    @unlocked << self if @n_waiters.nonzero?
    self
  ensure
    @lock << owner
  end

  def synchronize
    lock
    yield
  ensure
    unlock
  end
end
{'fraction_non_alphanumeric': 0.07475560667050028, 'fraction_numerical': 0.02156411730879816, 'mean_word_length': 2.358108108108108, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 11, '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': '2677625', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1069, 'n_words': 379}