Module: Mutex_m

Module | Mutex_m
In: | mutex_m.rb

      mutex_m.rb -
          $Release Version: 3.0$
          $Revision: 1.7 $
          $Date: 1998/02/27 04:28:57 $
          Original from mutex.rb
          by Keiju ISHITSUKA(keiju@ishitsuka.com)
          modified by matz
          patched by akira yamada

—

      Usage:
          require "mutex_m.rb"
          obj = Object.new
          obj.extend Mutex_m
          ...
          extended object can be handled like Mutex
          or
          class Foo
            include Mutex_m
            ...
          end
          obj = Foo.new
          this obj can be handled like Mutex

### Methods

append_features define_aliases extend_object mu_extended mu_lock mu_locked? mu_synchronize mu_try_lock mu_unlock new

### Public Class methods

append_features(cl)

[Source]

    # File mutex_m.rb, line 38
      def Mutex_m.append_features(cl)
        super
        define_aliases(cl) unless cl.instance_of?(Module)
      end

define_aliases(cl)

[Source]

    # File mutex_m.rb, line 28
      def Mutex_m.define_aliases(cl)
        cl.module_eval %q{
          alias locked? mu_locked?
          alias lock mu_lock
          alias unlock mu_unlock
          alias try_lock mu_try_lock
          alias synchronize mu_synchronize
        }
      end

extend_object(obj)

[Source]

    # File mutex_m.rb, line 43
      def Mutex_m.extend_object(obj)
        super
        obj.mu_extended
      end

new(*args)

[Source]

    # File mutex_m.rb, line 114
      def initialize(*args)
        mu_initialize
        super
      end

### Public Instance methods

mu_extended()

[Source]

    # File mutex_m.rb, line 48
      def mu_extended
        unless (defined? locked? and
                defined? lock and
                defined? unlock and
                defined? try_lock and
                defined? synchronize)
          Mutex_m.define_aliases(class<<self;self;end)
        end
        mu_initialize
      end

mu_lock()

[Source]

    # File mutex_m.rb, line 84
      def mu_lock
        while (Thread.critical = true; @mu_locked)
          @mu_waiting.push Thread.current
          Thread.stop
        end
        @mu_locked = true
        Thread.critical = false
        self
      end

mu_locked?()

[Source]

    # File mutex_m.rb, line 69
      def mu_locked?
        @mu_locked
      end

mu_synchronize() {|| ...}

locking

[Source]

    # File mutex_m.rb, line 60
      def mu_synchronize
        begin
          mu_lock
          yield
        ensure
          mu_unlock
        end
      end

mu_try_lock()

[Source]

    # File mutex_m.rb, line 73
      def mu_try_lock
        result = false
        Thread.critical = true
        unless @mu_locked
          @mu_locked = true
          result = true
        end
        Thread.critical = false
        result
      end

mu_unlock()

[Source]

    # File mutex_m.rb, line 94
      def mu_unlock
        return unless @mu_locked
        Thread.critical = true
        wait = @mu_waiting
        @mu_waiting = []
        @mu_locked = false
        Thread.critical = false
        for w in wait
          w.run
        end
        self
      end

[Validate]