Analysis on preemptible multithreading in Lua

Source: Internet
Author: User

AnalysisLuaImplement preemptibleMultithreadingIs the content to be introduced in this article, mainly to learnLuaOfMultithreading.LuaThe development progress of 5.2 can be traced back to January 2010. The long process is almost two years now, and the beta version is finally available. I very much look forward to its official release in 2011. In the past two years, many new features have been pushed into version 5.2 and eventually rejected.

When we look at the improvement list, we don't seem to see much refreshing. However, if you read the source code carefully, you will find that most of them have been implemented again to work with these seemingly minor changes. If youLuaWith sufficient understanding, we will find that the most exciting improvement this time is "yieldable pcall and metamethods ". It is also officially listed as the first item in Main changes. The major new features of the language are listed at the end.

Of course, this is just my rough understanding. This conclusion is a little sloppy without a 5.2 period of practice. But I still want to talk about what this improvement can bring to our development.

Coroutine's yield can be used almost anywhere now. I used it almost because it still has some restrictions. These restrictions are not easy to understand. To understand them, I spent a whole day reading the source code of lua 5.2 beta. Next time I have the opportunity to write another blog to summarize this topic. Today, I only talk about applications.

We know that coroutine can implement a collaborationMultithreadingModel. That is, eachThread(Coroutine) Only jumps out in the desired place, and can jump back later to keep the original status of the jump ). This solves many preemptible methods.Multithreading. In an interview, the inventor of lua talked about coroutine's problem of solving concurrency.

However, writing yield is annoying. The framework often hides the yield call. If I remember correctly, the early kepler framework I read was to hide yield in the html output. The more beautiful way to think of is to compile a lua debug hook and call yield in the hook. In this way, the lua vm will automatically perform the yield operation every few lines of lua byte code.

Unfortunately, this is not supported in versions earlier than lua 5.1. Because yield has too many restrictions. If yield happens within a metamethod call or within a pcall, it will fail. This is related to nested C/Lua functions. When implementing the pcall and metamethod mechanisms, lua vm continuously redirects between functions C and lua.

Once yield occurs in multiple layers of C functions and luaFunctionNested call. Using the yield mechanism returned by longjmp will result in the loss of the stack frame called by the C function. That is to say, the status of the lua vm itself can be kept in L, but the status of the C function is lost and cannot be returned correctly. Lua 5.2 introduces a new api to solve this problem. If you are interested, read the 4.7-Handling Yields in C of the new document. But if you really want to figure it out, we recommend that you read the source code.

It is not enough to use only the debug library that comes with the system. Use debug. sethook of lua to set a lua function for hook. coroutine. yield cannot be called in it. This is still restricted by the implementation of lua. The correct method is to directly use the C api lua_sethook and set such a hook function:

 
 
  1. static void  
  2.  
  3. hook(lua_State *L, lua_Debug *ar)  
  4. {  
  5.     lua_yield(L,0);  

As for the yield code that you want to make it run every few lines, or make it happen when the function is called, it depends on your preference. However, once the setting is successful, a piece of lua code can be transparent and regularly released by yield.

What are these functions? I imagine two usage methods,

I. YesLuaCreate a scheduler to simulate a preemptibleMultithreadingLibrary. Essentially, this library is implemented based on coroutine. However, the switching thread uses the debug hook to perform regular forced switching. Based on this library, you can improve the gadgets I have previously written. It is easier to use.

2. Start Multiple independent lua states in an OS process. Each lua state does not contain multiple coroutine, but uses only one main thread. Set the debug hook so that the main thread can run yield at every stage and return the control to the upper-layer C code. Write an effective scheduler at level C. Each lua state is independent and can communicate with each other through libraries such as zeromq. The lua state can also be deployed on multiple OS threads to implement an M: N thread model. Its scheduler is more efficient than the OS. Regarding the performance differences between language-level M: N thread models and OS-level M: N-line program models, we made a discussion on google + from October 12 to October 12. Unfortunately, it was done in a limited circle, and the method was not found to be made public for the time being) and it rarely occupied a large amount of stack resources.

Now, let's look at it again. Isn't that actually the Erlang model? :)

Ps. ActuallyLuaIn earlier versions, lua coco can also be used to implement unlimited yield operations. But coco uses the fiber library of OS, which has additional stack overhead than lua Implementation of version 5.2.

Some unimplemented ideas: Can the npc ai and other things in the game server be completed in an independent lua state? They interact with each other and use messages. A state pool can be allocated at the beginning to dynamically bind new AI units to reduce the initialization cost ). Splitting a task into independent small units and doing it in an independent lua state is also very beneficial to the gc operation of lua.

Summary: AnalysisLuaImplement preemptibleMultithreadingI hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.