Class: Concurrent::LockFreeStack

Inherits:
Synchronization::Object show all
Includes:
Enumerable
Defined in:
lib/concurrent/edge/lock_free_stack.rb

Defined Under Namespace

Classes: Empty, Node

Constant Summary

EMPTY =
Empty[nil, nil]

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (LockFreeStack) initialize(head = EMPTY)

Returns a new instance of LockFreeStack



39
40
41
42
# File 'lib/concurrent/edge/lock_free_stack.rb', line 39

def initialize(head = EMPTY)
  super()
  self.head = head
end

Class Method Details

+ (undocumented) of1(value)



31
32
33
# File 'lib/concurrent/edge/lock_free_stack.rb', line 31

def self.of1(value)
  new Node[value, EMPTY]
end

+ (undocumented) of2(value1, value2)



35
36
37
# File 'lib/concurrent/edge/lock_free_stack.rb', line 35

def self.of2(value1, value2)
  new Node[value1, Node[value2, EMPTY]]
end

Instance Method Details

- (undocumented) clear



90
91
92
93
94
95
96
# File 'lib/concurrent/edge/lock_free_stack.rb', line 90

def clear
  while true
    current_head = head
    return false if current_head == EMPTY
    return true if compare_and_set_head current_head, EMPTY
  end
end

- (undocumented) clear_each(&block)



106
107
108
109
110
111
112
113
114
115
# File 'lib/concurrent/edge/lock_free_stack.rb', line 106

def clear_each(&block)
  while true
    current_head = head
    return self if current_head == EMPTY
    if compare_and_set_head current_head, EMPTY
      each current_head, &block
      return self
    end
  end
end

- (undocumented) clear_if(head)



98
99
100
# File 'lib/concurrent/edge/lock_free_stack.rb', line 98

def clear_if(head)
  compare_and_set_head head, EMPTY
end

- (undocumented) compare_and_clear(head)



74
75
76
# File 'lib/concurrent/edge/lock_free_stack.rb', line 74

def compare_and_clear(head)
  compare_and_set_head head, EMPTY
end

- (undocumented) compare_and_pop(head)



63
64
65
# File 'lib/concurrent/edge/lock_free_stack.rb', line 63

def compare_and_pop(head)
  compare_and_set_head head, head.next_node
end

- (undocumented) compare_and_push(head, value)



48
49
50
# File 'lib/concurrent/edge/lock_free_stack.rb', line 48

def compare_and_push(head, value)
  compare_and_set_head head, Node[value, head]
end

- (undocumented) each(head = nil)



80
81
82
83
84
85
86
87
88
# File 'lib/concurrent/edge/lock_free_stack.rb', line 80

def each(head = nil)
  return to_enum(:each, head) unless block_given?
  it = head || peek
  until it.equal?(EMPTY)
    yield it.value
    it = it.next_node
  end
  self
end

- (Boolean) empty?(head = self.head)

Returns:

  • (Boolean)


44
45
46
# File 'lib/concurrent/edge/lock_free_stack.rb', line 44

def empty?(head = self.head)
  head.equal? EMPTY
end

- (undocumented) peek



59
60
61
# File 'lib/concurrent/edge/lock_free_stack.rb', line 59

def peek
  head
end

- (undocumented) pop



67
68
69
70
71
72
# File 'lib/concurrent/edge/lock_free_stack.rb', line 67

def pop
  while true
    current_head = head
    return current_head.value if compare_and_set_head current_head, current_head.next_node
  end
end

- (undocumented) push(value)



52
53
54
55
56
57
# File 'lib/concurrent/edge/lock_free_stack.rb', line 52

def push(value)
  while true
    current_head = head
    return self if compare_and_set_head current_head, Node[value, current_head]
  end
end

- (undocumented) replace_if(head, new_head)



102
103
104
# File 'lib/concurrent/edge/lock_free_stack.rb', line 102

def replace_if(head, new_head)
  compare_and_set_head head, new_head
end

- (String) to_s

Returns Short string representation.

Returns:

  • (String)

    Short string representation.



118
119
120
# File 'lib/concurrent/edge/lock_free_stack.rb', line 118

def to_s
  format '<#%s:0x%x %s>', self.class, object_id << 1, to_a.to_s
end