Skip to content

Commit

Permalink
More improvements for threaded enumeration:
Browse files Browse the repository at this point in the history
* A property to turn off lightweight enums, for benchmarking purposes
* Use the new Queue#shutdown to trigger the iterator threads to stop themselves
* Do not join in the finalizer, to ensure the finalization thread keeps up
* Join after the #rewind thread kill
  • Loading branch information
headius committed Oct 21, 2009
1 parent 26ab137 commit d15d1ed
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/ruby/site_ruby/shared/generator.rb
Expand Up @@ -143,15 +143,14 @@ def _run
end
end

class ThreadFinalizer
def initialize(thread)
@thread = thread
class QueueFinalizer
def initialize(queue)
@queue = queue
end

def to_proc
proc do
@thread.raise StopIteration.new("generator terminating")
@thread.join rescue nil
@queue.shutdown!
end
end
end
Expand All @@ -177,7 +176,7 @@ def initialize(enum = nil, &block)
@thread = @queue._run(&block)
end

ObjectSpace.define_finalizer self, &ThreadFinalizer.new(@thread)
ObjectSpace.define_finalizer self, &QueueFinalizer.new(@queue)

self
end
Expand Down Expand Up @@ -220,7 +219,8 @@ def current()
# Rewinds the generator.
def rewind()
if @index.nonzero?
@thread.kill
@queue.shutdown!
@thread.join
initialize(@enum, &@block) if @index.nonzero?
end
self
Expand Down Expand Up @@ -281,7 +281,8 @@ def __generator

def __choose_generator
iter_for_method = :"iter_for_#{@__method__}"
if @__object__.respond_to? iter_for_method
if ENV_JAVA['jruby.enumerator.lightweight'] != 'false' &&
@__object__.respond_to?(iter_for_method)
@__object__.send iter_for_method
else
Generator::Threaded.new(self)
Expand Down
1 change: 1 addition & 0 deletions src/org/jruby/libraries/ThreadLibrary.java
Expand Up @@ -276,6 +276,7 @@ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
@JRubyMethod(name = "shutdown!")
public synchronized IRubyObject shutdown(ThreadContext context) {
entries = null;
notifyAll();
return context.getRuntime().getNil();
}

Expand Down

0 comments on commit d15d1ed

Please sign in to comment.