|
| 1 | +require 'concurrent/edge/lock_free_linked_set/node' |
| 2 | +require 'concurrent/edge/lock_free_linked_set/window' |
| 3 | + |
| 4 | +module Concurrent |
| 5 | + module Edge |
| 6 | + # This class implements a lock-free linked set. The general idea of this |
| 7 | + # implementation is this: each node has a successor which is an Atomic |
| 8 | + # Markable Reference. This is used to ensure that all modifications to the |
| 9 | + # list are atomic, preserving the structure of the linked list under _any_ |
| 10 | + # circumstance in a multithreaded application. |
| 11 | + # |
| 12 | + # One interesting aspect of this algorithm occurs with removing a node. |
| 13 | + # Instead of physically removing a node when remove is called, a node is |
| 14 | + # logically removed, by 'marking it.' By doing this, we prevent calls to |
| 15 | + # `remove` from traversing the list twice to perform a physical removal. |
| 16 | + # Instead, we have have calls to `add` and `remove` clean up all marked |
| 17 | + # nodes they encounter while traversing the list. |
| 18 | + # |
| 19 | + # This algorithm is a variation of the Nonblocking Linked Set found in |
| 20 | + # 'The Art of Multiprocessor Programming' by Herlihy and Shavit. |
| 21 | + class LockFreeLinkedSet |
| 22 | + include Enumerable |
| 23 | + |
| 24 | + # @!macro [attach] lock_free_linked_list_method_initialize |
| 25 | + # |
| 26 | + # @param [Fixnum] initial_size the size of the linked_list to initialize |
| 27 | + def initialize(initial_size = 0, val = nil) |
| 28 | + @head = Head.new |
| 29 | + |
| 30 | + initial_size.times do |
| 31 | + val = block_given? ? yield : val |
| 32 | + add val |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + # @!macro [attach] lock_free_linked_list_method_add |
| 37 | + # |
| 38 | + # Atomically adds the item to the set if it does not yet exist. Note: |
| 39 | + # internally the set uses `Object#hash` to compare equality of items, |
| 40 | + # meaning that Strings and other objects will be considered equal |
| 41 | + # despite being different objects. |
| 42 | + # |
| 43 | + # @param [Object] item the item you wish to insert |
| 44 | + # |
| 45 | + # @return [Boolean] `true` if successful. A `false` return indicates |
| 46 | + # that the item was already in the set. |
| 47 | + def add(item) |
| 48 | + loop do |
| 49 | + window = Window.find @head, item |
| 50 | + |
| 51 | + pred, curr = window.pred, window.curr |
| 52 | + |
| 53 | + # Item already in set |
| 54 | + return false if curr == item |
| 55 | + |
| 56 | + node = Node.new item, curr |
| 57 | + |
| 58 | + if pred.Successor_reference.compare_and_set curr, node, false, false |
| 59 | + return true |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + # @!macro [attach] lock_free_linked_list_method_<< |
| 65 | + # |
| 66 | + # Atomically adds the item to the set if it does not yet exist. |
| 67 | + # |
| 68 | + # @param [Object] item the item you wish to insert |
| 69 | + # |
| 70 | + # @return [Oject] the set on which the :<< method was invoked |
| 71 | + def <<(item) |
| 72 | + add item |
| 73 | + self |
| 74 | + end |
| 75 | + |
| 76 | + # @!macro [attach] lock_free_linked_list_method_contains |
| 77 | + # |
| 78 | + # Atomically checks to see if the set contains an item. This method |
| 79 | + # compares equality based on the `Object#hash` method, meaning that the |
| 80 | + # hashed contents of an object is what determines equality instead of |
| 81 | + # `Object#object_id` |
| 82 | + # |
| 83 | + # @param [Object] item the item you to check for presence in the set |
| 84 | + # |
| 85 | + # @return [Boolean] whether or not the item is in the set |
| 86 | + def contains?(item) |
| 87 | + curr = @head |
| 88 | + |
| 89 | + while curr < item |
| 90 | + curr = curr.next_node |
| 91 | + marked = curr.Successor_reference.marked? |
| 92 | + end |
| 93 | + |
| 94 | + curr == item && !marked |
| 95 | + end |
| 96 | + |
| 97 | + # @!macro [attach] lock_free_linked_list_method_remove |
| 98 | + # |
| 99 | + # Atomically attempts to remove an item, comparing using `Object#hash`. |
| 100 | + # |
| 101 | + # @param [Object] item the item you to remove from the set |
| 102 | + # |
| 103 | + # @return [Boolean] whether or not the item was removed from the set |
| 104 | + def remove(item) |
| 105 | + loop do |
| 106 | + window = Window.find @head, item |
| 107 | + pred, curr = window.pred, window.curr |
| 108 | + |
| 109 | + return false if curr != item |
| 110 | + |
| 111 | + succ = curr.next_node |
| 112 | + removed = curr.Successor_reference.compare_and_set succ, succ, false, true |
| 113 | + |
| 114 | + next_node unless removed |
| 115 | + |
| 116 | + pred.Successor_reference.compare_and_set curr, succ, false, false |
| 117 | + |
| 118 | + return true |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + # @!macro [attach] lock_free_linked_list_method_each |
| 123 | + # |
| 124 | + # An iterator to loop through the set. |
| 125 | + # |
| 126 | + # @param [Object] item the item you to remove from the set |
| 127 | + # @yeild [Object] each item in the set |
| 128 | + # |
| 129 | + # @return [Object] self: the linked set on which each was called |
| 130 | + def each |
| 131 | + return to_enum unless block_given? |
| 132 | + |
| 133 | + curr = @head |
| 134 | + |
| 135 | + until curr.last? |
| 136 | + curr = curr.next_node |
| 137 | + marked = curr.Successor_reference.marked? |
| 138 | + |
| 139 | + yield curr.Data unless marked |
| 140 | + end |
| 141 | + |
| 142 | + self |
| 143 | + end |
| 144 | + end |
| 145 | + end |
| 146 | +end |
0 commit comments