Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 92 lines (78 sloc) 1.491 kb
511dc44 initial import
Laurent Sansonetti authored
1 #
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
2 # mutex_m.rb -
511dc44 initial import
Laurent Sansonetti authored
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 }
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
37 end
511dc44 initial import
Laurent Sansonetti authored
38
39 def Mutex_m.append_features(cl)
40 super
41 define_aliases(cl) unless cl.instance_of?(Module)
42 end
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
43
511dc44 initial import
Laurent Sansonetti authored
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
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
59
60 # locking
511dc44 initial import
Laurent Sansonetti authored
61 def mu_synchronize(&block)
62 @_mutex.synchronize(&block)
63 end
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
64
511dc44 initial import
Laurent Sansonetti authored
65 def mu_locked?
66 @_mutex.locked?
67 end
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
68
511dc44 initial import
Laurent Sansonetti authored
69 def mu_try_lock
70 @_mutex.try_lock
71 end
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
72
511dc44 initial import
Laurent Sansonetti authored
73 def mu_lock
74 @_mutex.lock
75 end
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
76
511dc44 initial import
Laurent Sansonetti authored
77 def mu_unlock
78 @_mutex.unlock
79 end
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
80
511dc44 initial import
Laurent Sansonetti authored
81 private
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
82
511dc44 initial import
Laurent Sansonetti authored
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.