I’m studying Ruby 1.9.2 source code. Some issues about simultaneity.
Object.new will call following funtion to allocate space from heap:
static VALUE rb_class_allocate_instance(VALUE klass)
{
NEWOBJ(obj, struct RObject);
OBJSETUP(obj, klass, T_OBJECT);
return (VALUE)obj;
}
#define NEWOBJ(obj,type) type obj = (type)rb_newobj()
VALUE
rb_newobj(void)
{
if (during_gc) {
dont_gc = 1;
during_gc = 0;
rb_bug(“object allocation during garbage collection phase”);
}
return rb_newobj_from_heap(objspace);
}
static VALUE
rb_newobj_from_heap(rb_objspace_t *objspace)
{
VALUE obj;
if ((ruby_gc_stress && !ruby_disable_gc_stress) || !freelist) {
if (!heaps_increment(objspace) && !garbage_collect(objspace)) {
during_gc = 0;
rb_memerror();
}
}
obj = (VALUE)freelist;
freelist = freelist->as.free.next;
MEMZERO((void*)obj, RVALUE, 1);
return obj;
}
I doesn’t see any code(in rb_newobj_from_heap), which is to make sure
only one thread can call the function rb_newobj at the same time in
multi-threading.
The issue found in many other places. It seems that there can be only
one thread in Ruby. But class Thread can create another thread.
x = Thread.new {
for a in 1…999
Object.new
end
}
for b in 1…999
Object.new
End
Please tell me if some other mechanism solve the simultaneity issue.
Thanks Vince