-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
POSIX threads corrections & enhancements #6944
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,89 @@ | ||
require "spec" | ||
|
||
# These specs are marked as pending until we add | ||
# support for parallelism, where we'll see if we | ||
# need condition variables. | ||
# | ||
# Also: review these specs! | ||
describe Thread::ConditionVariable do | ||
pending "signals" do | ||
it "signals" do | ||
mutex = Thread::Mutex.new | ||
cond = Thread::ConditionVariable.new | ||
pcond = Thread::ConditionVariable.new | ||
waiting = 0 | ||
signaled = 0 | ||
|
||
threads = 3.times.map do | ||
mutex.synchronize do | ||
Thread.new do | ||
mutex.synchronize do | ||
waiting += 1 | ||
# TODO: why is this needed? Bug? | ||
pcond.signal | ||
cond.wait mutex | ||
end | ||
mutex.synchronize { cond.signal } | ||
end | ||
end.to_a | ||
|
||
while signaled < 3 | ||
mutex.synchronize do | ||
if waiting > 0 | ||
waiting -= 1 | ||
signaled += 1 | ||
cond.signal | ||
end | ||
end | ||
cond.wait(mutex) | ||
end | ||
threads.map &.join | ||
end | ||
|
||
pending "broadcasts" do | ||
it "signals & broadcasts" do | ||
mutex = Thread::Mutex.new | ||
cond = Thread::ConditionVariable.new | ||
pcond = Thread::ConditionVariable.new | ||
cv1 = Thread::ConditionVariable.new | ||
cv2 = Thread::ConditionVariable.new | ||
waiting = 0 | ||
signaled = false | ||
|
||
threads = 3.times.map do | ||
5.times do | ||
Thread.new do | ||
mutex.synchronize do | ||
waiting += 1 | ||
pcond.signal | ||
cond.wait mutex | ||
cv1.wait(mutex) | ||
|
||
waiting -= 1 | ||
cv2.signal | ||
end | ||
end | ||
end.to_a | ||
|
||
until signaled | ||
mutex.synchronize do | ||
if waiting >= 3 | ||
cond.broadcast | ||
signaled = true | ||
else | ||
pcond.wait mutex | ||
end | ||
end | ||
|
||
# wait for all threads to have acquired the mutex and be waiting on the | ||
# condition variable, otherwise the main thread could acquire the lock | ||
# first, and signal into the void. | ||
# | ||
# since increments to waiting are synchronized, at least 4 threads are | ||
# guaranteed to be waiting when waiting is 5, which is enough for futher | ||
# tests to never hangup. | ||
until waiting == 5 | ||
Thread.yield | ||
end | ||
|
||
# wakes up at least 1 thread: | ||
mutex.synchronize do | ||
cv1.signal | ||
cv2.wait(mutex) | ||
end | ||
waiting.should be < 5 | ||
|
||
# wakes up all waiting threads: | ||
mutex.synchronize do | ||
cv1.broadcast | ||
|
||
until waiting == 0 | ||
cv2.wait(mutex, 1.second) { raise "unexpected wait timeout" } | ||
end | ||
end | ||
end | ||
|
||
it "timeouts" do | ||
timedout = false | ||
mutex = Thread::Mutex.new | ||
cond = Thread::ConditionVariable.new | ||
|
||
threads.map &.join | ||
mutex.synchronize do | ||
cond.wait(mutex, 1.microsecond) { timedout = true } | ||
end | ||
timedout.should be_true | ||
end | ||
|
||
pending "waits and send signal" do | ||
a = 0 | ||
cv1 = Thread::ConditionVariable.new | ||
cv2 = Thread::ConditionVariable.new | ||
m = Thread::Mutex.new | ||
it "resumes before timeout" do | ||
timedout = false | ||
mutex = Thread::Mutex.new | ||
cond = Thread::ConditionVariable.new | ||
|
||
thread = Thread.new do | ||
3.times do | ||
m.synchronize { cv1.wait(m); a += 1; cv2.signal } | ||
mutex.synchronize do | ||
Thread.new do | ||
mutex.synchronize { cond.signal } | ||
end | ||
end | ||
|
||
a.should eq(0) | ||
3.times do |i| | ||
m.synchronize { cv1.signal; cv2.wait(m) } | ||
a.should eq(i + 1) | ||
cond.wait(mutex, 1.second) { timedout = true } | ||
end | ||
|
||
thread.join | ||
timedout.should be_false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require "spec" | ||
|
||
describe Thread::Mutex do | ||
it "synchronizes" do | ||
a = 0 | ||
mutex = Thread::Mutex.new | ||
|
||
threads = 10.times.map do | ||
Thread.new do | ||
mutex.synchronize { a += 1 } | ||
end | ||
end.to_a | ||
|
||
threads.each(&.join) | ||
a.should eq(10) | ||
end | ||
|
||
it "won't lock recursively" do | ||
mutex = Thread::Mutex.new | ||
mutex.try_lock.should be_true | ||
mutex.try_lock.should be_false | ||
expect_raises(Errno) { mutex.lock } | ||
mutex.unlock | ||
end | ||
|
||
it "won't unlock from another thread" do | ||
mutex = Thread::Mutex.new | ||
mutex.lock | ||
|
||
expect_raises(Errno) do | ||
Thread.new { mutex.unlock }.join | ||
end | ||
|
||
mutex.unlock | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
lib LibC | ||
fun sched_yield : Int | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
lib LibC | ||
fun sched_yield : Int | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
lib LibC | ||
fun sched_yield : Int | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
lib LibC | ||
fun sched_yield : Int | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
lib LibC | ||
fun sched_yield : Int | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
lib LibC | ||
fun sched_yield : Int | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since these were pending until somebody works on parallelism, and nobody did that yet, is it worth uncommenting these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Who said "nobody's working on MT" :)
But sure, I can keep the note.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I meant all the specs. But if "somebody" ;-) is working on it, then this is fine.