On Fri, Nov 18, 2011 at 5:24 PM, Hiroshi Nakamura <nahi / ruby-lang.org> wrote:
> (2011/11/19 6:31), Charles Oliver Nutter wrote:
>>> Sure. This deadlock from cross-require is intentional at this
>>> moment I believe, and that's the reason for the warning 'loading
>>> in progress, circular require considered harmful'. Related
>>> discussion is at #920.
>>>
>>> I don't know how Java's classloader avoids this problem.
>>
>> Each classloader has a single lock, and if it needs to call a
>> parent classloader the locks are still always acquired in the same
>> order (meaning deadlock is impossible).
>
> 1) No parallel loading in single classloader
>  (single lock, disallowing concurrent loading)

Correct.

> 2) Classloaders know class loading order at compile time to avoid
>  deadlock

No, but classloading always progresses in the same direction, from
dependent classes to dependencies and from child classloaders to
parent classloaders, so having multiple locks involved can't deadlock.

Also, classloading does not in itself trigger any code execution; not
until the class has successfully loaded do static initializers run.
Ruby's problem with parallel loading is that files are executed
*while* they're loaded, which makes running them in parallel
inherently unsafe.

> Indeed. Warning would be the best we can do.

It would have to be a warning saying "you are doing concurrent
requires" rather than a warning for circular requires. The deadlock
does not occur in circular requires...only in requires from across
threads that depend on each others' files.

>> You're right, though...it's important to point out Yehuda's problem
>> is not an autoload problem. Autoload thread-safety itself is
>> achievable. Require thread-safety doesn't exist even today. That's
>> the real problem.
>
> Sure. Moreover, I think that require thread-safety could not be
> solved easily in Ruby, so I want to find a different way to solve
> Yehuda's real problem.

I think we could claim autoload is thread-safe in itself if it always
just used a single lock. In other words, only one thread can be
processing autoloads at a time. This doesn't fix the parallel require
issue, but it would fix parallel autoloading.

I think that's an ok fix, no?

- Charlie