Ruby C extension and volatile

When I was writing an extension of Ruby in C, I started to notice the keyword volatile.

What is volatile?

volatile isn't really taken care of even if you're usually programming in C or C ++. These are basically related to the interaction with ** outside the program **. To put it simply, the effect is that "reading and writing to the destination with volatile will be performed exactly as written in the program. " In other words, to prevent optimizations such as "I'm writing many times in a row, so I'll only do it the last time" or "I'm reading without changing anything in the code, so I'll cut off the reading". Become. this is,

It is a convenient mechanism for handling (There is also more detailed article).

That said, Ruby doesn't have direct access to the hardware. If so, it means "external work", but what is it?

About Ruby Garbage Collector

Ruby has a garbage collector, which is implemented at the base C language level. And this garbage collector will not be garbage collected by simply putting the handle [^ 1] VALUE of the Ruby object in the register or stack, not only in a special area.

However, if the area is not taken as a variable, that is also useless. So, volatile VALUE will appear frequently to force you to reserve space as a variable.

Scenes that are still missing

However, there are still some parts that cannot be covered. Normally, there is no problem because VALUE is routed around, but if you take out the pointer of the contents with a character string etc. and use only that, after the last reference to VALUE (in C language) ) It may happen that the VALUE variable becomes unnecessary and is overwritten by another one and becomes a prey to the garbage collector (Example. /2012/04/ruby-garbage-collection-exerb-ruby.html)). In such a case, write RB_GC_GUARD (VALUE variable) after the part that requires VALUE so that VALUE will survive up to that point [^ 2].

Summary

Even in the world of C extension, "ease of writing" as the idea of Ruby is still alive, but I think that the conflict with the darkness of C language is also an undeniable aspect.

[^ 1]: There are some things like Fixnum, nil, false, true that treat it as a direct value instead of a handle. [^ 2]: The contents of RB_GC_GUARD include" passing VALUE to a function whose optimization is suppressed by compiler-specific notation" and "writing VALUE to a volatile external variable". It was.

Recommended Posts

Ruby C extension and volatile
[Tutorial] [Ruby] Creating and debugging C native extension gem
Ruby and Gem
I made a Ruby extension library in C
Symbols and Destructive Ruby
[Ruby] Big Decimal and DECIMAL
Ruby classes and instances
Ruby inheritance and delegation
Ruby variables and methods
Solving with Ruby, Perl and Java AtCoder ABC 128 C
Solving in Ruby, Perl and Java AtCoder ABC 113 C Reference
Math Girls Secret Note 104th implemented in Ruby and C
GraphQL Ruby and actual development
Ruby syntax errors and countermeasures
Use C program from Ruby
About Ruby hashes and symbols
Summarize Ruby and Dependency Injection
About Ruby and object model
[Ruby] Singular methods and singular classes
About Ruby classes and instances
Ruby variables and functions (methods)
Ruby methods and classes (basic)
Creating Ruby classes and instances
C # and Java Overrides Story
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)
[Ruby] Singular methods and singular classes
Offline real-time how to write F03 ruby and C implementation example
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java
[Ruby] Difference between get and post
Write Ruby methods using C (Part 1)
[Ruby] present/blank method and postfix if.
[Ruby] Difference between is_a? And instance_of?
Ruby standard input and various methods
About Ruby single quotes and double quotes
CGI in C and Dart: Introduction (1)
[Ruby] then keyword and case in
[Ruby basics] split method and to_s method
About Ruby product operator (&) and sum operator (|)
Write keys and values in Ruby
[Ruby] If and else problems-with operators-
[Ruby] Boolean values ​​and logical operators
Project ruby and rails version upgrade
About object-oriented inheritance and about yield Ruby
Encrypt with Java and decrypt with C #
[Ruby / Refactoring] From Ruby iterative processing like Java and C language to Ruby-like iterative processing
Sorting AtCoder ABC 111 C hashes to solve in Ruby, Perl and Java
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 2) Dynamic programming