Skip to content
This repository
Newer
Older
100644 92 lines (78 sloc) 1.491 kb
511dc44a »
2008-02-25 initial import
1 #
467bc1b0 »
2010-05-18 Update library and removing working tags
2 # mutex_m.rb -
511dc44a »
2008-02-25 initial import
3 # $Release Version: 3.0$
4 # $Revision: 1.7 $
5 # Original from mutex.rb
6 # by Keiju ISHITSUKA(keiju@ishitsuka.com)
7 # modified by matz
8 # patched by akira yamada
9 #
10 # --
11 # Usage:
12 # require "mutex_m.rb"
13 # obj = Object.new
14 # obj.extend Mutex_m
15 # ...
16 # extended object can be handled like Mutex
17 # or
18 # class Foo
19 # include Mutex_m
20 # ...
21 # end
22 # obj = Foo.new
23 # this obj can be handled like Mutex
24 #
25
26 require 'thread'
27
28 module Mutex_m
29 def Mutex_m.define_aliases(cl)
30 cl.module_eval %q{
31 alias locked? mu_locked?
32 alias lock mu_lock
33 alias unlock mu_unlock
34 alias try_lock mu_try_lock
35 alias synchronize mu_synchronize
36 }
467bc1b0 »
2010-05-18 Update library and removing working tags
37 end
511dc44a »
2008-02-25 initial import
38
39 def Mutex_m.append_features(cl)
40 super
41 define_aliases(cl) unless cl.instance_of?(Module)
42 end
467bc1b0 »
2010-05-18 Update library and removing working tags
43
511dc44a »
2008-02-25 initial import
44 def Mutex_m.extend_object(obj)
45 super
46 obj.mu_extended
47 end
48
49 def mu_extended
50 unless (defined? locked? and
51 defined? lock and
52 defined? unlock and
53 defined? try_lock and
54 defined? synchronize)
55 Mutex_m.define_aliases(class<<self;self;end)
56 end
57 mu_initialize
58 end
467bc1b0 »
2010-05-18 Update library and removing working tags
59
60 # locking
511dc44a »
2008-02-25 initial import
61 def mu_synchronize(&block)
62 @_mutex.synchronize(&block)
63 end
467bc1b0 »
2010-05-18 Update library and removing working tags
64
511dc44a »
2008-02-25 initial import
65 def mu_locked?
66 @_mutex.locked?
67 end
467bc1b0 »
2010-05-18 Update library and removing working tags
68
511dc44a »
2008-02-25 initial import
69 def mu_try_lock
70 @_mutex.try_lock
71 end
467bc1b0 »
2010-05-18 Update library and removing working tags
72
511dc44a »
2008-02-25 initial import
73 def mu_lock
74 @_mutex.lock
75 end
467bc1b0 »
2010-05-18 Update library and removing working tags
76
511dc44a »
2008-02-25 initial import
77 def mu_unlock
78 @_mutex.unlock
79 end
467bc1b0 »
2010-05-18 Update library and removing working tags
80
511dc44a »
2008-02-25 initial import
81 private
467bc1b0 »
2010-05-18 Update library and removing working tags
82
511dc44a »
2008-02-25 initial import
83 def mu_initialize
84 @_mutex = Mutex.new
85 end
86
87 def initialize(*args)
88 mu_initialize
89 super
90 end
91 end
Something went wrong with that request. Please try again.