简体   繁体   中英

Does LLVM provide any facilities for implementing “green threads”/lightweight processes?

I am looking into designing a concurrent language with support for lightweight processes ("green threads") in the vein of Erlang using LLVM as a native code generator. Lightweight processes are allocated to native OS threads in an M:N fashion, and work-stealing between threads should be possible (ie processes should be represented by a data structure that can be passed between threads if necessary). A very large number of processes might exist at once, so processes shouldn't take up much memory and context switching between them should be as quick as possible. Furthermore, it should be rather simple to "pause" a lightweight process during context switches or if garbage collection occurs. I understand Erlang has an LLVM backend, but I can find very little literature on its implementation; can someone describe to me how this might be possible?

I have no specific experience with LLVM or Erlang.

But I've implemented such a system, in a programming langauge called PARLANSE. Yes, getting the context switches to be cheap is difficult.

More details at this SO answer: https://stackoverflow.com/a/999610/120163

What little knowledge I have of LLVM suggests it may be difficult. What you need to generate is context switching code. I'm not sure LLVM supports that directly. It certainly isn't something that is easy to do when generating pure C code, because the language primitives don't let you get at the machine/thread state very well.

he flip side here is the Clang, in trying to support C++14 features, must surely stumble into "C++ native" threads. There has to be context switching support to enable that, so maybe somebody has is or already has addressed the problem.

LLVM isn't directly relevant to implementing this type of system. There are plenty of frontends for languages with such constructs that lower to LLVM's IR.

LLVM is just compiler technology to generate native code for a single thread of execution. Implementing context switching, setting up the stack appropriately (cactus stacks or other techniques), and other concerns are primarily the responsibility of the runtime and environment.

One exception is supporting the synthesis of runtime calls to grow the stack when necessary, and that potentially splitting the stack into non-contiguous regions. As indicated in comments, LLVM has some support for this, although it is less well tested. However, your frontend can also control the usage of the stack in order to avoid needing any support in LLVM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM