<commit_before>module Concurrent
  # TODO rename to Synchronization
  # TODO add newCondition
  module Synchronization
    # Safe synchronization under any Ruby implementation.
    # It provides methods like {#synchronize}, {#wait}, {#signal} and {#broadcast}.
    # Provides a single layer which can improve its implementation over time without changes needed to
    # the classes using it. Use {Synchronization::Object} not this abstract class.
    #
    # @note this object does not support usage together with
    #   [`Thread#wakeup`](http://ruby-doc.org/core-2.2.0/Thread.html#method-i-wakeup)
    #   and [`Thread#raise`](http://ruby-doc.org/core-2.2.0/Thread.html#method-i-raise).
    #   `Thread#sleep` and `Thread#wakeup` will work as expected but mixing `Synchronization::Object#wait` and
    #   `Thread#wakeup` will not work on all platforms.
    #
    # @see {Event} implementation as an example of this class use
    #
    # @example simple
    #   class AnClass < Synchronization::Object
    #     def initialize
    #       super
    #       synchronize { @value = 'asd' }
    #     end
    #
    #     def value
    #       synchronize { @value }
    #     end
    #   end
    class AbstractObject

      # @abstract for helper ivar initialization if needed,
      #   otherwise it can be left empty. It has to call ns_initialize.
      def initialize(*args, &block)
        raise NotImplementedError
      end

      # @yield runs the block synchronized against this object,
      #   equvivalent of java's `synchronize(this) {}`
      def synchronize
        raise NotImplementedError
      end

      private

      # wait until another thread calls #signal or #broadcast,
      # spurious wake-ups can happen.
      # @param [Numeric, nil] timeout in seconds, `nil` means no timeout
      # @return [self]
      # @note intended to be made public if required in child classes
      def wait(timeout = nil)
        synchronize { ns_wait(timeout) }
        self
      end

      # initialization of the object called inside synchronize block
      def ns_initialize(*args, &block)
      end

      # Wait until condition is met or timeout passes,
      # protects against spurious wake-ups.
      # @param [Numeric, nil] timeout in seconds, `nil` means no timeout
      # @yield condition to be met
      # @yieldreturn [true, false]
      # @return [true, false]
      # @note intended to be made public if required in child classes
      def wait_until(timeout = nil, &condition)
        synchronize { ns_wait_until(timeout, &condition) }
      end

      # signal one waiting thread
      # @return [self]
      # @note intended to be made public if required in child classes
      def signal
        synchronize { ns_signal }
        self
      end

      # broadcast to all waiting threads
      # @return [self]
      # @note intended to be made public if required in child classes
      def broadcast
        synchronize { ns_broadcast }
        self
      end

      # @note only to be used inside synchronized block
      # @yield condition
      # @return [true, false]
      # see #wait_until
      def ns_wait_until(timeout, &condition)
        if timeout
          wait_until = Concurrent.monotonic_time + timeout
          loop do
            now              = Concurrent.monotonic_time
            condition_result = condition.call
            # 0.001 correction to avoid error when `wait_until - now` is smaller than 0.0005 and rounded to 0
            # when passed to java #wait(long timeout)
            return condition_result if (now + 0.001) >= wait_until || condition_result
            ns_wait wait_until - now
          end
        else
          ns_wait timeout until condition.call
          true
        end
      end

      # @note only to be used inside synchronized block
      # @return [self]
      # @see #wait
      def ns_wait(timeout = nil)
        raise NotImplementedError
      end

      # @note only to be used inside synchronized block
      # @return [self]
      # @see #signal
      def ns_signal
        raise NotImplementedError
      end

      # @note only to be used inside synchronized block
      # @return [self]
      # @see #broadcast
      def ns_broadcast
        raise NotImplementedError
      end
    end
  end
end
<commit_msg>Remove wait, signal, broadcast in favor of ns_ counterparts<commit_after>module Concurrent
  # TODO rename to Synchronization
  # TODO add newCondition
  module Synchronization
    # Safe synchronization under any Ruby implementation.
    # It provides methods like {#synchronize}, {#wait}, {#signal} and {#broadcast}.
    # Provides a single layer which can improve its implementation over time without changes needed to
    # the classes using it. Use {Synchronization::Object} not this abstract class.
    #
    # @note this object does not support usage together with
    #   [`Thread#wakeup`](http://ruby-doc.org/core-2.2.0/Thread.html#method-i-wakeup)
    #   and [`Thread#raise`](http://ruby-doc.org/core-2.2.0/Thread.html#method-i-raise).
    #   `Thread#sleep` and `Thread#wakeup` will work as expected but mixing `Synchronization::Object#wait` and
    #   `Thread#wakeup` will not work on all platforms.
    #
    # @see {Event} implementation as an example of this class use
    #
    # @example simple
    #   class AnClass < Synchronization::Object
    #     def initialize
    #       super
    #       synchronize { @value = 'asd' }
    #     end
    #
    #     def value
    #       synchronize { @value }
    #     end
    #   end
    class AbstractObject

      # @abstract for helper ivar initialization if needed,
      #   otherwise it can be left empty. It has to call ns_initialize.
      def initialize(*args, &block)
        raise NotImplementedError
      end

      # @yield runs the block synchronized against this object,
      #   equvivalent of java's `synchronize(this) {}`
      def synchronize
        raise NotImplementedError
      end

      private

      # initialization of the object called inside synchronize block
      def ns_initialize(*args, &block)
      end

      # Wait until condition is met or timeout passes,
      # protects against spurious wake-ups.
      # @param [Numeric, nil] timeout in seconds, `nil` means no timeout
      # @yield condition to be met
      # @yieldreturn [true, false]
      # @return [true, false] if condition met
      # @note only to be used inside synchronized block
      # @note to provide direct access to this method in a descendant add method
      #   ```
      #   def wait_until(timeout = nil, &condition)
      #     synchronize { ns_wait_until(timeout, &condition) }
      #   end
      #   ```
      def ns_wait_until(timeout, &condition)
        if timeout
          wait_until = Concurrent.monotonic_time + timeout
          loop do
            now              = Concurrent.monotonic_time
            condition_result = condition.call
            # 0.001 correction to avoid error when `wait_until - now` is smaller than 0.0005 and rounded to 0
            # when passed to java #wait(long timeout)
            return condition_result if (now + 0.001) >= wait_until || condition_result
            ns_wait wait_until - now
          end
        else
          ns_wait timeout until condition.call
          true
        end
      end

      # Wait until another thread calls #signal or #broadcast,
      # spurious wake-ups can happen.
      #
      # @param [Numeric, nil] timeout in seconds, `nil` means no timeout
      # @return [self]
      # @note only to be used inside synchronized block
      # @note to provide direct access to this method in a descendant add method
      #   ```
      #   def wait(timeout = nil)
      #     synchronize { ns_wait(timeout) }
      #   end
      #   ```
      def ns_wait(timeout = nil)
        raise NotImplementedError
      end

      # Signal one waiting thread
      # @return [self]
      # @note only to be used inside synchronized block
      # @note to provide direct access to this method in a descendant add method
      #   ```
      #   def signal
      #     synchronize { ns_signal }
      #   end
      #   ```
      def ns_signal
        raise NotImplementedError
      end

      # Broadcast to all waiting threads
      # @return [self]
      # @note only to be used inside synchronized block
      # @note to provide direct access to this method in a descendant add method
      #   ```
      #   def broadcast
      #     synchronize { ns_broadcast }
      #   end
      #   ```
      def ns_broadcast
        raise NotImplementedError
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.07374392220421394, 'fraction_numerical': 0.0046307015512850195, 'mean_word_length': 2.1644688644688643, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5005468', 'n_tokens_mistral': 2367, 'n_tokens_neox': 2313, 'n_words': 859}