Skip to content

Instantly share code, notes, and snippets.

@mrkn
Created August 23, 2012 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrkn/3434046 to your computer and use it in GitHub Desktop.
Save mrkn/3434046 to your computer and use it in GitHub Desktop.
$ make -C .build test-all TESTS='test_sync.rb'
CC = clang
LD = ld
LDSHARED = clang -dynamiclib
CFLAGS = -O0 -march=core2 -mtune=core2 -ggdb -gdwarf-2 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Werror=pointer-arith -Werror=write-strings -Werror=declaration-after-statement -Werror=shorten-64-to-32 -Werror=implicit-function-declaration -fno-common -pipe -arch x86_64
XCFLAGS = -include ruby/config.h -include ruby/missing.h -D_FORTIFY_SOURCE=2 -fstack-protector -fno-strict-overflow -fvisibility=hidden -DRUBY_EXPORT
CPPFLAGS = -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -I. -I.ext/include/x86_64-darwin11.4.0 -I../include -I..
DLDFLAGS = -Wl,-undefined,dynamic_lookup -Wl,-multiply_defined,suppress -install_name /Users/kenta-murata/work/ruby.git/.build/../.prefix/lib/libruby.2.0.0.dylib -current_version 2.0.0 -compatibility_version 2.0.0 -Wl,-unexported_symbol,_Init_* -Wl,-unexported_symbol,*_threadptr_* -fstack-protector -Wl,-u,_objc_msgSend -fstack-protector -Wl,-u,_objc_msgSend -arch x86_64
SOLIBS =
./miniruby -I../lib -I. -I.ext/common ../tool/runruby.rb --extout=.ext -- --disable-gems "../test/runner.rb" --ruby="./miniruby -I../lib -I. -I.ext/common ../tool/runruby.rb --extout=.ext -- --disable-gems" test_sync.rb
Run options: "--ruby=./miniruby -I../lib -I. -I.ext/common ../tool/runruby.rb --extout=.ext -- --disable-gems"
# Running tests:
[1/1] SyncTest#test_synchronize = 0.51 s
1) Failure:
test_synchronize(SyncTest) [/Users/kenta-murata/work/ruby.git/test/test_sync.rb:57]:
<[#<Thread:0x007f904183aa08 sleep>,
#<Thread:0x007f904183ab98 sleep>,
#<Thread:0x007f904183af58 sleep>]> expected but was
<[#<Thread:0x007f904183aa08 sleep>,
#<Thread:0x007f904183ab98 sleep>,
#<Thread:0x007f904183af58 sleep>,
#<Thread:0x007f904183aa08 sleep>]>.
Finished tests in 0.512839s, 1.9499 tests/s, 1.9499 assertions/s.
1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
ruby -v: ruby 2.0.0dev (2012-08-23 trunk 36795) [x86_64-darwin11.4.0]
make: *** [yes-test-all] Error 1
diff --git a/lib/sync.rb b/lib/sync.rb
index bae05a4..c6a7321 100644
--- a/lib/sync.rb
+++ b/lib/sync.rb
@@ -147,7 +147,7 @@ module Sync_m
sync_upgrade_waiting.push [Thread.current, sync_sh_locker[Thread.current]]
sync_sh_locker.delete(Thread.current)
else
- sync_waiting.push Thread.current
+ sync_waiting.push Thread.current unless sync_waiting.include? Thread.current
end
@sync_mutex.sleep
end
@@ -216,7 +216,7 @@ module Sync_m
end
end
for th in wakeup_threads
- th.run
+ th.run if th.alive?
end
self
end
require 'test/unit'
require 'sync'
require 'timeout'
class SyncTest < Test::Unit::TestCase
class Tester
include Sync_m
end
def test_synchronize
count = 0
while count < 10
tester = Tester.new
th1 = Thread.start {
tester.sync_lock
sleep
tester.sync_unlock
tester.sync_lock
sleep
tester.sync_unlock
}
sleep 0.1 until th1.status.nil? || th1.status == 'sleep'
th2 = Thread.start {
tester.sync_lock
sleep
tester.sync_unlock
}
sleep 0.1 until th2.status == 'sleep'
th3 = Thread.start {
tester.sync_lock
sleep
tester.sync_unlock
}
sleep 0.1 until th3.status == 'sleep'
th4 = Thread.start {
tester.sync_lock
sleep
tester.sync_unlock
}
sleep 0.1 until th4.status == 'sleep'
begin
Timeout.timeout(5) do
th1.run
sleep 0.1 until th1.status == 'sleep'
sleep 0.1 until th2.status == 'sleep'
sleep 0.1 until th3.status == 'sleep'
sleep 0.1 until th4.status == 'sleep'
end
rescue Timeout::Error
next
end
assert_equal(tester.sync_waiting.uniq, tester.sync_waiting)
count += 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment