ractor-lvar 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d91be6df13e8cab174be96e651de28aaa9105667331370cf4b3e45f09f7c2dc7
4
+ data.tar.gz: 35b6656152660e77815514b6a998e1b6def697c46c0e5f8b4c949b0dc507ab3b
5
+ SHA512:
6
+ metadata.gz: 06243c24f1d5ada37ecbe33615873c603ae655b1ffc85988cce9b6c0a7ed24b33bb047ede1991120a98ee7c35d1bb6110c073d1500da3e4400cdc1401816c852
7
+ data.tar.gz: 1f39d79815c7c0997dc3c27070ca02c57dc02ca74a067dd64bbaf080521c4b9fb4d208bef0144f3a2495319b477502cf323ab6cc397172c60fd285eb21a60741
@@ -0,0 +1,20 @@
1
+ name: Development
2
+ on: [push,pull_request]
3
+ jobs:
4
+ test:
5
+ strategy:
6
+ fail-fast: false
7
+ matrix:
8
+ os: [ubuntu]
9
+ ruby: [head, debug]
10
+
11
+ runs-on: ${{ matrix.os }}-latest
12
+ continue-on-error: ${{ matrix.ruby == 'head' || matrix.ruby == 'debug' }}
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+ - uses: ruby/setup-ruby@v1
16
+ with:
17
+ ruby-version: ${{ matrix.ruby }}
18
+
19
+ - run: bundle install
20
+ - run: bundle exec rake
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ractor-lvar.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "test-unit", "~> 3.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Koichi Sasada
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ # Ractor::LVar
2
+
3
+ More flexible Ractor-local variable object.
4
+
5
+ ```ruby
6
+ require 'ractor/lvar'
7
+
8
+ V = Ractor::LVar.new{ 42 }
9
+
10
+ r = Ractor.new do
11
+ V.ractor_local_value *= 10
12
+ V.ractor_local_value
13
+ end
14
+
15
+ p [V.ractor_local_value, r.take]
16
+ #=> [42, 420]
17
+ ```
18
+
19
+ ## Installation
20
+
21
+ You need Ruby 3.0 or later.
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem 'ractor-lvar'
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ $ bundle install
32
+
33
+ Or install it yourself as:
34
+
35
+ $ gem install ractor-lvar
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test-unit` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ko1/ractor-lvar.
46
+
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+ require "rake/extensiontask"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << "test"
9
+ t.libs << "lib"
10
+ t.test_files = FileList["test/**/*_test.rb"]
11
+ end
12
+
13
+ Rake::ExtensionTask.new "ractor/lvar" do |ext|
14
+
15
+ end
16
+
17
+ task :test => :compile
18
+ task default: :test
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ractor/lvar"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,2 @@
1
+ require 'mkmf'
2
+ create_makefile('ractor/lvar')
@@ -0,0 +1,104 @@
1
+ #include "ruby/ruby.h"
2
+ #include "ruby/ractor.h"
3
+
4
+ static VALUE rb_cRactorLVar;
5
+
6
+ // hidden C-API
7
+ void rb_ractor_local_storage_delkey(rb_ractor_local_key_t key);
8
+ VALUE rb_proc_isolate(VALUE proc);
9
+
10
+ struct ractor_lvar {
11
+ rb_ractor_local_key_t key;
12
+ VALUE default_proc;
13
+ };
14
+
15
+ static void
16
+ ractor_lvar_mark(void *ptr)
17
+ {
18
+ struct ractor_lvar *lvar = (struct ractor_lvar *)ptr;
19
+ rb_gc_mark(lvar->default_proc);
20
+ }
21
+
22
+ static void
23
+ ractor_lvar_free(void *ptr)
24
+ {
25
+ struct ractor_lvar *lvar = (struct ractor_lvar *)ptr;
26
+ rb_ractor_local_storage_delkey(lvar->key);
27
+ ruby_xfree(lvar);
28
+ }
29
+
30
+ static size_t
31
+ ractor_lvar_memsize(const void *ptr)
32
+ {
33
+ return sizeof(struct ractor_lvar);
34
+ }
35
+
36
+ static const rb_data_type_t ractor_lvar_data_type = {
37
+ "ractor/lvar",
38
+ {
39
+ ractor_lvar_mark,
40
+ ractor_lvar_free,
41
+ ractor_lvar_memsize,
42
+ NULL, // update
43
+ },
44
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
45
+ };
46
+
47
+ static VALUE
48
+ ractor_lvar_new(VALUE self)
49
+ {
50
+ VALUE default_proc = rb_block_given_p() ? rb_block_proc() : Qnil;
51
+
52
+ struct ractor_lvar *lvar;
53
+ VALUE lv = TypedData_Make_Struct(rb_cRactorLVar, struct ractor_lvar, &ractor_lvar_data_type, lvar);
54
+ lvar->key = rb_ractor_local_storage_value_newkey();
55
+ lvar->default_proc = RTEST(default_proc) ? rb_proc_isolate(default_proc) : Qfalse;
56
+ FL_SET_RAW(lv, RUBY_FL_SHAREABLE);
57
+ return lv;
58
+ }
59
+
60
+ static struct ractor_lvar *
61
+ ractor_lvar_ptr(VALUE self)
62
+ {
63
+ RUBY_ASSERT(rb_typeddata_is_kind_of(self, &ractor_lvar_data_type));
64
+ return DATA_PTR(self);
65
+ }
66
+
67
+ static VALUE
68
+ ractor_lvar_get(VALUE self)
69
+ {
70
+ rb_ractor_local_key_t key = ractor_lvar_ptr(self)->key;
71
+ VALUE val;
72
+ if (rb_ractor_local_storage_value(key, &val)) {
73
+ return val;
74
+ }
75
+ else {
76
+ VALUE default_proc = ractor_lvar_ptr(self)->default_proc;
77
+ if (default_proc) {
78
+ val = rb_proc_call_with_block(default_proc, 0, NULL, Qnil);
79
+ rb_ractor_local_storage_value_set(key, val);
80
+ return val;
81
+ }
82
+ else {
83
+ return Qnil;
84
+ }
85
+ }
86
+ }
87
+
88
+ static VALUE
89
+ ractor_lvar_set(VALUE self, VALUE val)
90
+ {
91
+ rb_ractor_local_key_t key = ractor_lvar_ptr(self)->key;
92
+ rb_ractor_local_storage_value_set(key, val);
93
+ return val;
94
+ }
95
+
96
+ void
97
+ Init_lvar(void)
98
+ {
99
+ rb_ext_ractor_safe(true);
100
+ rb_cRactorLVar = rb_define_class_under(rb_cRactor, "LVar", rb_cObject);
101
+ rb_define_singleton_method(rb_cRactorLVar, "new", ractor_lvar_new, 0);
102
+ rb_define_method(rb_cRactorLVar, "ractor_local_value", ractor_lvar_get, 0);
103
+ rb_define_method(rb_cRactorLVar, "ractor_local_value=", ractor_lvar_set, 1);
104
+ }
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ractor/lvar/version"
4
+ require 'ractor/lvar.so'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Ractor
4
+ class LVar
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/ractor/lvar/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ractor-lvar"
7
+ spec.version = Ractor::LVar::VERSION
8
+ spec.authors = ["Koichi Sasada"]
9
+ spec.email = ["ko1@atdot.net"]
10
+
11
+ spec.summary = "Ractor::LVar"
12
+ spec.description = "Ractor::LVar"
13
+ spec.homepage = "https://github.com/ko1/ractor-lvar"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.0.0dev")
16
+ spec.extensions = %w(ext/ractor/lvar/extconf.rb)
17
+
18
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/ko1/ractor-lvar"
22
+ spec.metadata["changelog_uri"] = "https://github.com/ko1/ractor-lvar"
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
28
+ end
29
+ spec.require_paths = ["lib"]
30
+ spec.add_development_dependency "rake"
31
+ spec.add_development_dependency "rake-compiler"
32
+ end
data/test.rb ADDED
File without changes
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ractor-lvar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Koichi Sasada
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake-compiler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Ractor::LVar
42
+ email:
43
+ - ko1@atdot.net
44
+ executables: []
45
+ extensions:
46
+ - ext/ractor/lvar/extconf.rb
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".github/workflows/main.yml"
50
+ - ".gitignore"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - ext/ractor/lvar/extconf.rb
58
+ - ext/ractor/lvar/lvar.c
59
+ - lib/ractor/lvar.rb
60
+ - lib/ractor/lvar/version.rb
61
+ - ractor-lvar.gemspec
62
+ - test.rb
63
+ homepage: https://github.com/ko1/ractor-lvar
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ homepage_uri: https://github.com/ko1/ractor-lvar
68
+ source_code_uri: https://github.com/ko1/ractor-lvar
69
+ changelog_uri: https://github.com/ko1/ractor-lvar
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 3.0.0dev
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.3.0.dev
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Ractor::LVar
89
+ test_files: []