Class: Hamster::Partitioned

Inherits:
Realizable show all
Defined in:
lib/hamster/list.rb

Overview

One of the ‘List`s which gets its items from a Partitioner

Constant Summary

Constants included from List

List::CADR

Instance Method Summary collapse

Methods inherited from Realizable

#cached_size?, #empty?, #head, #realized?, #size, #tail

Methods included from List

#<<, [], #add, #append, #at, #break, #cached_size?, #chunk, #clear, #combination, #cycle, #delete, #delete_at, #drop, #drop_while, #dup, #each, #each_chunk, empty, #eql?, #fill, #flat_map, #flatten, from_enum, #group_by, #hash, #indices, #init, #inits, #insert, #inspect, #intersperse, #last, #map, #merge, #merge_by, #partition, #permutation, #pop, #pretty_print, #respond_to?, #reverse, #rotate, #sample, #select, #size, #slice, #sort, #sort_by, #span, #split_at, #subsequences, #tails, #take, #take_while, #to_list, #transpose, #union, #uniq, #zip

Methods included from Enumerable

#<=>, #==, #compact, #each_index, #grep, #grep_v, #group_by, #inspect, #join, #partition, #pretty_print, #product, #reject, #sort_by, #sum, #to_set

Methods included from Enumerable

#to_list

Constructor Details

#initialize(partitioner, buffer, mutex) ⇒ Partitioned

Returns a new instance of Partitioned.



1448
1449
1450
1451
# File 'lib/hamster/list.rb', line 1448

def initialize(partitioner, buffer, mutex)
  super()
  @partitioner, @buffer, @mutex = partitioner, buffer, mutex
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hamster::List

Instance Method Details

#realizeObject



1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
# File 'lib/hamster/list.rb', line 1453

def realize
  # another thread may get ahead of us and null out @mutex
  mutex = @mutex
  mutex && mutex.synchronize do
    return if @head != Undefined # another thread got ahead of us
    while true
      if !@buffer.empty?
        @head = @buffer.shift
        @tail = Partitioned.new(@partitioner, @buffer, @mutex)
        # don't hold onto references
        # tail will keep references alive until end of list is reached
        @partitioner, @buffer, @mutex = nil, nil, nil
        return
      elsif @partitioner.done?
        @head, @size, @tail = nil, 0, self
        @partitioner, @buffer, @mutex = nil, nil, nil # allow them to be GC'd
        return
      else
        @partitioner.next_item
      end
    end
  end
end