Skip to content

Commit ce37db4

Browse files
committed
dont add data$ get struct call when no_gil used for c functions in attached classes
1 parent e54af4c commit ce37db4

File tree

7 files changed

+38
-23
lines changed

7 files changed

+38
-23
lines changed

lib/rubex/ast/expression/method_call/c_function_call.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@ class CFunctionCall < MethodCall
55
def analyse_types(local_scope)
66
@entry = local_scope.find(@method_name)
77
super
8-
append_self_argument unless @entry.extern?
8+
append_self_argument if !@entry.extern? && !@entry.no_gil
99
@type = @entry.type.base_type
10-
#puts @method_name
11-
if @method_name == "bar"
12-
#binding.pry
13-
end
14-
#@arg_list.analyse_types local_scope
1510
@arg_list.analyse_for_target_type @type.arg_list, local_scope
1611
@arg_list.allocate_temps local_scope
1712
@arg_list.release_temps local_scope
@@ -31,9 +26,7 @@ def c_code(local_scope)
3126
private
3227

3328
def append_self_argument
34-
if !@entry.no_gil
35-
@arg_list << Expression::Self.new
36-
end
29+
@arg_list << Expression::Self.new
3730
end
3831

3932
def code_for_c_method_call(local_scope)

lib/rubex/ast/top_statement/klass/attached_klass.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ def initialize(name, attached_type, ancestor, statements, location)
1717

1818
def analyse_statement(outer_scope)
1919
super(outer_scope, attach_klass: true)
20-
prepare_data_holding_struct
20+
prepare_data_holding_struct!
2121
prepare_rb_data_type_t_struct
2222
detach_and_modify_auxillary_c_functions_from_statements
2323
add_auxillary_functions_to_klass_scope
2424
prepare_auxillary_c_functions
2525
@statements[1..-1].each do |stmt| # 0th stmt is the data struct
26-
if ruby_method_or_c_func?(stmt)
26+
if ruby_method_or_c_func_without_gil_lock?(stmt)
2727
rewrite_method_with_data_fetching stmt
2828
end
2929
stmt.analyse_statement @scope
@@ -198,7 +198,7 @@ def write_data_type_t_struct(code)
198198

199199
# Prepare the data holding struct 'data' that will hold a pointer to the
200200
# struct that is attached to this class.
201-
def prepare_data_holding_struct
201+
def prepare_data_holding_struct!
202202
struct_name = @name + '_data_struct'
203203
declarations = declarations_for_data_struct
204204
@data_struct = Statement::CStructOrUnionDef.new(
@@ -230,8 +230,8 @@ def prepare_auxillary_c_functions
230230
prepare_get_struct_c_function
231231
end
232232

233-
def ruby_method_or_c_func?(stmt)
234-
stmt.is_a?(RubyMethodDef) || stmt.is_a?(CFunctionDef)
233+
def ruby_method_or_c_func_without_gil_lock?(stmt)
234+
stmt.is_a?(RubyMethodDef) || (stmt.is_a?(CFunctionDef) && !stmt.no_gil)
235235
end
236236

237237
# Rewrite method `stmt` so that the `data` variable becomes available

lib/rubex/ast/top_statement/method_def/c_function_def.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ module Rubex
22
module AST
33
module TopStatement
44
class CFunctionDef < MethodDef
5-
attr_reader :type, :return_ptr_level
5+
attr_reader :type, :return_ptr_level, :no_gil
66

77
def initialize(type, return_ptr_level, name, arg_list, function_tags, statements)
88
super(name, arg_list, statements)
99
@type = type
1010
@return_ptr_level = return_ptr_level
1111
@function_tags = function_tags
12+
@no_gil = false
13+
if @function_tags == "no_gil"
14+
@no_gil = true
15+
end
1216
end
1317

1418
def analyse_statement(outer_scope, extern: false)
1519
super(outer_scope)
16-
if @function_tags == "no_gil"
17-
@entry.no_gil = true
18-
end
20+
@entry.no_gil = @no_gil
1921
end
2022

2123
def generate_code(code)

spec/binding_ptr_args_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
end
2121
end
2222

23-
context "Black Box testing" do
23+
context "Black Box testing", hell: true do
2424
it "compiles and checks for valid output" do
2525
setup_and_teardown_compiled_files(test_case) do |dir|
2626
require_relative "#{dir}/#{test_case}.#{os_extension}"

spec/c_function_ptrs_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
end
2222
end
2323

24-
context "Black Box testing", hell: true do
24+
context "Black Box testing" do
2525
it "compiles and checks for valid output" do
2626
setup_and_teardown_compiled_files(test_case) do |dir|
2727
require_relative "#{dir}/#{test_case}.#{os_extension}"
2828

2929
cls = CFunctionPtrs.new
3030

31-
# expect(cls.test_c_function_pointers(true)) .to eq(5)
32-
# expect(cls.test_c_function_pointers(false)).to eq(7)
31+
expect(cls.test_c_function_pointers(true)) .to eq(5)
32+
expect(cls.test_c_function_pointers(false)).to eq(7)
3333
expect(cls.test_pass_by_name).to eq(17)
3434
end
3535
end

spec/fixtures/no_gil/no_gil.rubex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
lib "rubex/ruby"; end
2+
13
cfunc void _work_without_gil(double n) no_gil
24
while n > 0 do
35
n ** 0.5 + 4
@@ -20,3 +22,21 @@ def work_with_gil(double n)
2022
n -= 1
2123
end
2224
end
25+
26+
struct test
27+
int a
28+
end
29+
30+
class A attach test
31+
cfunc void __bar no_gil
32+
int b
33+
end
34+
35+
def foo
36+
__bar
37+
end
38+
39+
cfunc void deallocate
40+
xfree(data$.test)
41+
end
42+
end

spec/no_gil_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
end
2121
end
2222

23-
context "Black Box testing" do
23+
context "Black Box testing", hell: true do
2424
it "compiles and checks for valid output" do
2525
setup_and_teardown_compiled_files(test_case) do |dir|
2626
require_relative "#{dir}/#{test_case}.#{os_extension}"

0 commit comments

Comments
 (0)