~ubuntu-branches/ubuntu/intrepid/ruby1.9/intrepid-updates

« back to all changes in this revision

Viewing changes to lib/mutex_m.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-05-21 14:00:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070521140019-ui4zd0v80duktssk
Tags: 1.9.0+20070521-1
new upstream snapshot. (2006-07-21)  (Closes: #414856, #388344)

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#       this obj can be handled like Mutex
25
25
#
26
26
 
 
27
require 'thread'
 
28
 
27
29
module Mutex_m
28
30
  def Mutex_m.define_aliases(cl)
29
31
    cl.module_eval %q{
57
59
  end
58
60
  
59
61
  # locking 
60
 
  def mu_synchronize
61
 
    begin
62
 
      mu_lock
63
 
      yield
64
 
    ensure
65
 
      mu_unlock
66
 
    end
 
62
  def mu_synchronize(&block)
 
63
    @_mutex.synchronize(&block)
67
64
  end
68
65
  
69
66
  def mu_locked?
70
 
    @mu_locked
 
67
    @_mutex.locked?
71
68
  end
72
69
  
73
70
  def mu_try_lock
74
 
    result = false
75
 
    Thread.critical = true
76
 
    unless @mu_locked
77
 
      @mu_locked = true
78
 
      result = true
79
 
    end
80
 
    Thread.critical = false
81
 
    result
 
71
    @_mutex.try_lock
82
72
  end
83
73
  
84
74
  def mu_lock
85
 
    while (Thread.critical = true; @mu_locked)
86
 
      @mu_waiting.push Thread.current
87
 
      Thread.stop
88
 
    end
89
 
    @mu_locked = true
90
 
    Thread.critical = false
91
 
    self
 
75
    @_mutex.lock
92
76
  end
93
77
  
94
78
  def mu_unlock
95
 
    return unless @mu_locked
96
 
    Thread.critical = true
97
 
    wait = @mu_waiting
98
 
    @mu_waiting = []
99
 
    @mu_locked = false
100
 
    Thread.critical = false
101
 
    for w in wait
102
 
      w.run
103
 
    end
104
 
    self
 
79
    @_mutex.unlock
105
80
  end
106
81
  
107
82
  private
108
83
  
109
84
  def mu_initialize
110
 
    @mu_waiting = []
111
 
    @mu_locked = false;
 
85
    @_mutex = Mutex.new
112
86
  end
113
87
 
114
88
  def initialize(*args)