Fiber (fiber) (post)

Source: Internet
Author: User
Fiber: Microsoft joins windows to better port Unix server applications to Windows. Therefore, this article does not really have much application value, just to make the notes more complete.

 

After reading this chapter, I feel that the fiber is a smaller running unit than the thread. You can split a thread into multiple fiber threads, and then manually convert them to make them work.

What you need to know is manual conversion, rather than automatic system switching. Because the implementation of threads is completed through the Windows Kernel, Windows can automatically schedule threads. However, the optical fiber
It is implemented through user-mode code. It is an algorithm written by the programmer. The kernel does not know the implementation method of the fiber path, but a scheduling algorithm defined by yourself, therefore, fiber routes are "non-preemptible" scheduling methods.

You also need to know that a thread can contain multiple fiber threads.

 

The first thing to do is to convert the current thread into a fiber:

Pvoid convertthreadtofiber (pvoid pvparam );

 

After this function is called, The system allocates about 200 bytes of storage space for the fiber execution environment. The execution environment consists of the following content:

1. the user-defined value is specified by the pvparam parameter.

2. structured exception handling chain head.

3. the maximum and lowest address of the fiber memory stack. When the thread is converted to the fiber stack, this is also the thread memory stack.

4. Various CPU registers, such as stack pointer registers and instruction pointer registers.

 

By default, the floating point status information of the CPU in the x86 system does not appear to belong to the CPU register in the fiber program. Therefore, executing some related floating point operations in the fiber process will destroy the data. For grams
Server, you need to call the convertthreadtofiberex function (Windows
Vista and later versions), and pass the 2nd parameter dwflags to the faber_flag_float_switch:

Pvoid convertthreadtofiberex (
Pvoid pvparam,
DWORD dwflags );

 

After the two functions are called, you initialize a fiber execution environment, which is associated with the thread execution environment, and the thread is converted to a fiber, the program runs inside the thread.
The convertthreadtofiber (Ex) function actually returns the memory address of the fiber execution environment. You will use this address later, but you cannot directly read or write this address. You
Use the fiber function provided by the system to manipulate the address.

When your fiber returns or calls exitthread, your fiber ends.

 

If there is only one fiber in a thread, there is no need to convert the thread into a fiber. Only when you plan to create another fiber in the same thread is necessary. To create a fiber program, use the createfiber function:

Pvoid createfiber (
DWORD dwstacksize, // size of the created stack. 0 indicates the default size.
Pfiber_start_routine pfnstartaddress, // fiber function address
Pvoid pvparam); // parameters passed to the fiber Function

 

This function creates a new stack. The stack size is specified by dwstacksize. If 0 is passed to it, a default Size Stack is created.

If you want to make a thread contain multiple fiber threads and want to spend less space, you can use the createfiberex function (available only in Windows Vista and later versions ):

Pvoid createfiberex (
Size_t dwstackcommitsize, // size of the initial stack commit
Size_t dwstackreservesize, // the size of the virtual memory to be retained
DWORD dwflags, // create a flag
Pfiber_start_routine pstartaddress, // fiber function pointer
Pvoid pvparam); // parameters passed to the fiber Function

 

If you pass the fiber_flag_float_switch parameter to dwflags, it indicates that the floating point information is added to the fiber execution environment.

 

After the createfiber (Ex) function creates a new stack, it allocates a new fiber execution environment structure and initializes it. User-defined data is saved through the pvparam parameter, the maximum and minimum addresses of the memory space of the new stack are saved, and the fiber function address is saved through the pstartaddress parameter.

The format of the fiber function must be defined as follows:

Void winapi fiberfunc (pvoid pvparam );

During the first scheduling of a fiber process, the fiber function is called. The pvparam parameter is specified by the pvparam parameter in createfiber (Ex. In fiber functions, you can do anything you want.

Like the convertthreadtofiber (Ex) function, createfiber (Ex) also returns the memory address of the fiber execution environment, which is like a handle and directly identifies a fiber.

After you use the createfiber (Ex) function to create a fiber, the fiber will not be executed because the system will not automatically schedule it. You must call the switchtofiber function to tell the system which fiber process you want to execute:

Void switchtofiber (pvoid pvfiberexecutioncontext );

 

The parameter of the switchtofiber function is the memory address of a fiber execution environment, which is returned by converthreadtofiber (Ex) or createfiber (Ex.

The internal execution steps of the switchtofiber function are as follows:

1. Save the current CPU register information, which is stored in the execution environment of the running fiber.

2. Load the last stored CPU register information from the execution environment of the fiber to be executed.

3. Associate the fiber execution environment to be executed with the thread, and the thread executes the specified fiber.

4. Set the command pointer to the saved value to continue the last execution.

 

The switchtofiber function is the only method that a fiber can be scheduled. Therefore, the scheduling of the fiber is completely controlled by the user. The scheduling of a fiber has nothing to do with the scheduling of a thread. One line
Including the running fiber, it will still be preemptible by other threads. When a thread is scheduled and there are several fiber threads in it, only the selected fiber thread will be executed, and the execution of other fiber threads will need to be called.
The switchtofiber function.

 

Finally, if a fiber completes the task, you need to delete it, call the deletefiber function, and pass the execution environment memory address of the fiber:

Void deletefiber (pvoid pvfiberexecutioncontext );

 

This function first clears the fiber stack and then deletes the fiber execution environment. However, if the parameter specifies a fiber program associated with the current thread, this function calls the exitthread function. When the thread ends, all other fiber threads are also completed. Therefore, the deletefiber function is generally called by one fiber program to delete another fiber.

When all fiber threads are finished, you need to switch from fiber to thread and call the convertfibertothread function.

 

If you need to save some data in a fiber, you can use the "fiber local storage" (FLS) mechanism. This mechanism is similar to "Thread Local Storage" (TLS.

First, call the flsalloc function to allocate a FLS slot to store data. This FLS slot can be used by all the fibers in the current process. The function has a parameter: a callback function pointer, this callback function is called in either of the following scenarios: a fiber is deleted, and the FLS slot is deleted through the flsfree function.

Then, after you call the flsalloc function, you can use the flssetvalue function in the program to save data to the FLS slot. At the same time, this function requires a DWORD parameter, it indicates the index of a FLS slot, that is, the data is saved in the relevant area of the FLS slot.

Then, you can use the flsgetvalue function in each fiber to obtain the corresponding data in the FLS slot. You also need the index of the above FLS slot and return the pointer to the data.

After using the data, you can use flsfree to release the FLS slot.

 

If you want to know whether you are running in a fiber execution environment, you can use the isthreadafiber function, which returns a bool value to indicate whether you are running in a fiber process.

 

A thread can only execute one fiber thread at a time, which is associated with this thread. You can use the following function to obtain the execution environment memory address of the fiber program being executed:

Pvoid getcurrentfiber ();

 

Each fiber path contains a user-defined data, which is specified by the pvparam parameter of createfiber (Ex) or convertthreadtofiber (Ex). You can use the following function to get the pointer to this data:

Pvoid getfiberdata ();

 

Finally, let's assume that a thread has two fiber threads and summarize the usage of the fiber threads:

1. Use converthreadtofiber (Ex) to convert the current thread to the fiber. This is the fiber F1

2. Define a fiber function to create a new fiber.

3. Call the createfiber (Ex) function in fiber F1 to create a new fiber F2

4. The switchtofiber function is used to switch the fiber process so that the newly created Fiber Process F2 can be executed.

5. When the F2 fiber function is executed, use switchtofiber to convert to F1

6. Call deletefiber in fiber F1.

7. In fiber F1, call converfibertothread and convert it to a thread.

8. End of Thread

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.