3 ms·
How is appending to a global array re-entrant? If this was c-code this would be suspicious. Is there something in ruby that makes this safe? For example if the
by benmmurphy 12y ago
How is appending to a global array re-entrant? If this was c-code this would be suspicious. Is there something in ruby that makes this safe? For example if the append code is:
1. array[size] = element
2. size = size + 1
then we could get:
1. array[size] = element (first signal handler is run)
2. array[size] = element (second signal handler is run)
3. size = size + 1
4. size = size + 1
and the array is corrupted
- simula67 12y agoI am assuming that the global queue is written in a way that makes enqueuing and dequeueing an atomic operation that cannot cause a control switch. This code will ensure that re-entrance is unpermitted only during the relatively short time it takes to enqueue or dequeue an element as opposed to making the whole signal handler code non-reentrable.
- benmmurphy 12y agoI think it works because signal handling in ruby is implemented within its own queue. It looks like when signals happen in ruby they are added to a buffer and then threads check this buffer when they are running. So threads will only run signals when it is compatible with the global interpreter lock to do so. So as you said this would make the enqueing/dequeuing atomic because they are protected by the GIL.