Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions spec/std/fiber/execution_context/global_queue_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "./spec_helper"
require "../../../support/thread"

describe Fiber::ExecutionContext::GlobalQueue do
it "#initialize" do
Expand Down Expand Up @@ -98,10 +99,9 @@ describe Fiber::ExecutionContext::GlobalQueue do
increments = 15
queue = Fiber::ExecutionContext::GlobalQueue.new(Thread::Mutex.new)
ready = Thread::WaitGroup.new(n)
shutdown = Thread::WaitGroup.new(n)

n.times do |i|
Thread.new("ONE-#{i}") do |thread|
threads = Array(Thread).new(n) do |i|
new_thread("ONE-#{i}") do
slept = 0
ready.done

Expand All @@ -117,10 +117,6 @@ describe Fiber::ExecutionContext::GlobalQueue do
break
end
end
rescue exception
Crystal::System.print_error "\nthread: #{thread.name}: exception: #{exception}"
ensure
shutdown.done
end
end
ready.wait
Expand All @@ -130,7 +126,7 @@ describe Fiber::ExecutionContext::GlobalQueue do
Thread.sleep(10.nanoseconds) if i % 10 == 9
end

shutdown.wait
threads.each(&.join)

# must have dequeued each fiber exactly X times
fibers.each { |fc| fc.counter.should eq(increments) }
Expand All @@ -146,10 +142,9 @@ describe Fiber::ExecutionContext::GlobalQueue do

queue = Fiber::ExecutionContext::GlobalQueue.new(Thread::Mutex.new)
ready = Thread::WaitGroup.new(n)
shutdown = Thread::WaitGroup.new(n)

n.times do |i|
Thread.new("BULK-#{i}") do |thread|
threads = Array(Thread).new(n) do |i|
new_thread("BULK-#{i}") do
slept = 0

r = Fiber::ExecutionContext::Runnables(3).new(queue)
Expand Down Expand Up @@ -200,10 +195,6 @@ describe Fiber::ExecutionContext::GlobalQueue do
slept += 1
Thread.sleep(1.nanosecond) # don't burn CPU
end
rescue exception
Crystal::System.print_error "\nthread #{thread.name} raised: #{exception}"
ensure
shutdown.done
end
end
ready.wait
Expand All @@ -216,7 +207,7 @@ describe Fiber::ExecutionContext::GlobalQueue do
Thread.sleep(10.nanoseconds) if i % 4 == 3
end

shutdown.wait
threads.each(&.join)

# must have dequeued each fiber exactly X times (no less, no more)
fibers.each { |fc| fc.counter.should eq(increments) }
Expand Down
12 changes: 4 additions & 8 deletions spec/std/fiber/execution_context/runnables_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "./spec_helper"
require "../../../support/thread"

describe Fiber::ExecutionContext::Runnables do
it "#initialize" do
Expand Down Expand Up @@ -190,14 +191,13 @@ describe Fiber::ExecutionContext::Runnables do

global_queue = Fiber::ExecutionContext::GlobalQueue.new(Thread::Mutex.new)
ready = Thread::WaitGroup.new(n)
shutdown = Thread::WaitGroup.new(n)

all_runnables = Array(Fiber::ExecutionContext::Runnables(16)).new(n) do
Fiber::ExecutionContext::Runnables(16).new(global_queue)
end

n.times do |i|
Thread.new("RUN-#{i}") do |thread|
threads = Array(Thread).new(n) do |i|
new_thread("RUN-#{i}") do
runnables = all_runnables[i]
slept = 0

Expand Down Expand Up @@ -239,10 +239,6 @@ describe Fiber::ExecutionContext::Runnables do
slept += 1
Thread.sleep(1.nanosecond) # don't burn CPU
end
rescue exception
Crystal::System.print_error "\nthread #{thread.name} raised: #{exception}"
ensure
shutdown.done
end
end
ready.wait
Expand All @@ -255,7 +251,7 @@ describe Fiber::ExecutionContext::Runnables do
Thread.sleep(10.nanoseconds) if i % 2 == 1
end

shutdown.wait
threads.map(&.join)

# must have dequeued each fiber exactly X times (no less, no more)
fibers.each { |fc| fc.counter.should eq(increments) }
Expand Down
3 changes: 2 additions & 1 deletion spec/std/file_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "./spec_helper"
require "../support/thread"

private def it_raises_on_null_byte(operation, file = __FILE__, line = __LINE__, end_line = __END_LINE__, &block)
it "errors on #{operation}", file, line, end_line do
Expand Down Expand Up @@ -109,7 +110,7 @@ describe "File" do
# thread or process also opened the file; we should pass
# O_NONBLOCK to the open(2) call itself, not afterwards
file = nil
Thread.new { file = File.new(path, "w", blocking: nil) }
new_thread { file = File.new(path, "w", blocking: nil) }

begin
File.open(path, "r", blocking: false) do |file|
Expand Down
7 changes: 4 additions & 3 deletions spec/std/thread/condition_variable_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "../spec_helper"
require "../../support/thread"

# interpreter doesn't support threads yet (#14287)
pending_interpreted describe: Thread::ConditionVariable do
Expand All @@ -7,7 +8,7 @@ pending_interpreted describe: Thread::ConditionVariable do
cond = Thread::ConditionVariable.new

mutex.synchronize do
Thread.new do
new_thread do
mutex.synchronize { cond.signal }
end

Expand All @@ -22,7 +23,7 @@ pending_interpreted describe: Thread::ConditionVariable do
waiting = 0

5.times do
Thread.new do
new_thread do
mutex.synchronize do
waiting += 1
cv1.wait(mutex)
Expand Down Expand Up @@ -78,7 +79,7 @@ pending_interpreted describe: Thread::ConditionVariable do
cond = Thread::ConditionVariable.new

mutex.synchronize do
Thread.new do
new_thread do
mutex.synchronize { cond.signal }
end

Expand Down
7 changes: 4 additions & 3 deletions spec/std/thread/mutex_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "../spec_helper"
require "../../support/thread"

# interpreter doesn't support threads yet (#14287)
pending_interpreted describe: Thread::Mutex do
Expand All @@ -7,7 +8,7 @@ pending_interpreted describe: Thread::Mutex do
mutex = Thread::Mutex.new

threads = Array.new(10) do
Thread.new do
new_thread do
mutex.synchronize { a += 1 }
end
end
Expand All @@ -22,15 +23,15 @@ pending_interpreted describe: Thread::Mutex do
mutex.try_lock.should be_false
expect_raises(RuntimeError) { mutex.lock }
mutex.unlock
Thread.new { mutex.synchronize { } }.join
new_thread { mutex.synchronize { } }.join
end

it "won't unlock from another thread" do
mutex = Thread::Mutex.new
mutex.lock

expect_raises(RuntimeError) do
Thread.new { mutex.unlock }.join
new_thread { mutex.unlock }.join
end

mutex.unlock
Expand Down
11 changes: 6 additions & 5 deletions spec/std/thread_spec.cr
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
require "./spec_helper"
require "../support/thread"

# interpreter doesn't support threads yet (#14287)
pending_interpreted describe: Thread do
it "allows passing an argumentless fun to execute" do
a = 0
thread = Thread.new { a = 1; 10 }
thread = new_thread { a = 1; 10 }
thread.join
a.should eq(1)
end

it "raises inside thread and gets it on join" do
thread = Thread.new { raise "OH NO" }
thread = new_thread { raise "OH NO" }
expect_raises Exception, "OH NO" do
thread.join
end
end

it "returns current thread object" do
current = nil
thread = Thread.new { current = Thread.current }
thread = new_thread { current = Thread.current }
thread.join
current.should be(thread)
current.should_not be(Thread.current)
Expand All @@ -31,7 +32,7 @@ pending_interpreted describe: Thread do
it "yields the processor" do
done = false

thread = Thread.new do
thread = new_thread do
3.times { Thread.yield }
done = true
end
Expand All @@ -51,7 +52,7 @@ pending_interpreted describe: Thread do
{% end %}

name = nil
thread = Thread.new(name: "some-name") do
thread = new_thread(name: "some-name") do
name = Thread.current.name
end
thread.name.should eq("some-name")
Expand Down
10 changes: 10 additions & 0 deletions spec/support/thread.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% begin %}
def new_thread(name = nil, &block) : Thread
{% if flag?(:execution_context) %}
ctx = Fiber::ExecutionContext::Isolated.new(name: name || "SPEC") { block.call }
ctx.@thread
{% else %}
Thread.new(name) { block.call }
{% end %}
end
{% end %}
6 changes: 5 additions & 1 deletion src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,11 @@ private module ConsoleUtils
@@read_requests = Deque(ReadRequest).new
@@bytes_read = Deque(Int32).new
@@mtx = ::Thread::Mutex.new
@@reader_thread = ::Thread.new { reader_loop }
{% if flag?(:execution_context) %}
@@reader_thread = ::Fiber::ExecutionContext::Isolated.new("READER-LOOP") { reader_loop }
{% else %}
@@reader_thread = ::Thread.new { reader_loop }
{% end %}

private def self.reader_loop
while true
Expand Down
Loading
Loading