concurrent-ruby 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,81 +0,0 @@
1
- require 'concurrent/thread_safe/util'
2
- require 'concurrent/thread_safe/util/volatile'
3
- require 'concurrent/utility/engine'
4
-
5
- module Concurrent
6
-
7
- # @!visibility private
8
- module ThreadSafe
9
-
10
- # @!visibility private
11
- module Util
12
-
13
- # Provides a cheapest possible (mainly in terms of memory usage) +Mutex+
14
- # with the +ConditionVariable+ bundled in.
15
- #
16
- # Usage:
17
- # class A
18
- # include CheapLockable
19
- #
20
- # def do_exlusively
21
- # cheap_synchronize { yield }
22
- # end
23
- #
24
- # def wait_for_something
25
- # cheap_synchronize do
26
- # cheap_wait until resource_available?
27
- # do_something
28
- # cheap_broadcast # wake up others
29
- # end
30
- # end
31
- # end
32
- #
33
- # @!visibility private
34
- module CheapLockable
35
- private
36
- if Concurrent.on_jruby?
37
- # Use Java's native synchronized (this) { wait(); notifyAll(); } to avoid the overhead of the extra Mutex objects
38
- require 'jruby'
39
-
40
- def cheap_synchronize
41
- JRuby.reference0(self).synchronized { yield }
42
- end
43
-
44
- def cheap_wait
45
- JRuby.reference0(self).wait
46
- end
47
-
48
- def cheap_broadcast
49
- JRuby.reference0(self).notify_all
50
- end
51
- else
52
- require 'thread'
53
-
54
- extend Volatile
55
- attr_volatile :mutex
56
-
57
- # Non-reentrant Mutex#syncrhonize
58
- def cheap_synchronize
59
- true until (my_mutex = mutex) || cas_mutex(nil, my_mutex = Mutex.new)
60
- my_mutex.synchronize { yield }
61
- end
62
-
63
- # Releases this object's +cheap_synchronize+ lock and goes to sleep waiting for other threads to +cheap_broadcast+, reacquires the lock on wakeup.
64
- # Must only be called in +cheap_broadcast+'s block.
65
- def cheap_wait
66
- conditional_variable = @conditional_variable ||= ConditionVariable.new
67
- conditional_variable.wait(mutex)
68
- end
69
-
70
- # Wakes up all threads waiting for this object's +cheap_synchronize+ lock.
71
- # Must only be called in +cheap_broadcast+'s block.
72
- def cheap_broadcast
73
- if conditional_variable = @conditional_variable
74
- conditional_variable.broadcast
75
- end
76
- end
77
- end
78
- end
79
- end
80
- end
81
- end