concurrent-ruby 1.1.5 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (151) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +84 -1
  3. data/Gemfile +5 -10
  4. data/{LICENSE.md → LICENSE.txt} +18 -20
  5. data/README.md +55 -31
  6. data/Rakefile +84 -92
  7. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaAtomicFixnumLibrary.java +0 -0
  8. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaSemaphoreLibrary.java +52 -22
  9. data/ext/concurrent-ruby/com/concurrent_ruby/ext/SynchronizationLibrary.java +10 -25
  10. data/lib/{concurrent → concurrent-ruby/concurrent}/agent.rb +2 -1
  11. data/lib/{concurrent → concurrent-ruby/concurrent}/array.rb +6 -16
  12. data/lib/{concurrent → concurrent-ruby/concurrent}/async.rb +10 -20
  13. data/lib/{concurrent → concurrent-ruby/concurrent}/atom.rb +2 -2
  14. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/atomic_boolean.rb +7 -6
  15. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/atomic_fixnum.rb +5 -4
  16. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/atomic_markable_reference.rb +3 -0
  17. data/lib/concurrent-ruby/concurrent/atomic/atomic_reference.rb +135 -0
  18. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/cyclic_barrier.rb +1 -1
  19. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/event.rb +3 -3
  20. data/lib/concurrent-ruby/concurrent/atomic/fiber_local_var.rb +109 -0
  21. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/java_count_down_latch.rb +1 -0
  22. data/lib/concurrent-ruby/concurrent/atomic/locals.rb +189 -0
  23. data/lib/concurrent-ruby/concurrent/atomic/lock_local_var.rb +28 -0
  24. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_atomic_boolean.rb +11 -5
  25. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_atomic_fixnum.rb +11 -5
  26. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_count_down_latch.rb +1 -1
  27. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_semaphore.rb +19 -3
  28. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/read_write_lock.rb +2 -1
  29. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/reentrant_read_write_lock.rb +9 -9
  30. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/semaphore.rb +32 -14
  31. data/lib/concurrent-ruby/concurrent/atomic/thread_local_var.rb +111 -0
  32. data/lib/concurrent-ruby/concurrent/atomic_reference/atomic_direct_update.rb +37 -0
  33. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic_reference/mutex_atomic.rb +15 -4
  34. data/lib/{concurrent → concurrent-ruby/concurrent}/collection/copy_on_notify_observer_set.rb +1 -1
  35. data/lib/{concurrent → concurrent-ruby/concurrent}/collection/copy_on_write_observer_set.rb +1 -1
  36. data/lib/{concurrent → concurrent-ruby/concurrent}/collection/lock_free_stack.rb +2 -0
  37. data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/mri_map_backend.rb +3 -3
  38. data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/non_concurrent_map_backend.rb +16 -8
  39. data/lib/concurrent-ruby/concurrent/collection/map/truffleruby_map_backend.rb +14 -0
  40. data/lib/{concurrent → concurrent-ruby/concurrent}/collection/ruby_non_concurrent_priority_queue.rb +11 -1
  41. data/lib/{concurrent → concurrent-ruby/concurrent}/concern/dereferenceable.rb +2 -2
  42. data/lib/concurrent-ruby/concurrent/concern/logging.rb +116 -0
  43. data/lib/concurrent-ruby/concurrent/concurrent_ruby.jar +0 -0
  44. data/lib/concurrent-ruby/concurrent/configuration.rb +105 -0
  45. data/lib/{concurrent → concurrent-ruby/concurrent}/delay.rb +2 -2
  46. data/lib/{concurrent → concurrent-ruby/concurrent}/errors.rb +5 -0
  47. data/lib/{concurrent → concurrent-ruby/concurrent}/exchanger.rb +1 -0
  48. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/abstract_executor_service.rb +34 -37
  49. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/cached_thread_pool.rb +4 -4
  50. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/executor_service.rb +2 -2
  51. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/fixed_thread_pool.rb +29 -15
  52. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/java_executor_service.rb +21 -9
  53. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/java_single_thread_executor.rb +4 -3
  54. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/java_thread_pool_executor.rb +19 -2
  55. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/ruby_executor_service.rb +10 -6
  56. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/ruby_single_thread_executor.rb +0 -1
  57. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/ruby_thread_pool_executor.rb +46 -42
  58. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/safe_task_executor.rb +6 -6
  59. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/serialized_execution.rb +1 -1
  60. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/simple_executor_service.rb +5 -2
  61. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/single_thread_executor.rb +1 -0
  62. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/thread_pool_executor.rb +2 -1
  63. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/timer_set.rb +0 -1
  64. data/lib/{concurrent → concurrent-ruby/concurrent}/hash.rb +1 -10
  65. data/lib/{concurrent → concurrent-ruby/concurrent}/immutable_struct.rb +10 -2
  66. data/lib/{concurrent → concurrent-ruby/concurrent}/ivar.rb +2 -1
  67. data/lib/{concurrent → concurrent-ruby/concurrent}/map.rb +44 -31
  68. data/lib/{concurrent → concurrent-ruby/concurrent}/maybe.rb +1 -1
  69. data/lib/{concurrent → concurrent-ruby/concurrent}/mutable_struct.rb +13 -3
  70. data/lib/{concurrent → concurrent-ruby/concurrent}/mvar.rb +1 -1
  71. data/lib/{concurrent → concurrent-ruby/concurrent}/promise.rb +2 -1
  72. data/lib/{concurrent → concurrent-ruby/concurrent}/promises.rb +7 -6
  73. data/lib/{concurrent → concurrent-ruby/concurrent}/re_include.rb +2 -0
  74. data/lib/{concurrent → concurrent-ruby/concurrent}/scheduled_task.rb +30 -17
  75. data/lib/{concurrent → concurrent-ruby/concurrent}/set.rb +17 -19
  76. data/lib/{concurrent → concurrent-ruby/concurrent}/settable_struct.rb +13 -3
  77. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/abstract_lockable_object.rb +5 -1
  78. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/abstract_object.rb +1 -3
  79. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/abstract_struct.rb +11 -0
  80. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/condition.rb +2 -0
  81. data/lib/concurrent-ruby/concurrent/synchronization/full_memory_barrier.rb +29 -0
  82. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/jruby_lockable_object.rb +3 -1
  83. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/lock.rb +2 -0
  84. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/lockable_object.rb +8 -7
  85. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/mutex_lockable_object.rb +18 -5
  86. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/object.rb +12 -44
  87. data/lib/concurrent-ruby/concurrent/synchronization/safe_initialization.rb +36 -0
  88. data/lib/concurrent-ruby/concurrent/synchronization/volatile.rb +101 -0
  89. data/lib/concurrent-ruby/concurrent/synchronization.rb +13 -0
  90. data/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb +47 -0
  91. data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/cheap_lockable.rb +2 -39
  92. data/lib/concurrent-ruby/concurrent/thread_safe/util/data_structures.rb +52 -0
  93. data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/striped64.rb +1 -1
  94. data/lib/{concurrent → concurrent-ruby/concurrent}/timer_task.rb +11 -34
  95. data/lib/{concurrent → concurrent-ruby/concurrent}/tuple.rb +1 -5
  96. data/lib/{concurrent → concurrent-ruby/concurrent}/tvar.rb +21 -57
  97. data/lib/{concurrent → concurrent-ruby/concurrent}/utility/engine.rb +5 -16
  98. data/lib/concurrent-ruby/concurrent/utility/monotonic_time.rb +19 -0
  99. data/lib/{concurrent → concurrent-ruby/concurrent}/utility/native_extension_loader.rb +8 -10
  100. data/lib/{concurrent → concurrent-ruby/concurrent}/utility/native_integer.rb +1 -0
  101. data/lib/concurrent-ruby/concurrent/utility/processor_counter.rb +110 -0
  102. data/lib/concurrent-ruby/concurrent/version.rb +3 -0
  103. data/lib/concurrent-ruby/concurrent-ruby.rb +5 -0
  104. metadata +127 -129
  105. data/lib/concurrent/atomic/abstract_thread_local_var.rb +0 -66
  106. data/lib/concurrent/atomic/atomic_reference.rb +0 -204
  107. data/lib/concurrent/atomic/java_thread_local_var.rb +0 -37
  108. data/lib/concurrent/atomic/ruby_thread_local_var.rb +0 -161
  109. data/lib/concurrent/atomic/thread_local_var.rb +0 -104
  110. data/lib/concurrent/concern/logging.rb +0 -32
  111. data/lib/concurrent/concurrent_ruby.jar +0 -0
  112. data/lib/concurrent/configuration.rb +0 -184
  113. data/lib/concurrent/synchronization/jruby_object.rb +0 -45
  114. data/lib/concurrent/synchronization/mri_object.rb +0 -44
  115. data/lib/concurrent/synchronization/rbx_lockable_object.rb +0 -65
  116. data/lib/concurrent/synchronization/rbx_object.rb +0 -49
  117. data/lib/concurrent/synchronization/truffleruby_object.rb +0 -47
  118. data/lib/concurrent/synchronization/volatile.rb +0 -36
  119. data/lib/concurrent/synchronization.rb +0 -30
  120. data/lib/concurrent/thread_safe/synchronized_delegator.rb +0 -50
  121. data/lib/concurrent/thread_safe/util/data_structures.rb +0 -63
  122. data/lib/concurrent/utility/at_exit.rb +0 -97
  123. data/lib/concurrent/utility/monotonic_time.rb +0 -58
  124. data/lib/concurrent/utility/processor_counter.rb +0 -158
  125. data/lib/concurrent/version.rb +0 -3
  126. data/lib/concurrent-ruby.rb +0 -1
  127. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/count_down_latch.rb +1 -1
  128. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic_reference/numeric_cas_wrapper.rb +0 -0
  129. data/lib/{concurrent → concurrent-ruby/concurrent}/atomics.rb +0 -0
  130. data/lib/{concurrent → concurrent-ruby/concurrent}/collection/java_non_concurrent_priority_queue.rb +0 -0
  131. data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/atomic_reference_map_backend.rb +0 -0
  132. data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/synchronized_map_backend.rb +0 -0
  133. data/lib/{concurrent → concurrent-ruby/concurrent}/collection/non_concurrent_priority_queue.rb +1 -1
  134. /data/lib/{concurrent → concurrent-ruby/concurrent}/concern/deprecation.rb +0 -0
  135. /data/lib/{concurrent → concurrent-ruby/concurrent}/concern/obligation.rb +0 -0
  136. /data/lib/{concurrent → concurrent-ruby/concurrent}/concern/observable.rb +0 -0
  137. /data/lib/{concurrent → concurrent-ruby/concurrent}/constants.rb +0 -0
  138. /data/lib/{concurrent → concurrent-ruby/concurrent}/dataflow.rb +0 -0
  139. /data/lib/{concurrent → concurrent-ruby/concurrent}/executor/immediate_executor.rb +0 -0
  140. /data/lib/{concurrent → concurrent-ruby/concurrent}/executor/indirect_immediate_executor.rb +0 -0
  141. /data/lib/{concurrent → concurrent-ruby/concurrent}/executor/serial_executor_service.rb +0 -0
  142. /data/lib/{concurrent → concurrent-ruby/concurrent}/executor/serialized_execution_delegator.rb +0 -0
  143. /data/lib/{concurrent → concurrent-ruby/concurrent}/executors.rb +0 -0
  144. /data/lib/{concurrent → concurrent-ruby/concurrent}/future.rb +0 -0
  145. /data/lib/{concurrent → concurrent-ruby/concurrent}/options.rb +0 -0
  146. /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/adder.rb +0 -0
  147. /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/power_of_two_tuple.rb +0 -0
  148. /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/volatile.rb +0 -0
  149. /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/xor_shift_random.rb +0 -0
  150. /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util.rb +0 -0
  151. /data/lib/{concurrent.rb → concurrent-ruby/concurrent.rb} +0 -0
@@ -57,30 +57,43 @@ module Concurrent
57
57
  #
58
58
  # @example Basic usage
59
59
  #
60
- # require 'concurrent'
61
- # require 'thread' # for Queue
62
- # require 'open-uri' # for open(uri)
60
+ # require 'concurrent/scheduled_task'
61
+ # require 'csv'
62
+ # require 'open-uri'
63
63
  #
64
64
  # class Ticker
65
- # def get_year_end_closing(symbol, year)
66
- # uri = "http://ichart.finance.yahoo.com/table.csv?s=#{symbol}&a=11&b=01&c=#{year}&d=11&e=31&f=#{year}&g=m"
67
- # data = open(uri) {|f| f.collect{|line| line.strip } }
68
- # data[1].split(',')[4].to_f
69
- # end
65
+ # def get_year_end_closing(symbol, year, api_key)
66
+ # uri = "https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=#{symbol}&apikey=#{api_key}&datatype=csv"
67
+ # data = []
68
+ # csv = URI.parse(uri).read
69
+ # if csv.include?('call frequency')
70
+ # return :rate_limit_exceeded
71
+ # end
72
+ # CSV.parse(csv, headers: true) do |row|
73
+ # data << row['close'].to_f if row['timestamp'].include?(year.to_s)
74
+ # end
75
+ # year_end = data.first
76
+ # year_end
77
+ # rescue => e
78
+ # p e
79
+ # end
70
80
  # end
71
81
  #
82
+ # api_key = ENV['ALPHAVANTAGE_KEY']
83
+ # abort(error_message) unless api_key
84
+ #
72
85
  # # Future
73
- # price = Concurrent::Future.execute{ Ticker.new.get_year_end_closing('TWTR', 2013) }
86
+ # price = Concurrent::Future.execute{ Ticker.new.get_year_end_closing('TWTR', 2013, api_key) }
74
87
  # price.state #=> :pending
75
- # sleep(1) # do other stuff
76
- # price.value #=> 63.65
77
- # price.state #=> :fulfilled
88
+ # price.pending? #=> true
89
+ # price.value(0) #=> nil (does not block)
78
90
  #
79
- # # ScheduledTask
80
- # task = Concurrent::ScheduledTask.execute(2){ Ticker.new.get_year_end_closing('INTC', 2013) }
81
- # task.state #=> :pending
82
- # sleep(3) # do other stuff
83
- # task.value #=> 25.96
91
+ # sleep(1) # do other stuff
92
+ #
93
+ # price.value #=> 63.65 (after blocking if necessary)
94
+ # price.state #=> :fulfilled
95
+ # price.fulfilled? #=> true
96
+ # price.value #=> 63.65
84
97
  #
85
98
  # @example Successful task execution
86
99
  #
@@ -11,21 +11,27 @@ module Concurrent
11
11
  # or writing at a time. This includes iteration methods like `#each`.
12
12
  #
13
13
  # @note `a += b` is **not** a **thread-safe** operation on
14
- # `Concurrent::Set`. It reads Set `a`, then it creates new `Concurrent::Set`
15
- # which is union of `a` and `b`, then it writes the union to `a`.
16
- # The read and write are independent operations they do not form a single atomic
17
- # operation therefore when two `+=` operations are executed concurrently updates
18
- # may be lost. Use `#merge` instead.
14
+ # `Concurrent::Set`. It reads Set `a`, then it creates new `Concurrent::Set`
15
+ # which is union of `a` and `b`, then it writes the union to `a`.
16
+ # The read and write are independent operations they do not form a single atomic
17
+ # operation therefore when two `+=` operations are executed concurrently updates
18
+ # may be lost. Use `#merge` instead.
19
19
  #
20
20
  # @see http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html Ruby standard library `Set`
21
21
 
22
-
23
22
  # @!macro internal_implementation_note
24
23
  SetImplementation = case
25
24
  when Concurrent.on_cruby?
26
- # Because MRI never runs code in parallel, the existing
27
- # non-thread-safe structures should usually work fine.
28
- ::Set
25
+ # The CRuby implementation of Set is written in Ruby itself and is
26
+ # not thread safe for certain methods.
27
+ require 'monitor'
28
+ require 'concurrent/thread_safe/util/data_structures'
29
+
30
+ class CRubySet < ::Set
31
+ end
32
+
33
+ ThreadSafe::Util.make_synchronized_on_cruby CRubySet
34
+ CRubySet
29
35
 
30
36
  when Concurrent.on_jruby?
31
37
  require 'jruby/synchronized'
@@ -33,16 +39,8 @@ module Concurrent
33
39
  class JRubySet < ::Set
34
40
  include JRuby::Synchronized
35
41
  end
36
- JRubySet
37
-
38
- when Concurrent.on_rbx?
39
- require 'monitor'
40
- require 'concurrent/thread_safe/util/data_structures'
41
42
 
42
- class RbxSet < ::Set
43
- end
44
- ThreadSafe::Util.make_synchronized_on_rbx Concurrent::RbxSet
45
- RbxSet
43
+ JRubySet
46
44
 
47
45
  when Concurrent.on_truffleruby?
48
46
  require 'concurrent/thread_safe/util/data_structures'
@@ -50,7 +48,7 @@ module Concurrent
50
48
  class TruffleRubySet < ::Set
51
49
  end
52
50
 
53
- ThreadSafe::Util.make_synchronized_on_truffleruby Concurrent::TruffleRubySet
51
+ ThreadSafe::Util.make_synchronized_on_truffleruby TruffleRubySet
54
52
  TruffleRubySet
55
53
 
56
54
  else
@@ -1,6 +1,6 @@
1
- require 'concurrent/synchronization/abstract_struct'
2
1
  require 'concurrent/errors'
3
- require 'concurrent/synchronization'
2
+ require 'concurrent/synchronization/abstract_struct'
3
+ require 'concurrent/synchronization/lockable_object'
4
4
 
5
5
  module Concurrent
6
6
 
@@ -9,7 +9,7 @@ module Concurrent
9
9
  # or any time thereafter. Attempting to assign a value to a member
10
10
  # that has already been set will result in a `Concurrent::ImmutabilityError`.
11
11
  #
12
- # @see http://ruby-doc.org/core-2.2.0/Struct.html Ruby standard library `Struct`
12
+ # @see http://ruby-doc.org/core/Struct.html Ruby standard library `Struct`
13
13
  # @see http://en.wikipedia.org/wiki/Final_(Java) Java `final` keyword
14
14
  module SettableStruct
15
15
  include Synchronization::AbstractStruct
@@ -91,6 +91,16 @@ module Concurrent
91
91
  raise NameError.new("no member '#{member}' in struct")
92
92
  end
93
93
 
94
+ private
95
+
96
+ # @!visibility private
97
+ def initialize_copy(original)
98
+ synchronize do
99
+ super(original)
100
+ ns_initialize_copy
101
+ end
102
+ end
103
+
94
104
  # @!macro struct_new
95
105
  def self.new(*args, &block)
96
106
  clazz_name = nil
@@ -1,3 +1,7 @@
1
+ require 'concurrent/utility/native_extension_loader' # load native parts first
2
+ require 'concurrent/utility/monotonic_time'
3
+ require 'concurrent/synchronization/object'
4
+
1
5
  module Concurrent
2
6
  module Synchronization
3
7
 
@@ -34,7 +38,7 @@ module Concurrent
34
38
  if timeout
35
39
  wait_until = Concurrent.monotonic_time + timeout
36
40
  loop do
37
- now = Concurrent.monotonic_time
41
+ now = Concurrent.monotonic_time
38
42
  condition_result = condition.call
39
43
  return condition_result if now >= wait_until || condition_result
40
44
  ns_wait wait_until - now
@@ -4,10 +4,8 @@ module Concurrent
4
4
  # @!visibility private
5
5
  # @!macro internal_implementation_note
6
6
  class AbstractObject
7
-
8
- # @abstract has to be implemented based on Ruby runtime
9
7
  def initialize
10
- raise NotImplementedError
8
+ # nothing to do
11
9
  end
12
10
 
13
11
  # @!visibility private
@@ -115,6 +115,17 @@ module Concurrent
115
115
  self.class.new(*self.to_h.merge(other, &block).values)
116
116
  end
117
117
 
118
+ # @!visibility private
119
+ def ns_initialize_copy
120
+ @values = @values.map do |val|
121
+ begin
122
+ val.clone
123
+ rescue TypeError
124
+ val
125
+ end
126
+ end
127
+ end
128
+
118
129
  # @!visibility private
119
130
  def pr_underscore(clazz)
120
131
  word = clazz.to_s.dup # dup string to workaround JRuby 9.2.0.0 bug https://github.com/jruby/jruby/issues/5229
@@ -1,3 +1,5 @@
1
+ require 'concurrent/synchronization/lockable_object'
2
+
1
3
  module Concurrent
2
4
  module Synchronization
3
5
 
@@ -0,0 +1,29 @@
1
+ require 'concurrent/utility/native_extension_loader' # load native parts first
2
+
3
+ module Concurrent
4
+ module Synchronization
5
+ case
6
+ when Concurrent.on_cruby?
7
+ def self.full_memory_barrier
8
+ # relying on undocumented behavior of CRuby, GVL acquire has lock which ensures visibility of ivars
9
+ # https://github.com/ruby/ruby/blob/ruby_2_2/thread_pthread.c#L204-L211
10
+ end
11
+
12
+ when Concurrent.on_jruby?
13
+ require 'concurrent/utility/native_extension_loader'
14
+ def self.full_memory_barrier
15
+ JRubyAttrVolatile.full_memory_barrier
16
+ end
17
+
18
+ when Concurrent.on_truffleruby?
19
+ def self.full_memory_barrier
20
+ TruffleRuby.full_memory_barrier
21
+ end
22
+
23
+ else
24
+ warn 'Possibly unsupported Ruby implementation'
25
+ def self.full_memory_barrier
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,7 +1,9 @@
1
+ require 'concurrent/utility/native_extension_loader' # load native parts first
2
+
1
3
  module Concurrent
2
4
  module Synchronization
3
5
 
4
- if Concurrent.on_jruby? && Concurrent.java_extensions_loaded?
6
+ if Concurrent.on_jruby?
5
7
 
6
8
  # @!visibility private
7
9
  # @!macro internal_implementation_note
@@ -1,3 +1,5 @@
1
+ require 'concurrent/synchronization/lockable_object'
2
+
1
3
  module Concurrent
2
4
  module Synchronization
3
5
 
@@ -1,17 +1,18 @@
1
+ require 'concurrent/utility/engine'
2
+ require 'concurrent/synchronization/abstract_lockable_object'
3
+ require 'concurrent/synchronization/mutex_lockable_object'
4
+ require 'concurrent/synchronization/jruby_lockable_object'
5
+
1
6
  module Concurrent
2
7
  module Synchronization
3
8
 
4
9
  # @!visibility private
5
10
  # @!macro internal_implementation_note
6
11
  LockableObjectImplementation = case
7
- when Concurrent.on_cruby? && Concurrent.ruby_version(:<=, 1, 9, 3)
8
- MonitorLockableObject
9
- when Concurrent.on_cruby? && Concurrent.ruby_version(:>, 1, 9, 3)
12
+ when Concurrent.on_cruby?
10
13
  MutexLockableObject
11
14
  when Concurrent.on_jruby?
12
15
  JRubyLockableObject
13
- when Concurrent.on_rbx?
14
- RbxLockableObject
15
16
  when Concurrent.on_truffleruby?
16
17
  MutexLockableObject
17
18
  else
@@ -26,8 +27,8 @@ module Concurrent
26
27
  # the classes using it. Use {Synchronization::Object} not this abstract class.
27
28
  #
28
29
  # @note this object does not support usage together with
29
- # [`Thread#wakeup`](http://ruby-doc.org/core-2.2.0/Thread.html#method-i-wakeup)
30
- # and [`Thread#raise`](http://ruby-doc.org/core-2.2.0/Thread.html#method-i-raise).
30
+ # [`Thread#wakeup`](http://ruby-doc.org/core/Thread.html#method-i-wakeup)
31
+ # and [`Thread#raise`](http://ruby-doc.org/core/Thread.html#method-i-raise).
31
32
  # `Thread#sleep` and `Thread#wakeup` will work as expected but mixing `Synchronization::Object#wait` and
32
33
  # `Thread#wakeup` will not work on all platforms.
33
34
  #
@@ -1,5 +1,6 @@
1
+ require 'concurrent/synchronization/abstract_lockable_object'
2
+
1
3
  module Concurrent
2
- # noinspection RubyInstanceVariableNamingConvention
3
4
  module Synchronization
4
5
 
5
6
  # @!visibility private
@@ -26,8 +27,14 @@ module Concurrent
26
27
 
27
28
  safe_initialization!
28
29
 
29
- def initialize(*defaults)
30
- super(*defaults)
30
+ def initialize
31
+ super()
32
+ @__Lock__ = ::Mutex.new
33
+ @__Condition__ = ::ConditionVariable.new
34
+ end
35
+
36
+ def initialize_copy(other)
37
+ super
31
38
  @__Lock__ = ::Mutex.new
32
39
  @__Condition__ = ::ConditionVariable.new
33
40
  end
@@ -55,8 +62,14 @@ module Concurrent
55
62
 
56
63
  safe_initialization!
57
64
 
58
- def initialize(*defaults)
59
- super(*defaults)
65
+ def initialize
66
+ super()
67
+ @__Lock__ = ::Monitor.new
68
+ @__Condition__ = @__Lock__.new_cond
69
+ end
70
+
71
+ def initialize_copy(other)
72
+ super
60
73
  @__Lock__ = ::Monitor.new
61
74
  @__Condition__ = @__Lock__.new_cond
62
75
  end
@@ -1,28 +1,20 @@
1
+ require 'concurrent/utility/native_extension_loader' # load native parts first
2
+
3
+ require 'concurrent/synchronization/safe_initialization'
4
+ require 'concurrent/synchronization/volatile'
5
+ require 'concurrent/atomic/atomic_reference'
6
+
1
7
  module Concurrent
2
8
  module Synchronization
3
9
 
4
- # @!visibility private
5
- # @!macro internal_implementation_note
6
- ObjectImplementation = case
7
- when Concurrent.on_cruby?
8
- MriObject
9
- when Concurrent.on_jruby?
10
- JRubyObject
11
- when Concurrent.on_rbx?
12
- RbxObject
13
- when Concurrent.on_truffleruby?
14
- TruffleRubyObject
15
- else
16
- warn 'Possibly unsupported Ruby implementation'
17
- MriObject
18
- end
19
- private_constant :ObjectImplementation
20
-
21
10
  # Abstract object providing final, volatile, ans CAS extensions to build other concurrent abstractions.
22
11
  # - final instance variables see {Object.safe_initialization!}
23
12
  # - volatile instance variables see {Object.attr_volatile}
24
13
  # - volatile instance variables see {Object.attr_atomic}
25
- class Object < ObjectImplementation
14
+ # @!visibility private
15
+ class Object < AbstractObject
16
+ include Volatile
17
+
26
18
  # TODO make it a module if possible
27
19
 
28
20
  # @!method self.attr_volatile(*names)
@@ -38,36 +30,12 @@ module Concurrent
38
30
  __initialize_atomic_fields__
39
31
  end
40
32
 
41
- # By calling this method on a class, it and all its children are marked to be constructed safely. Meaning that
42
- # all writes (ivar initializations) are made visible to all readers of newly constructed object. It ensures
43
- # same behaviour as Java's final fields.
44
- # @example
45
- # class AClass < Concurrent::Synchronization::Object
46
- # safe_initialization!
47
- #
48
- # def initialize
49
- # @AFinalValue = 'value' # published safely, does not have to be synchronized
50
- # end
51
- # end
52
- # @return [true]
53
33
  def self.safe_initialization!
54
- # define only once, and not again in children
55
- return if safe_initialization?
56
-
57
- # @!visibility private
58
- def self.new(*args, &block)
59
- object = super(*args, &block)
60
- ensure
61
- object.full_memory_barrier if object
62
- end
63
-
64
- @safe_initialization = true
34
+ extend SafeInitialization unless safe_initialization?
65
35
  end
66
36
 
67
- # @return [true, false] if this class is safely initialized.
68
37
  def self.safe_initialization?
69
- @safe_initialization = false unless defined? @safe_initialization
70
- @safe_initialization || (superclass.respond_to?(:safe_initialization?) && superclass.safe_initialization?)
38
+ self.singleton_class < SafeInitialization
71
39
  end
72
40
 
73
41
  # For testing purposes, quite slow. Injects assert code to new method which will raise if class instance contains
@@ -0,0 +1,36 @@
1
+ require 'concurrent/synchronization/full_memory_barrier'
2
+
3
+ module Concurrent
4
+ module Synchronization
5
+
6
+ # @!visibility private
7
+ # @!macro internal_implementation_note
8
+ #
9
+ # By extending this module, a class and all its children are marked to be constructed safely. Meaning that
10
+ # all writes (ivar initializations) are made visible to all readers of newly constructed object. It ensures
11
+ # same behaviour as Java's final fields.
12
+ #
13
+ # Due to using Kernel#extend, the module is not included again if already present in the ancestors,
14
+ # which avoids extra overhead.
15
+ #
16
+ # @example
17
+ # class AClass < Concurrent::Synchronization::Object
18
+ # extend Concurrent::Synchronization::SafeInitialization
19
+ #
20
+ # def initialize
21
+ # @AFinalValue = 'value' # published safely, #foo will never return nil
22
+ # end
23
+ #
24
+ # def foo
25
+ # @AFinalValue
26
+ # end
27
+ # end
28
+ module SafeInitialization
29
+ def new(*args, &block)
30
+ super(*args, &block)
31
+ ensure
32
+ Concurrent::Synchronization.full_memory_barrier
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,101 @@
1
+ require 'concurrent/utility/native_extension_loader' # load native parts first
2
+ require 'concurrent/utility/engine'
3
+ require 'concurrent/synchronization/full_memory_barrier'
4
+
5
+ module Concurrent
6
+ module Synchronization
7
+
8
+ # Volatile adds the attr_volatile class method when included.
9
+ #
10
+ # @example
11
+ # class Foo
12
+ # include Concurrent::Synchronization::Volatile
13
+ #
14
+ # attr_volatile :bar
15
+ #
16
+ # def initialize
17
+ # self.bar = 1
18
+ # end
19
+ # end
20
+ #
21
+ # foo = Foo.new
22
+ # foo.bar
23
+ # => 1
24
+ # foo.bar = 2
25
+ # => 2
26
+ #
27
+ # @!visibility private
28
+ module Volatile
29
+ def self.included(base)
30
+ base.extend(ClassMethods)
31
+ end
32
+
33
+ def full_memory_barrier
34
+ Synchronization.full_memory_barrier
35
+ end
36
+
37
+ module ClassMethods
38
+ if Concurrent.on_cruby?
39
+ def attr_volatile(*names)
40
+ names.each do |name|
41
+ ivar = :"@volatile_#{name}"
42
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
43
+ def #{name}
44
+ #{ivar}
45
+ end
46
+
47
+ def #{name}=(value)
48
+ #{ivar} = value
49
+ end
50
+ RUBY
51
+ end
52
+ names.map { |n| [n, :"#{n}="] }.flatten
53
+ end
54
+
55
+ elsif Concurrent.on_jruby?
56
+ def attr_volatile(*names)
57
+ names.each do |name|
58
+ ivar = :"@volatile_#{name}"
59
+
60
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
61
+ def #{name}
62
+ ::Concurrent::Synchronization::JRubyAttrVolatile.instance_variable_get_volatile(self, :#{ivar})
63
+ end
64
+
65
+ def #{name}=(value)
66
+ ::Concurrent::Synchronization::JRubyAttrVolatile.instance_variable_set_volatile(self, :#{ivar}, value)
67
+ end
68
+ RUBY
69
+
70
+ end
71
+ names.map { |n| [n, :"#{n}="] }.flatten
72
+ end
73
+
74
+ else
75
+ warn 'Possibly unsupported Ruby implementation' unless Concurrent.on_truffleruby?
76
+
77
+ def attr_volatile(*names)
78
+ names.each do |name|
79
+ ivar = :"@volatile_#{name}"
80
+
81
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
82
+ def #{name}
83
+ ::Concurrent::Synchronization.full_memory_barrier
84
+ #{ivar}
85
+ end
86
+
87
+ def #{name}=(value)
88
+ #{ivar} = value
89
+ ::Concurrent::Synchronization.full_memory_barrier
90
+ end
91
+ RUBY
92
+ end
93
+
94
+ names.map { |n| [n, :"#{n}="] }.flatten
95
+ end
96
+ end
97
+ end
98
+
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,13 @@
1
+ require 'concurrent/utility/native_extension_loader' # load native parts first
2
+
3
+ require 'concurrent/synchronization/object'
4
+ require 'concurrent/synchronization/lockable_object'
5
+ require 'concurrent/synchronization/condition'
6
+ require 'concurrent/synchronization/lock'
7
+
8
+ module Concurrent
9
+ # @!visibility private
10
+ module Synchronization
11
+ end
12
+ end
13
+
@@ -0,0 +1,47 @@
1
+ require 'delegate'
2
+ require 'monitor'
3
+
4
+ module Concurrent
5
+ # This class provides a trivial way to synchronize all calls to a given object
6
+ # by wrapping it with a `Delegator` that performs `Monitor#enter/exit` calls
7
+ # around the delegated `#send`. Example:
8
+ #
9
+ # array = [] # not thread-safe on many impls
10
+ # array = SynchronizedDelegator.new([]) # thread-safe
11
+ #
12
+ # A simple `Monitor` provides a very coarse-grained way to synchronize a given
13
+ # object, in that it will cause synchronization for methods that have no need
14
+ # for it, but this is a trivial way to get thread-safety where none may exist
15
+ # currently on some implementations.
16
+ #
17
+ # This class is currently being considered for inclusion into stdlib, via
18
+ # https://bugs.ruby-lang.org/issues/8556
19
+ #
20
+ # @!visibility private
21
+ class SynchronizedDelegator < SimpleDelegator
22
+ def setup
23
+ @old_abort = Thread.abort_on_exception
24
+ Thread.abort_on_exception = true
25
+ end
26
+
27
+ def teardown
28
+ Thread.abort_on_exception = @old_abort
29
+ end
30
+
31
+ def initialize(obj)
32
+ __setobj__(obj)
33
+ @monitor = Monitor.new
34
+ end
35
+
36
+ def method_missing(method, *args, &block)
37
+ monitor = @monitor
38
+ begin
39
+ monitor.enter
40
+ super
41
+ ensure
42
+ monitor.exit
43
+ end
44
+ end
45
+
46
+ end
47
+ end
@@ -1,5 +1,6 @@
1
1
  require 'concurrent/thread_safe/util'
2
2
  require 'concurrent/thread_safe/util/volatile'
3
+ require 'concurrent/utility/engine'
3
4
 
4
5
  module Concurrent
5
6
 
@@ -32,45 +33,7 @@ module Concurrent
32
33
  # @!visibility private
33
34
  module CheapLockable
34
35
  private
35
- engine = defined?(RUBY_ENGINE) && RUBY_ENGINE
36
- if engine == 'rbx'
37
- # Making use of the Rubinius' ability to lock via object headers to avoid the overhead of the extra Mutex objects.
38
- def cheap_synchronize
39
- Rubinius.lock(self)
40
- begin
41
- yield
42
- ensure
43
- Rubinius.unlock(self)
44
- end
45
- end
46
-
47
- def cheap_wait
48
- wchan = Rubinius::Channel.new
49
-
50
- begin
51
- waiters = @waiters ||= []
52
- waiters.push wchan
53
- Rubinius.unlock(self)
54
- signaled = wchan.receive_timeout nil
55
- ensure
56
- Rubinius.lock(self)
57
-
58
- unless signaled or waiters.delete(wchan)
59
- # we timed out, but got signaled afterwards (e.g. while waiting to
60
- # acquire @lock), so pass that signal on to the next waiter
61
- waiters.shift << true unless waiters.empty?
62
- end
63
- end
64
-
65
- self
66
- end
67
-
68
- def cheap_broadcast
69
- waiters = @waiters ||= []
70
- waiters.shift << true until waiters.empty?
71
- self
72
- end
73
- elsif engine == 'jruby'
36
+ if Concurrent.on_jruby?
74
37
  # Use Java's native synchronized (this) { wait(); notifyAll(); } to avoid the overhead of the extra Mutex objects
75
38
  require 'jruby'
76
39