13

I read that the V8 Javascript engine is a just in time compiler. And that PyPy is a Python interpreter that is also a just in time compiler. PyPy is known for having a GIL in the presence of multiple threads.

Does the V8 Javascript engine have something equivalent to a global interpreter lock (GIL) to deal with web worker threads?

And do all dynamic languages have problems dealing with multi-core and if so why do the JIT compilers have problems with a GIL?

user782220
  • 10,677
  • 21
  • 72
  • 135

2 Answers2

22

Chromium Web Workers are implemented on top of V8 Isolates. Each Isolate is essentially a completely independent instance of V8 VM. Many Isolates can coexist in the same process and execute JavaScript code concurrently.

However each Isolate can only be owned by a single thread at any given moment of time. There is an Isolate level locking mechanism that embedder must use to ensure exclusive access to an Isolate.

Vyacheslav Egorov
  • 10,302
  • 2
  • 43
  • 45
  • 14
    can you translate to plain English please? – Alexander Mills Aug 08 '15 at 20:26
  • umm, can you explain why this means V8 does or doesn't have a GIL. I am pretty certain it doesn't but you need to make that more clear for readers. Having a single thread means it doesn't have a need for a GIL, from what I understand. – Alexander Mills Aug 08 '15 at 21:42
  • 7
    @AlexMills Having a single thread precisely means that you don't need a GIL because GIL is a synchronization mechanism used to ensure consistency of interpreter's internals when several threads are running inside the same interpreter. V8 doesn't have a GIL in the same sense as e.g. Python does, where interpreter itself has a lock which it takes and releases essentially across each N instructions. However absence of GIL does not mean you can run several threads inside the same instance of V8 - you have to perform locking externally to guarantee exclusive access to an instance. – Vyacheslav Egorov Aug 09 '15 at 07:42
  • @Vyacheslav Egorov So your JS app runs as multiple instance in multiple Isolate. Am I right? – S.M.Mousavi Jul 01 '17 at 17:24
4

To answer your last question, I don't think GILs are something that must necessarily be present in dynamically interpreted or JIT compiled languages. For instance, PyPy has done some preliminary work on eliminating the GIL using software transactional memory. The fact that PyPy and CPython have GILs has more to do with the design decisions that were made earlier in their histories and the fact that their internal data structures are not thread-safe.

David Sanders
  • 4,069
  • 1
  • 24
  • 38