Rate This Document
Findability
Accuracy
Completeness
Readability

Multi-Thread Support

The WD algorithm layer supports synchronous and asynchronous invoking. Figure 1 shows the thread model.

Figure 1 Thread model at the WD algorithm layer

In the preceding figure, you can create a thread in "3. call back" to execute a task that takes a long time.

Thread model

  • In synchronous invoking mode, each thread uses a context to send requests by using wd_send. After these requests enter the queue, the thread uses the context to cyclically query whether data processing is complete by using wd_recv. The processing procedure is the same as that in Default Scenario. For details about the pseudocode, see Interface Application Examples.
  • In asynchronous invoking mode, you can use any thread to invoke wd_send with the same context and create a poll thread to invoke the poll interface at the algorithm layer. If data processing is complete, you can find the context and invoke the callback function of the context. In the callback function, you can create a thread to execute a task that takes a long time to avoid blocking. The following uses the Hash algorithm as an example to describe the processing and synchronous invoking processes. For details about the pseudocode, see Interface Application Examples.
  1. Before applying for a queue, declare a structure variable struct wd_queue and specify the required algorithm string in the member variable alg of the member variable capa. (The process is the same as that in Default Scenario.)
  2. When applying for a queue, use the address of the structure variable struct wd_queue in step 1 as the parameter, and invoke int wd_request_queue(struct wd_queue *q) to apply for a queue of a specified algorithm. (The process is the same as that in Default Scenario.)
  3. When creating an algorithm context, invoke the wcrypto_create_digest_ctx function to create the context. (The process is the same as that in Default Scenario.)
  4. Before invoking an algorithm, declare the struct wcrypto_digest_op_data variable of the operation structure of the algorithm and set the members in, ou, in_bytes, and out_bytes. (The process is the same as that in Default Scenario.) After the processing is complete, the user is responsible for the release.
  5. Before invoking an algorithm, define the callback function for processing the data returned by the hardware. In the callback function, you can create a thread to execute a task that takes a long time, preventing other tasks from being blocked.
  6. Before invoking an algorithm, define the function of the poll thread. In the function, the wcrypto_digest_poll function needs to be invoked. The wcrypto_digest_poll function fills the data processed by hardware into the member variable out of the struct wcrypto_digest_op_data variable declared in step 4 by using the recv interface at the WD basic layer, and then invokes the callback function defined by the context.
  7. Before invoking an algorithm, create a poll thread, and use the address of the struct wd_queue structure variable as the parameter for the thread function.
  8. When invoking an algorithm, use the context pointer returned in step 3, the address of the struct wcrypto_digest_op_data variable in step 4, and the callback function defined in step 5 as parameters. The wcrypto_do_digest function is called to send data to the accelerator hardware through the send interface of the WD basic layer. If the data is sent successfully, 0 is returned. If the operation fails, a negative value is returned.
  9. When releasing the algorithm context, use the context pointer returned in step 3 as the parameter, and invoke the wcrypto_del_digest_ctx function to release the context.
  10. After the poll thread receives data and invokes the callback function and the callback function processes the data returned by the accelerator, release the memory of in and out related to the struct wcrypto_digest_op_data variable.
  11. When a queue is being released, use the address of the structure variable struct wd_queue in step 1 as the parameter, and invoke void wd_release_queue(struct wd_queue *q) to release the queue and related hardware resources after the thread ends.