Google Inc. the first set of patches to be included in the Linux kernel, implementing the components necessary for the functioning of the M:N threading model. Google's initiative is related to the unveiling of a previously closed API. for the Linux kernel, enabling the operation of a user-space multithreading subsystem that uses the M:N threading model. This subsystem is used by Google to support services that require minimal latency. Thread scheduling and management of thread distribution are done entirely in user space, significantly reducing the number of context-switching operations by minimizing system calls.
To support the operation of this subsystem at the Linux kernel level, the SwitchTo API has been implemented, offering three basic operations: wait, resume, and swap. To be included in the kernel, new operation code FUTEX_SWAP has been proposed, complementing , and providing a basis for creating multithreading libraries in user space. FUTEX_SWAP can also be used for message passing between tasks, similar to RPC. For example, currently, passing a message between tasks requires at least four calls to FUTEX_WAIT and FUTEX_WAKE, whereas using FUTEX_SWAP will allow this with a single operation, performed 5-10 times faster.
Currently, the 1:1 and N:1 threading models are primarily used in practice. The 1:1 model is used in (POSIX threads) and , and implies a direct mapping of a user-space thread to a scheduling unit in the kernel. The N:1 model is implemented in , which moves thread dispatching to user space and allows N user-space threads to be bound to one thread in the kernel, with the kernel having no information about user threads.
The main drawback of the 1:1 model is the high overhead of context switching between the kernel and user space. The N:1 model addresses this issue but creates a new one—since a flow in the kernel is an indivisible unit of execution scheduling, user threads tied to one kernel thread in the operating system cannot scale across CPU cores and end up being bound to a single CPU core.
The M:N model is hybrid and eliminates all the aforementioned drawbacks by mapping N user space threads to M kernel threads, which reduces context switching overhead and allows scaling across CPU cores. The trade-off for this option is a significant increase in the complexity of implementing the thread scheduler in user space and the need for synchronization mechanisms with the kernel scheduler.

Source: opennet.ru
