|
| 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 | + class LockFreeLinkedSet |
| 7 | + include Enumerable |
| 8 | + |
| 9 | + # @!macro [attach] lock_free_linked_list_method_initialize |
| 10 | + # |
| 11 | + # @param [Fixnum] initial_size the size of the linked_list to initialize |
| 12 | + def initialize(initial_size = 0, val = nil) |
| 13 | + @head = Head.new |
| 14 | + |
| 15 | + initial_size.times do |
| 16 | + val = block_given? ? yield : val |
| 17 | + |
| 18 | + self.add val |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + # @!macro [attach] lock_free_linked_list_method_add |
| 23 | + # |
| 24 | + # Atomically adds the item to the set if it does not yet exist. Note: |
| 25 | + # internally the set uses `Object#hash` to compare equality of items, |
| 26 | + # meaning that Strings and other objects will be considered equal |
| 27 | + # despite being different objects. |
| 28 | + # |
| 29 | + # @param [Object] item the item you wish to insert |
| 30 | + # |
| 31 | + # @return [Boolean] `true` if successful. A `false` return indicates |
| 32 | + # that the item was already in the set. |
| 33 | + def add(item) |
| 34 | + loop do |
| 35 | + window = Window.find @head, item |
| 36 | + |
| 37 | + pred, curr = window.pred, window.curr |
| 38 | + |
| 39 | + # Item already in set |
| 40 | + if curr == item |
| 41 | + return false |
| 42 | + else |
| 43 | + node = Node.new item, curr |
| 44 | + |
| 45 | + return true if pred.succ.compare_and_set curr, node, false, false |
| 46 | + end |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + # @!macro [attach] lock_free_linked_list_method_<< |
| 51 | + # |
| 52 | + # Atomically adds the item to the set if it does not yet exist. |
| 53 | + # |
| 54 | + # @param [Object] item the item you wish to insert |
| 55 | + # |
| 56 | + # @return [Oject] the set on which the :<< method was invoked |
| 57 | + def <<(item) |
| 58 | + self if add item |
| 59 | + end |
| 60 | + |
| 61 | + # @!macro [attach] lock_free_linked_list_method_contains |
| 62 | + # |
| 63 | + # Atomically checks to see if the set contains an item. This method |
| 64 | + # compares equality based on the `Object#hash` method, meaning that the |
| 65 | + # hashed contents of an object is what determines equality instead of |
| 66 | + # `Object#object_id` |
| 67 | + # |
| 68 | + # @param [Object] item the item you to check for presence in the set |
| 69 | + # |
| 70 | + # @return [Boolean] whether or not the item is in the set |
| 71 | + def contains?(item) |
| 72 | + curr = @head |
| 73 | + |
| 74 | + while curr < item |
| 75 | + curr = curr.next |
| 76 | + marked = curr.succ.marked? |
| 77 | + end |
| 78 | + |
| 79 | + curr == item && !marked |
| 80 | + end |
| 81 | + |
| 82 | + # @!macro [attach] lock_free_linked_list_method_remove |
| 83 | + # |
| 84 | + # Atomically attempts to remove an item, comparing using `Object#hash`. |
| 85 | + # |
| 86 | + # @param [Object] item the item you to remove from the set |
| 87 | + # |
| 88 | + # @return [Boolean] whether or not the item was removed from the set |
| 89 | + def remove(item) |
| 90 | + loop do |
| 91 | + window = Window.find @head, item |
| 92 | + pred, curr = window.pred, window.curr |
| 93 | + |
| 94 | + if curr != item |
| 95 | + return false |
| 96 | + else |
| 97 | + succ = curr.next |
| 98 | + snip = curr.succ.compare_and_set succ, succ, false, true |
| 99 | + |
| 100 | + next unless snip |
| 101 | + |
| 102 | + pred.succ.compare_and_set curr, succ, false, false |
| 103 | + |
| 104 | + return true |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + # @!macro [attach] lock_free_linked_list_method_each |
| 110 | + # |
| 111 | + # An iterator to loop through the set. |
| 112 | + # |
| 113 | + # @param [Object] item the item you to remove from the set |
| 114 | + # @yeild [Object] each item in the set |
| 115 | + # |
| 116 | + # @return [Object] self: the linked set on which each was called |
| 117 | + def each |
| 118 | + return to_enum unless block_given? |
| 119 | + |
| 120 | + curr = @head |
| 121 | + |
| 122 | + until curr.last? |
| 123 | + curr = curr.next |
| 124 | + marked = curr.succ.marked? |
| 125 | + |
| 126 | + yield curr.data if !marked |
| 127 | + end |
| 128 | + |
| 129 | + self |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | +end |
0 commit comments