// Push adds an element to the top of the stack.
// When the stack's capacity is reached the storage grows as defined during
// construction. If the stack reaches 2^31 elements it is considered full
// and will return an LimitError.
Push adds an element to the top of the stack.
When the stack's capacity is reached the storage grows as defined during
construction. If the stack reaches 2^31 elements it is considered full
and will return an LimitError.
// Push adds an element to the top of the stack . // When the stack ' s capacity is reached the storage grows as defined during // construction . If the stack reaches 2^31 elements it is considered full // and will return an LimitError .
func (s *Stack) Push(v interface{}) error {
	spin := s.spin
	for {
		head := atomic.LoadUint32(s.head)
		unlockedHead := head & unlockMask
		lockedHead := head | lockMask
		if unlockedHead == unlockMask {
			return LimitError{"Stack is full"}
		}
		if atomic.CompareAndSwapUint32(s.head, unlockedHead, lockedHead) {
			if unlockedHead == uint32(len(s.data)) {
				old := s.data
				s.data = make([]interface{}, len(s.data)+s.growBy)
				copy(s.data, old)
			}
			s.data[unlockedHead] = v                   
			atomic.StoreUint32(s.head, unlockedHead+1) 
			return nil                                 
		}
		spin.Yield()
	}
}
func ( s * Stack ) Push ( v interface { } ) error { spin := s . spin for { head := atomic . LoadUint32 ( s . head ) unlockedHead := head & unlockMask lockedHead := head | lockMask if unlockedHead == unlockMask { return LimitError { " " } } if atomic . CompareAndSwapUint32 ( s . head , unlockedHead , lockedHead ) { if unlockedHead == uint32 ( len ( s . data ) ) { old := s . data s . data = make ( [ ] interface { } , len ( s . data ) + s . growBy ) copy ( s . data , old ) } s . data [ unlockedHead ] = v atomic . StoreUint32 ( s . head , unlockedHead + 1 ) return nil } spin . Yield ( ) } }
Push adds an element to the top of the stack.
Push adds an element to the top of the stack .
// Always work with unlocked head as head might be locked // Grow stack // write to new head // unlock // ### return ###