Git Product home page Git Product logo

Comments (8)

laverdet avatar laverdet commented on July 18, 2024

Good point.

from node-fibers.

tpetry avatar tpetry commented on July 18, 2024

Would definitely make clear that you can develop threadlike but does not use threads, while in many discussions people get the details of node-fibers completly wrong, they think you've implemented threads and they would loose all the asynchronous nodejs efficiency,

from node-fibers.

japj avatar japj commented on July 18, 2024

I also was wondering about https://github.com/laverdet/node-fibers/blob/master/src/coroutine.cc#L27 which states:

// Assume that v8 registered their TLS keys within the past 5 keys.. if not there's trouble.

What (and why) exactly does it depend on V8 TLS keys for?
So far I could only find that V8 Isolates uses TLS (through the V8 Thread::LocalStorageKey Thread::CreateThreadLocalKey() API in platform-win32.cc)

from node-fibers.

laverdet avatar laverdet commented on July 18, 2024

v8 uses TLS to determine which execution stack is currently running, and if v8 has been initialized in this context. Since v8 is creating machine code on the fly it needs to some extra information about the stack it's on. In most cases threads and stacks are 1:1 but fibers violate this by switching out the stack without v8 knowing it. To get around this, fibers also swaps out the last 5 TLS keys registered so that v8 thinks each new fiber is a new thread. Note that when using CORO_PTHREADS this step is skipped.

from node-fibers.

japj avatar japj commented on July 18, 2024

If I understand correctly then fibers are implemented through V8 isolates? (and the Locker API is used to 'switch' between isolates and have V8 initialise if needed?)

Is there any place outside isolates that make use of TLS in V8?
I have been looking through the V8 code and so far it seems that only Isolates are actually using TLS.

The Isolates API exposes (2 of the 3) TLS keys used through Isolate::isolate_key() and Isolate::thread_id_key(). Unfortunately I can't find an implementation for Isolate::per_isolate_thread_data_key() although it is definded in the API.

Would it help to make node-fibers use the V8 TLS api to switch the 'right' TLS keys (instead of using the pthread API directly)? That would also prevent setting any TLS to NULL for keys that are not used by V8 (so far I could only see V8 using 3 TLS keys in the Isolates implementation, while coroutine::transfer resets possibly 5 TLS keys)

from node-fibers.

japj avatar japj commented on July 18, 2024

hmm, apparently the Isolate API I looked at was internal to V8, sorry for that

from node-fibers.

laverdet avatar laverdet commented on July 18, 2024

Yeah those are internal to v8. You can access them under the symbol v8::internal::* if v8 is compiled as a dynamic library, but it is an archive by default in node so no go. Over-swapping TLS isn't really a problem though. TLS keys are mostly just pointers returned by functions like String::c_str(). In an earlier version of node-fibers, all TLS keys were swapped:
https://github.com/laverdet/node-fibers/blob/02c50facd989986932ff93445f9c20045532d8bb/src/coroutine.cc

v8 switched from using pthread_self() for thread identity to a TLS key, though, which made everything a lot easier for me.

from node-fibers.

laverdet avatar laverdet commented on July 18, 2024

I'm going to go ahead and close this. node-fibers is a highly technical project and if you're having problems with what it is you will probably discover something like asyncblock first anyway.

from node-fibers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.