Class: Concurrent::LockFreeStack

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

Defined Under Namespace

Classes: Empty, Node

Constant Summary collapse

EMPTY =
Empty[nil, nil]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(head = EMPTY) ⇒ LockFreeStack

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

.of1(value) ⇒ Object



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

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

.of2(value1, value2) ⇒ Object



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

#clearObject



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

#clear_each(&block) ⇒ Object



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

#clear_if(head) ⇒ Object



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

def clear_if(head)
  compare_and_set_head head, EMPTY
end

#compare_and_clear(head) ⇒ Object



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

#compare_and_pop(head) ⇒ Object



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

#compare_and_push(head, value) ⇒ Object



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

#each(head = nil) ⇒ Object



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

#empty?(head = self.head) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#peekObject



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

def peek
  head
end

#popObject



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

#push(value) ⇒ Object



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

#replace_if(head, new_head) ⇒ Object



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

#to_sString

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