Skip to content

Commit 3894d5b

Browse files
author
Petr Chalupa
authored
Merge pull request #579 from matthewd/unified-integer
Minimal changes to avoid Fixnum deprecation warnings
2 parents 6e75e4b + f9890a7 commit 3894d5b

File tree

8 files changed

+84
-43
lines changed

8 files changed

+84
-43
lines changed

lib/concurrent/atomic/cyclic_barrier.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'concurrent/synchronization'
2+
require 'concurrent/utility/native_integer'
23

34
module Concurrent
45

@@ -18,9 +19,9 @@ class CyclicBarrier < Synchronization::LockableObject
1819
#
1920
# @raise [ArgumentError] if `parties` is not an integer or is less than zero
2021
def initialize(parties, &block)
21-
if !parties.is_a?(Fixnum) || parties < 1
22-
raise ArgumentError.new('count must be in integer greater than or equal zero')
23-
end
22+
Utility::NativeInteger.ensure_integer_and_bounds parties
23+
Utility::NativeInteger.ensure_positive_and_no_zero parties
24+
2425
super(&nil)
2526
synchronize { ns_initialize parties, &block }
2627
end

lib/concurrent/atomic/mutex_atomic_fixnum.rb

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'concurrent/synchronization'
2+
require 'concurrent/utility/native_integer'
23

34
module Concurrent
45

@@ -7,10 +8,6 @@ module Concurrent
78
# @!macro internal_implementation_note
89
class MutexAtomicFixnum < Synchronization::LockableObject
910

10-
# http://stackoverflow.com/questions/535721/ruby-max-integer
11-
MIN_VALUE = -(2**(0.size * 8 - 2))
12-
MAX_VALUE = (2**(0.size * 8 - 2) - 1)
13-
1411
# @!macro atomic_fixnum_method_initialize
1512
def initialize(initial = 0)
1613
super()
@@ -71,21 +68,8 @@ def ns_initialize(initial)
7168

7269
# @!visibility private
7370
def ns_set(value)
74-
range_check!(value)
71+
Utility::NativeInteger.ensure_integer_and_bounds value
7572
@value = value
7673
end
77-
78-
# @!visibility private
79-
def range_check!(value)
80-
if !value.is_a?(Fixnum)
81-
raise ArgumentError.new('value value must be a Fixnum')
82-
elsif value > MAX_VALUE
83-
raise RangeError.new("#{value} is greater than the maximum value of #{MAX_VALUE}")
84-
elsif value < MIN_VALUE
85-
raise RangeError.new("#{value} is less than the maximum value of #{MIN_VALUE}")
86-
else
87-
value
88-
end
89-
end
9074
end
9175
end

lib/concurrent/atomic/mutex_count_down_latch.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class MutexCountDownLatch < Synchronization::LockableObject
99

1010
# @!macro count_down_latch_method_initialize
1111
def initialize(count = 1)
12-
unless count.is_a?(Fixnum) && count >= 0
13-
raise ArgumentError.new('count must be in integer greater than or equal zero')
14-
end
12+
Utility::NativeInteger.ensure_integer_and_bounds count
13+
Utility::NativeInteger.ensure_positive count
14+
1515
super()
1616
synchronize { ns_initialize count }
1717
end

lib/concurrent/atomic/mutex_semaphore.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'concurrent/synchronization'
2+
require 'concurrent/utility/native_integer'
23

34
module Concurrent
45

@@ -9,18 +10,18 @@ class MutexSemaphore < Synchronization::LockableObject
910

1011
# @!macro semaphore_method_initialize
1112
def initialize(count)
12-
unless count.is_a?(Fixnum) && count >= 0
13-
fail ArgumentError, 'count must be an non-negative integer'
14-
end
13+
Utility::NativeInteger.ensure_integer_and_bounds count
14+
Utility::NativeInteger.ensure_positive count
15+
1516
super()
1617
synchronize { ns_initialize count }
1718
end
1819

1920
# @!macro semaphore_method_acquire
2021
def acquire(permits = 1)
21-
unless permits.is_a?(Fixnum) && permits > 0
22-
fail ArgumentError, 'permits must be an integer greater than zero'
23-
end
22+
Utility::NativeInteger.ensure_integer_and_bounds permits
23+
Utility::NativeInteger.ensure_positive_and_no_zero permits
24+
2425
synchronize do
2526
try_acquire_timed(permits, nil)
2627
nil
@@ -45,9 +46,9 @@ def drain_permits
4546

4647
# @!macro semaphore_method_try_acquire
4748
def try_acquire(permits = 1, timeout = nil)
48-
unless permits.is_a?(Fixnum) && permits > 0
49-
fail ArgumentError, 'permits must be an integer greater than zero'
50-
end
49+
Utility::NativeInteger.ensure_integer_and_bounds permits
50+
Utility::NativeInteger.ensure_positive_and_no_zero permits
51+
5152
synchronize do
5253
if timeout.nil?
5354
try_acquire_now(permits)
@@ -59,9 +60,9 @@ def try_acquire(permits = 1, timeout = nil)
5960

6061
# @!macro semaphore_method_release
6162
def release(permits = 1)
62-
unless permits.is_a?(Fixnum) && permits > 0
63-
fail ArgumentError, 'permits must be an integer greater than zero'
64-
end
63+
Utility::NativeInteger.ensure_integer_and_bounds permits
64+
Utility::NativeInteger.ensure_positive_and_no_zero permits
65+
6566
synchronize do
6667
@free += permits
6768
permits.times { ns_signal }
@@ -81,9 +82,9 @@ def release(permits = 1)
8182
#
8283
# @!visibility private
8384
def reduce_permits(reduction)
84-
unless reduction.is_a?(Fixnum) && reduction >= 0
85-
fail ArgumentError, 'reduction must be an non-negative integer'
86-
end
85+
Utility::NativeInteger.ensure_integer_and_bounds reduction
86+
Utility::NativeInteger.ensure_positive reduction
87+
8788
synchronize { @free -= reduction }
8889
nil
8990
end

lib/concurrent/map.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def marshal_load(hash)
203203
undef :freeze
204204

205205
# @!visibility private
206-
DEFAULT_OBJ_ID_STR_WIDTH = (2**50).class == Fixnum ? 14 : 7 # we want to look "native", 7 for 32-bit, 14 for 64-bit
206+
DEFAULT_OBJ_ID_STR_WIDTH = 0.size == 4 ? 7 : 14 # we want to look "native", 7 for 32-bit, 14 for 64-bit
207207
# override default #inspect() method: firstly, we don't want to be spilling our guts (i-vars), secondly, MRI backend's
208208
# #inspect() call on its @backend i-var will bump @backend's iter level while possibly yielding GVL
209209
def inspect
@@ -227,8 +227,8 @@ def populate_from(hash)
227227
end
228228

229229
def validate_options_hash!(options)
230-
if (initial_capacity = options[:initial_capacity]) && (!initial_capacity.kind_of?(Fixnum) || initial_capacity < 0)
231-
raise ArgumentError, ":initial_capacity must be a positive Fixnum"
230+
if (initial_capacity = options[:initial_capacity]) && (!initial_capacity.kind_of?(Integer) || initial_capacity < 0)
231+
raise ArgumentError, ":initial_capacity must be a positive Integer"
232232
end
233233
if (load_factor = options[:load_factor]) && (!load_factor.kind_of?(Numeric) || load_factor <= 0 || load_factor > 1)
234234
raise ArgumentError, ":load_factor must be a number between 0 and 1"

lib/concurrent/thread_safe/util.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ module ThreadSafe
66
# @!visibility private
77
module Util
88

9+
# TODO (pitr-ch 15-Oct-2016): migrate to Utility::NativeInteger
910
FIXNUM_BIT_SIZE = (0.size * 8) - 2
1011
MAX_INT = (2 ** FIXNUM_BIT_SIZE) - 1
12+
# TODO (pitr-ch 15-Oct-2016): migrate to Utility::ProcessorCounter
1113
CPU_COUNT = 16 # is there a way to determine this?
1214
end
1315
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module Concurrent
2+
module Utility
3+
# @private
4+
module NativeInteger
5+
# http://stackoverflow.com/questions/535721/ruby-max-integer
6+
MIN_VALUE = -(2**(0.size * 8 - 2))
7+
MAX_VALUE = (2**(0.size * 8 - 2) - 1)
8+
9+
def ensure_upper_bound(value)
10+
if value > MAX_VALUE
11+
raise RangeError.new("#{value} is greater than the maximum value of #{MAX_VALUE}")
12+
end
13+
value
14+
end
15+
16+
def ensure_lower_bound(value)
17+
if value < MIN_VALUE
18+
raise RangeError.new("#{value} is less than the maximum value of #{MIN_VALUE}")
19+
end
20+
value
21+
end
22+
23+
def ensure_integer(value)
24+
unless value.is_a?(Integer)
25+
raise ArgumentError.new("#{value} is not an Integer")
26+
end
27+
value
28+
end
29+
30+
def ensure_integer_and_bounds(value)
31+
ensure_integer value
32+
ensure_upper_bound value
33+
ensure_lower_bound value
34+
end
35+
36+
def ensure_positive(value)
37+
if value < 0
38+
raise ArgumentError.new("#{value} cannot be negative")
39+
end
40+
value
41+
end
42+
43+
def ensure_positive_and_no_zero(value)
44+
if value < 1
45+
raise ArgumentError.new("#{value} cannot be negative or zero")
46+
end
47+
value
48+
end
49+
50+
extend self
51+
end
52+
end
53+
end

spec/concurrent/edge/future_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
c = Concurrent.future { raise 'c' }
203203

204204
Concurrent.zip(a, b, c).chain { |*args| q << args }
205-
expect(q.pop.flatten.map(&:class)).to eq [FalseClass, Fixnum, NilClass, NilClass, NilClass, RuntimeError, RuntimeError]
205+
expect(q.pop.flatten.map(&:class)).to eq [FalseClass, 0.class, NilClass, NilClass, NilClass, RuntimeError, RuntimeError]
206206
Concurrent.zip(a, b, c).rescue { |*args| q << args }
207207
expect(q.pop.map(&:class)).to eq [NilClass, RuntimeError, RuntimeError]
208208

0 commit comments

Comments
 (0)