@@ -131,6 +131,8 @@ def post_on(executor, *args, &job)
131131 # Represents an event which will happen in future (will be completed). It has to always happen.
132132 class Event < Synchronization ::LockableObject
133133 safe_initialization!
134+ private *attr_volatile_with_cas ( :internal_state )
135+ public :internal_state
134136 include Concern ::Deprecation
135137
136138 # @!visibility private
@@ -180,17 +182,17 @@ def initialize(promise, default_executor)
180182 # TODO (pitr 12-Sep-2015): replace with AtomicFixnum, avoid aba problem
181183 # TODO (pitr 12-Sep-2015): look at java.util.concurrent solution
182184 @Waiters = LockFreeStack . new
183- @State = AtomicReference . new PENDING
185+ self . internal_state = PENDING
184186 end
185187
186188 # @return [:pending, :completed]
187189 def state
188- @State . get . to_sym
190+ internal_state . to_sym
189191 end
190192
191193 # Is Event/Future pending?
192194 # @return [Boolean]
193- def pending? ( state = @State . get )
195+ def pending? ( state = internal_state )
194196 !state . completed?
195197 end
196198
@@ -202,7 +204,7 @@ def unscheduled?
202204
203205 # Has the Event been completed?
204206 # @return [Boolean]
205- def completed? ( state = @State . get )
207+ def completed? ( state = internal_state )
206208 state . completed?
207209 end
208210
@@ -313,7 +315,7 @@ def set(*args, &block)
313315
314316 # @!visibility private
315317 def complete_with ( state , raise_on_reassign = true )
316- if @State . compare_and_set ( PENDING , state )
318+ if compare_and_set_internal_state ( PENDING , state )
317319 #(state)
318320 # go to synchronized block only if there were waiting threads
319321 synchronize { ns_broadcast } if @Waiters . clear
@@ -369,11 +371,6 @@ def waiting_threads
369371 @Waiters . each . to_a
370372 end
371373
372- # @!visibility private
373- def internal_state
374- @State . get
375- end
376-
377374 private
378375
379376 # @return [true, false]
@@ -536,7 +533,7 @@ def apply(block)
536533
537534 # Has Future been success?
538535 # @return [Boolean]
539- def success? ( state = @State . get )
536+ def success? ( state = internal_state )
540537 state . completed? && state . success?
541538 end
542539
@@ -547,7 +544,7 @@ def fulfilled?
547544
548545 # Has Future been failed?
549546 # @return [Boolean]
550- def failed? ( state = @State . get )
547+ def failed? ( state = internal_state )
551548 state . completed? && !state . success?
552549 end
553550
@@ -563,23 +560,23 @@ def rejected?
563560 # @!macro edge.periodical_wait
564561 def value ( timeout = nil )
565562 touch
566- @State . get . value if wait_until_complete timeout
563+ internal_state . value if wait_until_complete timeout
567564 end
568565
569566 # @return [Exception, nil] the reason of the Future's failure
570567 # @!macro edge.timeout_nil
571568 # @!macro edge.periodical_wait
572569 def reason ( timeout = nil )
573570 touch
574- @State . get . reason if wait_until_complete timeout
571+ internal_state . reason if wait_until_complete timeout
575572 end
576573
577574 # @return [Array(Boolean, Object, Exception), nil] triplet of success, value, reason
578575 # @!macro edge.timeout_nil
579576 # @!macro edge.periodical_wait
580577 def result ( timeout = nil )
581578 touch
582- @State . get . result if wait_until_complete timeout
579+ internal_state . result if wait_until_complete timeout
583580 end
584581
585582 # Wait until Future is #complete?
@@ -601,14 +598,14 @@ def wait!(timeout = nil)
601598 # @!macro edge.periodical_wait
602599 def value! ( timeout = nil )
603600 touch
604- @State . get . value if wait_until_complete! timeout
601+ internal_state . value if wait_until_complete! timeout
605602 end
606603
607604 # @example allows failed Future to be risen
608605 # raise Concurrent.future.fail
609606 def exception ( *args )
610607 raise 'obligation is not failed' unless failed?
611- reason = @State . get . reason
608+ reason = internal_state . reason
612609 if reason . is_a? ( ::Array )
613610 reason . each { |e | log ERROR , 'Edge::Future' , e }
614611 Concurrent ::Error . new 'multiple exceptions, inspect log'
@@ -727,7 +724,7 @@ def on_failure!(&callback)
727724
728725 # @!visibility private
729726 def complete_with ( state , raise_on_reassign = true )
730- if @State . compare_and_set ( PENDING , state )
727+ if compare_and_set_internal_state ( PENDING , state )
731728 @Waiters . clear
732729 synchronize { ns_broadcast }
733730 call_callbacks state
@@ -745,12 +742,12 @@ def complete_with(state, raise_on_reassign = true)
745742
746743 # @!visibility private
747744 def add_callback ( method , *args )
748- state = @State . get
745+ state = internal_state
749746 if completed? ( state )
750747 call_callback method , state , *args
751748 else
752749 @Callbacks . push [ method , *args ]
753- state = @State . get
750+ state = internal_state
754751 # take back if it was completed in the meanwhile
755752 call_callbacks state if completed? ( state )
756753 end
@@ -759,7 +756,7 @@ def add_callback(method, *args)
759756
760757 # @!visibility private
761758 def apply ( block )
762- @State . get . apply block
759+ internal_state . apply block
763760 end
764761
765762 private
0 commit comments