Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SizedQueue#num_waiting regression #3597

Merged
merged 1 commit into from Jan 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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