mri_gil_lock 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +5 -33
- data/bin/demo.rb +71 -0
- data/ext/hold/hold.c +15 -0
- data/lib/mri_gil_lock/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8556c962fa31f8eec0526a60c75585b2083a2adae5f9e723029eb2c18c811901
|
4
|
+
data.tar.gz: c9b7ab1b0b020e54667bbe7eca1498e76ad0250c23f0cd694ece126c1d413558
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a05c54d85602a71ecc9f9b7195bc9c3c1e0df651dfc26ff72f05ba5ea6f0d28ad73ce9e78bdba8a4334d15bf24e1ee674cb098d85a64c60c7925d41a75ced336
|
7
|
+
data.tar.gz: 56f10e78c89caa8fdd52f93f69b079c4496dc5c9a69deff5e71836e49660a69390e6429663e12a245063d06be58a8871193eaf261705798100b78f9ae1986382
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,35 +1,7 @@
|
|
1
|
-
#
|
1
|
+
# Demonstration of MRI Ruby's GIL locking
|
2
2
|
|
3
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mri_gil_lock`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
|
-
|
7
|
-
## Installation
|
8
|
-
|
9
|
-
Add this line to your application's Gemfile:
|
10
|
-
|
11
|
-
```ruby
|
12
|
-
gem 'mri_gil_lock'
|
13
3
|
```
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
Or install it yourself as:
|
20
|
-
|
21
|
-
$ gem install mri_gil_lock
|
22
|
-
|
23
|
-
## Usage
|
24
|
-
|
25
|
-
TODO: Write usage instructions here
|
26
|
-
|
27
|
-
## Development
|
28
|
-
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
-
|
31
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
-
|
33
|
-
## Contributing
|
34
|
-
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mri_gil_lock.
|
4
|
+
bundle
|
5
|
+
bundle exec rake compile
|
6
|
+
bin/demo.rb
|
7
|
+
```
|
data/bin/demo.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'mri_gil_lock'
|
4
|
+
require 'timeout'
|
5
|
+
|
6
|
+
Thread.abort_on_exception=true
|
7
|
+
|
8
|
+
def test_gil_lock
|
9
|
+
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
10
|
+
sleep(1)
|
11
|
+
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
12
|
+
elapsed_time = end_time - start_time
|
13
|
+
|
14
|
+
if elapsed_time > 1.1
|
15
|
+
puts "GIL locking, time elapsed #{elapsed_time}"
|
16
|
+
else
|
17
|
+
puts "NO GIL locking, time elapsed #{elapsed_time}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
puts "First, let's demo a non blocking thread that uses Ruby's sleep:"
|
22
|
+
ruby_sleep_thread = Thread.new do
|
23
|
+
while(true) do
|
24
|
+
sleep(2)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
monitoring_thread = Thread.new do
|
29
|
+
while(true) do
|
30
|
+
test_gil_lock
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
begin
|
35
|
+
Timeout.timeout(10) do
|
36
|
+
[ruby_sleep_thread, monitoring_thread].each(&:join)
|
37
|
+
end
|
38
|
+
rescue Timeout::Error
|
39
|
+
ruby_sleep_thread.kill
|
40
|
+
end
|
41
|
+
|
42
|
+
puts "Now let's demo a blocking thread that uses a C extension:"
|
43
|
+
c_extension_sleep_thread = Thread.new do
|
44
|
+
while(true) do
|
45
|
+
MriGilLock::Hold.for_microseconds(2_000_000)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
begin
|
49
|
+
Timeout::timeout(10) do
|
50
|
+
[c_extension_sleep_thread, monitoring_thread].each(&:join)
|
51
|
+
end
|
52
|
+
rescue Timeout::Error
|
53
|
+
c_extension_sleep_thread.kill
|
54
|
+
end
|
55
|
+
|
56
|
+
puts "Now let's demo a non blocking thread that uses a C extension which releases the GIL:"
|
57
|
+
c_extension_sleep_thread_that_releases_the_gil = Thread.new do
|
58
|
+
while(true) do
|
59
|
+
MriGilLock::Hold.for_two_seconds_without_gil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
begin
|
63
|
+
Timeout::timeout(10) do
|
64
|
+
[c_extension_sleep_thread_that_releases_the_gil, monitoring_thread].each(&:join)
|
65
|
+
end
|
66
|
+
rescue Timeout::Error
|
67
|
+
c_extension_sleep_thread_that_releases_the_gil.kill
|
68
|
+
end
|
69
|
+
|
70
|
+
monitoring_thread.kill # And we're done
|
71
|
+
puts "Done!"
|
data/ext/hold/hold.c
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
#include "ruby.h"
|
2
2
|
#include <unistd.h> // provides usleep
|
3
|
+
#include <ruby/thread.h> // provides rb_thread_call_without_gvl
|
3
4
|
|
4
5
|
VALUE holdFor(VALUE self, VALUE rubyTimeToHold)
|
5
6
|
{
|
@@ -7,9 +8,23 @@ VALUE holdFor(VALUE self, VALUE rubyTimeToHold)
|
|
7
8
|
return rubyTimeToHold;
|
8
9
|
}
|
9
10
|
|
11
|
+
void* holdForTwoSeconds()
|
12
|
+
{
|
13
|
+
usleep(2000000); // avoid possible collision with ruby sleep functions
|
14
|
+
return NULL;
|
15
|
+
}
|
16
|
+
|
17
|
+
VALUE holdForTwoSecondsWithoutGil(VALUE self)
|
18
|
+
{
|
19
|
+
// https://silverhammermba.github.io/emberb/c/#c-in-ruby-threads
|
20
|
+
rb_thread_call_without_gvl(holdForTwoSeconds, NULL, RUBY_UBF_IO, NULL);
|
21
|
+
return self;
|
22
|
+
}
|
23
|
+
|
10
24
|
void Init_hold()
|
11
25
|
{
|
12
26
|
VALUE moduleMriGilLock = rb_define_module("MriGilLock");
|
13
27
|
VALUE classHold = rb_define_class_under(moduleMriGilLock, "Hold", rb_cObject);
|
14
28
|
rb_define_singleton_method(classHold, "for_microseconds", holdFor, 1);
|
29
|
+
rb_define_singleton_method(classHold, "for_two_seconds_without_gil", holdForTwoSecondsWithoutGil, 0);
|
15
30
|
}
|
data/lib/mri_gil_lock/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mri_gil_lock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Buchalter
|
@@ -83,6 +83,7 @@ files:
|
|
83
83
|
- README.md
|
84
84
|
- Rakefile
|
85
85
|
- bin/console
|
86
|
+
- bin/demo.rb
|
86
87
|
- bin/setup
|
87
88
|
- ext/hold/extconf.rb
|
88
89
|
- ext/hold/hold.c
|