Line | |
---|
1 | #--
|
---|
2 | # mutex_m.rb -
|
---|
3 | # $Release Version: 3.0$
|
---|
4 | # $Revision: 1.7 $
|
---|
5 | # $Date: 1998/02/27 04:28:57 $
|
---|
6 | # Original from mutex.rb
|
---|
7 | # by Keiju ISHITSUKA([email protected])
|
---|
8 | # modified by matz
|
---|
9 | # patched by akira yamada
|
---|
10 | #++
|
---|
11 | #
|
---|
12 | # == Usage
|
---|
13 | #
|
---|
14 | # Extend an object and use it like a Mutex object:
|
---|
15 | #
|
---|
16 | # require "mutex_m.rb"
|
---|
17 | # obj = Object.new
|
---|
18 | # obj.extend Mutex_m
|
---|
19 | # # ...
|
---|
20 | #
|
---|
21 | # Or, include Mutex_m in a class to have its instances behave like a Mutex
|
---|
22 | # object:
|
---|
23 | #
|
---|
24 | # class Foo
|
---|
25 | # include Mutex_m
|
---|
26 | # # ...
|
---|
27 | # end
|
---|
28 | #
|
---|
29 | # obj = Foo.new
|
---|
30 |
|
---|
31 | module Mutex_m
|
---|
32 | def Mutex_m.define_aliases(cl)
|
---|
33 | cl.module_eval %q{
|
---|
34 | alias locked? mu_locked?
|
---|
35 | alias lock mu_lock
|
---|
36 | alias unlock mu_unlock
|
---|
37 | alias try_lock mu_try_lock
|
---|
38 | alias synchronize mu_synchronize
|
---|
39 | }
|
---|
40 | end
|
---|
41 |
|
---|
42 | def Mutex_m.append_features(cl)
|
---|
43 | super
|
---|
44 | define_aliases(cl) unless cl.instance_of?(Module)
|
---|
45 | end
|
---|
46 |
|
---|
47 | def Mutex_m.extend_object(obj)
|
---|
48 | super
|
---|
49 | obj.mu_extended
|
---|
50 | end
|
---|
51 |
|
---|
52 | def mu_extended
|
---|
53 | unless (defined? locked? and
|
---|
54 | defined? lock and
|
---|
55 | defined? unlock and
|
---|
56 | defined? try_lock and
|
---|
57 | defined? synchronize)
|
---|
58 | Mutex_m.define_aliases(class<<self;self;end)
|
---|
59 | end
|
---|
60 | mu_initialize
|
---|
61 | end
|
---|
62 |
|
---|
63 | # locking
|
---|
64 | def mu_synchronize
|
---|
65 | begin
|
---|
66 | mu_lock
|
---|
67 | yield
|
---|
68 | ensure
|
---|
69 | mu_unlock
|
---|
70 | end
|
---|
71 | end
|
---|
72 |
|
---|
73 | def mu_locked?
|
---|
74 | @mu_locked
|
---|
75 | end
|
---|
76 |
|
---|
77 | def mu_try_lock
|
---|
78 | result = false
|
---|
79 | Thread.critical = true
|
---|
80 | unless @mu_locked
|
---|
81 | @mu_locked = true
|
---|
82 | result = true
|
---|
83 | end
|
---|
84 | Thread.critical = false
|
---|
85 | result
|
---|
86 | end
|
---|
87 |
|
---|
88 | def mu_lock
|
---|
89 | while (Thread.critical = true; @mu_locked)
|
---|
90 | @mu_waiting.push Thread.current
|
---|
91 | Thread.stop
|
---|
92 | end
|
---|
93 | @mu_locked = true
|
---|
94 | Thread.critical = false
|
---|
95 | self
|
---|
96 | end
|
---|
97 |
|
---|
98 | def mu_unlock
|
---|
99 | return unless @mu_locked
|
---|
100 | Thread.critical = true
|
---|
101 | wait = @mu_waiting
|
---|
102 | @mu_waiting = []
|
---|
103 | @mu_locked = false
|
---|
104 | Thread.critical = false
|
---|
105 | for w in wait
|
---|
106 | w.run
|
---|
107 | end
|
---|
108 | self
|
---|
109 | end
|
---|
110 |
|
---|
111 | private
|
---|
112 |
|
---|
113 | def mu_initialize
|
---|
114 | @mu_waiting = []
|
---|
115 | @mu_locked = false;
|
---|
116 | end
|
---|
117 |
|
---|
118 | def initialize(*args)
|
---|
119 | mu_initialize
|
---|
120 | super
|
---|
121 | end
|
---|
122 | end
|
---|
Note:
See
TracBrowser
for help on using the repository browser.