Jump to content
  • Advertisement
hiago desena

Multi-threading race condition

Recommended Posts

In your documentation in the "Thing to think about in multithreading" you state:

  • Multiple threads may execute scripts in separate contexts. The contexts may execute scripts from the same module, but if the module has global variables you need to make sure the scripts perform proper access control so that they do not get corrupted, if multiple threads try to update them simultaneously.

So with that said in the scenario where i have 1 engine created. with 2 threads alive. Now each thread is designated its own personal context-pool. Now note: Each allocate script-class instance assigned to a specific thread and will always run on that thread. aka a script instances executing in thread 0 can not access/talk/write with another script instances executing in thread 1. 

Now the issue comes with this asCThreadLocalData when a context execute a function. When the context executes a function it will access the threadlocaldata and attempt to push the active stack on to the thread shared memory (memory of the thread that allocated the engine, which there is one). Then it follows with a pop when unprepare is called.

As you can see this pushing/popping on the thread local data is not safe ( which makes sense since its called thread-local data, 1-per engine). But that would mean that you cant ever invoke two script classes member functions using 2 different context from 2 different thread simultaneously as it will end up (at some point). Hitting the assert:

as_context.cpp line 173

asASSERT(tld && tld->activeContexts[tld->activeContexts.GetLength() - 1] == ctx); 

Now the fix for me obivously is since each thread is independent (aka any script classess assigned to a thread will always run on that thread) I can just create an engine per-group of thread and then the shared-local memory would be one per-thread. (Since they dont/cant communicate with each other anyways, but that seems bloated).

Im wondering if you can paint me a picture that clarifies the "Multiple threads may execute scripts in separate contexts.". Should i implement my own thread manager? (Looking into this now)

 

Thanks!

Share this post


Link to post
Share on other sites
Advertisement

After some digging. I got the crash to stop happening. Im dumb. The fix was to build angelscript with AS_NO_THREADS turned off and enabling locking mechanisms. 

 

Though this fixed my issue, still begs the question why locking the pushing and popping to that thread-local data is not needed.

Share this post


Link to post
Share on other sites

Thread-local data is local to each thread, so it is guaranteed that no two threads access the same memory (unless you mistakenly share the address to the memory across the threads).

That's the reason why it is not necessary to use any locking mechanism when manipulating the thread-local data.

 

Share this post


Link to post
Share on other sites

Thank you. Im dumb. Because I didnt build in the project with Multi-threading support ON the thread local data GetLocalData for each context for returning the single main thread-data. Thanks again. 

 

With a follow up question. I know angelscript works with Xbox One + PS4. Does this include multi-threading support for those platform as well. In addition have u tested for the Nitendo-Switch platform for portability.

Share this post


Link to post
Share on other sites

I'm afraid I cannot say for sure. I personally don't have access to any of these platforms.

However the multithreading support relies on standard C++ libraries so I would be very suprised if it doesn't work on any of them.

The Nintendo Switch uses a 64bit ARM processor and so far AngelScript doesn't have support for native calling conventions on any 64bit ARM platforms. However it should work just fine with AS_MAX_PORTABILITY using the generic calling convention.

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!