Project

General

Profile

Feature #14792

Updated by HfCloud (Xiangyu Shi) almost 6 years ago

    This is an old problem, maybe running multiple RubyVM is a good way, which can really run ruby code in parallel in one process (Each thread runs a RubyVM)and be compatible with old codes(If some codes depend on the GIL, they can still work properly). Some other programming languages have successfully realize it(It is said that lua uses this method, and my friend has make real multi-threading javascript by running multiple ES VM, threads can share data by passing pointers(safe problem is guaranteed by the programmer)).  

 Why do I have this requirement : 
   Really parallel threads needed, I choose ruby as the script language in my game engine(https://github.com/sxysxy/HfEngine). And in some situations, I don't need to do too much computation, but I hope they are really running in parallel. (for example,I divide rendering and logic into 2 parts and dispatch them onto two different threads, (putting rendering and logic in one thread is really not a good design)). The logic threads(logic threads are called 'producer', there can be many logic threads) will submit rendering commands list to the rendering thread('consumer'). This process requires to be highly real-time(maybe 3000 times per second), and multi-process will fail to make it. 

 Possibility: 
   1. Some other languages have make it.(As I noted before) 
   2. Though there are many global functions(such as 'rb_define_method'), but GET_THREAD() can help distinguish different threads so different RubyVM. When these functions are called, they can work on specific RubyVM of the calling thread.  
   3. It should be compatible with all existed codes.(I think). including ruby codes and ruby native extensions' codes.(because it do not demand to change any interface) 
   4. Each RubyVM can have it's own GC system. 
   5. There can be such ruby codes to create new RubyVM on new thread: 

 ~~~ ruby 
 vm = RubyVM.new("a.rb") #create a new RubyVM on new thread(not run immdiately) 
 #vm.set_global_variable("$argv", vm.set_global_variable("$argv", [1, 2, 3]) #wrong! this may cause trouble when GC. 
 a = [1, 2, 3] 
 vm.set_global_variable("$argv", a) #right! variable 'a' is managed by the RubyVM on the main thread, new thread do not hold a reference to a #passing argument 
 vm.exec #run it 
 #... 
 vm.shutdown #shutdown and destroy it 
 ~~~ 

 Hope to add this feature in new ruby.

Back