11require 'concurrent/synchronization'
2+ require 'concurrent/utility/native_integer'
23
34module 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
0 commit comments