Skip to content

Commit

Permalink
Merge branch 'etehtsea-num_waiting_regression'
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Jan 19, 2016
2 parents 8db8202 + 304129d commit c0b4ba1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
17 changes: 16 additions & 1 deletion core/src/main/java/org/jruby/ext/thread/SizedQueue.java
Expand Up @@ -12,7 +12,7 @@
* rights and limitations under the License.
*
* Copyright (C) 2006 MenTaLguY <mental@rydia.net>
*
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
Expand Down Expand Up @@ -101,4 +101,19 @@ public synchronized IRubyObject initialize(ThreadContext context, IRubyObject ar

return this;
}

@JRubyMethod(name = {"push", "<<", "enq"}, required = 1, optional = 1)
@Override
public IRubyObject push(ThreadContext context, final IRubyObject[] args) {
checkShutdown();
numWaiting.incrementAndGet();
try {
context.getThread().executeTask(context, args, putTask);
return this;
} catch (InterruptedException ie) {
throw context.runtime.newThreadError("interrupted in " + getMetaClass().getName() + "#push");
} finally {
numWaiting.decrementAndGet();
}
}
}
11 changes: 11 additions & 0 deletions spec/ruby/library/thread/sizedqueue/num_waiting_spec.rb
Expand Up @@ -4,4 +4,15 @@

describe "Thread::SizedQueue#num_waiting" do
it_behaves_like :queue_num_waiting, :num_waiting, SizedQueue.new(10)

it "reports the number of threads waiting to push" do
q = SizedQueue.new(1)
q.push(1)
t = Thread.new { q.push(2) }
sleep 0.05 until t.stop?
q.num_waiting.should == 1

q.pop
t.join
end
end

0 comments on commit c0b4ba1

Please sign in to comment.