The preemptible multi-thread library of pure user space is actually a very troublesome thing. Before designing it, we must first understand the significance of preemptible multi-thread. Its essence is the ancient multi-channel programming of UNIX, the policy can be a time-based or any other scheduling policy. No matter what the policy is, the Mechanism elements are provided by the underlying OS kernel and machine hardware. For Linux on x86, these elements include the paging mechanism, which provides stacks with identical virtual addresses and without conflicting virtual addresses between threads; clock interruption and any interruption mechanism-you can interrupt a process without notifying the user, and then choose scheduling. This mechanism is the premise of the scheduling policy; fork mechanism-starts a new thread. Only by fully simulating the above mechanisms, multithreading is the preemptible type. There is an article on the Internet that uses setjmp and longjmp to implement a collaborative multi-thread. Since the scheduling time must be determined by the thread, it cannot be preemptible. Because the hierarchical design of Linux kernel on x86 does not provide lower-layer calls to the upper layer, it is really troublesome to implement preemptible multithreading in a pure user space.
A preemptible multi-thread external environment in a user space has two main points: one is to abstract a machine, which must be able to interrupt the process outside the process, one way is to send a signal to the process. The second point is to be able to obtain the current environment of the process, such as all registers, and save the environment. The internal environment also has two main points: one is that each thread must have a stack of its own, because this is a pure user space thread, so it is best to use a way such as malloc for dynamic allocation; second, each thread must be able to start on its own. Below is some code of multithread:
// Jmp_buf env [2];
// Int idx [2];
Void interrput_func (INT sig)
{
Static int flag = 1;
// Idx [1] = setjmp (ENV [1]); // unfortunately, setjmp can only save the context of the current stack, so the context before the signal interruption cannot be obtained here, therefore, you must use the ptrace interface for help.
If (1 = Flag ){
Flag ++;
... // Create a new stack, that is, reset the ESP register and start thread_func2 on the new stack.
Thread_func2 ()
}
}
Void thread_func1 ()
{
While (1 ){
Printf ("F1 ---/N ");
}
}
Void thread_func2 ()
{
While (1 ){
Printf ("F2 ---/N ");
}
}
The code for creating a stack is omitted. thread_func2 and thread_func1 must be executed at the top of different stacks without errors.
In a parent process fork-exec, the above program Multithread is tracked using the ptrace interface. After the sigusr signal is sent to the multithread and learned by the parent process, the parent process uses ptrace_getregs/ptrace_setregs to save and set the register environment of the above program, so that thread_func1 and thread_func2 can be executed alternately.
In the above Code, the setjmp statement is annotated and called. setjmp/longjmp + signal can be used to simulate the multithreading of the operating system. However, the context saved by jmp_buf will become invalid after the function is called and returned, the signal function is executed on the current stack or another stack (using sigaltstack). In either case, sigreturn must be called, therefore, setjmp in the signal processing function is invalid. setjmp is only valid for the current stack frame. The main point here is that stack switching is inevitable to implement preemptible multithreading, stack switching does not affect the storage of the register environment. Therefore, you must use ptrace and other mechanisms to explicitly set the register context of the process. We still use the signal mechanism, this is because the signal mechanism can interrupt the process and notify the ptrace process, so that the ptrace process can modify the context of the multithread process register to simulate multithreading. In addition, thread startup is also a key point. In the case of one thread, it is almost impossible for you to start another thread using another stack in the current stack frame, all machines based on the von noriman system are single-threaded. The so-called x86 machine multithreading only keeps a series of context environments at the bottom of the process layer, then they are constantly switched to simulate multiple threads, just as the underlying system call is required for the creation and startup of OS Kernel threads, the signal mechanism is required for multi-thread creation and startup of a user space (signals are just an example and can be used in other cases). For the same reason, to implement multi-thread user space on the Feng's machine, you must use other execution threads, such as the help of ptrace.
If setjmp can get the context before interruption, and longjmp can set the context after interruption without affecting global variables, just like context_switch of kernel, then the multithread interrput_func will become:
Static int flag = 0; // flag indicates whether the execution thread enters the signal processing or longjmp.
Void interrput_func (INT sig)
{
Flag = 1;
Idx [1] = setjmp (ENV [1]); // comment *
If (1 = Flag) {// if it is a regular signal processing, switch the thread;
Flag = 0; // set the global variable, because after the following longjmp, the execution thread starts from the comment *, because the context has been switched, so no switching will be performed at that time.
Longjmp (ENV [0], idx [0]);
}
// Otherwise, the signal is returned. This thread is not entered by signal processing, but by longjmp.
}