1212#include < future>
1313#include < chrono>
1414
15+ #include < boost/asio/append.hpp>
16+ #include < boost/asio/async_result.hpp>
1517#include " boost/container/static_vector.hpp"
1618
1719// -----------------------------------------------------------------------------
@@ -49,36 +51,31 @@ static std::condition_variable poll_inst_cv;
4951
5052template <typename CompletionToken>
5153auto QccCrypto::async_get_instance (CompletionToken&& token) {
52- using boost::asio::async_completion;
5354 using Signature = void (int );
54- async_completion<CompletionToken, Signature> init (token);
55-
56- auto ex = boost::asio::get_associated_executor (init.completion_handler );
57-
58- boost::asio::post (my_pool, [this , ex, handler = std::move (init.completion_handler )]()mutable {
59- auto handler1 = std::move (handler);
60- if (!open_instances.empty ()) {
61- int avail_inst = open_instances.front ();
62- open_instances.pop_front ();
63- boost::asio::post (ex, std::bind (handler1, avail_inst));
64- } else if (!instance_completions.full ()) {
65- // keep a few objects to wait QAT instance to make sure qat full utilization as much as possible,
66- // that is, QAT don't need to wait for new objects to ensure
67- // that QAT will not be in a free state as much as possible
68- instance_completions.push_back ([ex, handler2 = std::move (handler1)](int inst)mutable {
69- boost::asio::post (ex, std::bind (handler2, inst));
70- });
71- } else {
72- boost::asio::post (ex, std::bind (handler1, NON_INSTANCE));
73- }
74- });
75- return init.result .get ();
55+ return boost::asio::async_initiate<CompletionToken, Signature>(
56+ [this ] (auto handler) {
57+ boost::asio::post (my_pool, [this , handler = std::move (handler)]()mutable {
58+ if (!open_instances.empty ()) {
59+ int avail_inst = open_instances.front ();
60+ open_instances.pop_front ();
61+ boost::asio::post (boost::asio::append (std::move (handler), avail_inst));
62+ } else if (!instance_completions.full ()) {
63+ // keep a few objects to wait QAT instance to make sure qat full utilization as much as possible,
64+ // that is, QAT don't need to wait for new objects to ensure
65+ // that QAT will not be in a free state as much as possible
66+ instance_completions.push_back (std::move (handler));
67+ } else {
68+ boost::asio::post (boost::asio::append (std::move (handler), NON_INSTANCE));
69+ }
70+ });
71+ }, token);
7672}
7773
7874void QccCrypto::QccFreeInstance (int entry) {
7975 boost::asio::post (my_pool, [this , entry]()mutable {
8076 if (!instance_completions.empty ()) {
81- instance_completions.front ()(entry);
77+ boost::asio::dispatch (boost::asio::append (
78+ std::move (instance_completions.front ()), entry));
8279 instance_completions.pop_front ();
8380 } else {
8481 open_instances.push_back (entry);
@@ -334,7 +331,7 @@ bool QccCrypto::perform_op_batch(unsigned char* out, const unsigned char* in, si
334331 int avail_inst = NON_INSTANCE;
335332
336333 if (y) {
337- spawn ::yield_context yield = y.get_yield_context ();
334+ boost::asio ::yield_context yield = y.get_yield_context ();
338335 avail_inst = async_get_instance (yield);
339336 } else {
340337 auto result = async_get_instance (boost::asio::use_future);
@@ -477,24 +474,29 @@ CpaStatus QccCrypto::initSession(CpaInstanceHandle cyInstHandle,
477474}
478475
479476template <typename CompletionToken>
480- auto QatCrypto::async_perform_op (int avail_inst, std::span<CpaCySymDpOpData*> pOpDataVec, CompletionToken&& token) {
481- CpaStatus status = CPA_STATUS_SUCCESS;
482- using boost::asio::async_completion;
477+ auto QatCrypto::async_perform_op (std::span<CpaCySymDpOpData*> pOpDataVec, CompletionToken&& token) {
483478 using Signature = void (CpaStatus);
484- async_completion<CompletionToken, Signature> init (token);
485- auto ex = boost::asio::get_associated_executor (init.completion_handler );
486- completion_handler = [ex, handler = init.completion_handler ](CpaStatus stat) {
487- boost::asio::post (ex, std::bind (handler, stat));
488- };
479+ return boost::asio::async_initiate<CompletionToken, Signature>(
480+ [this ] (auto handler, std::span<CpaCySymDpOpData*> pOpDataVec) {
481+ completion_handler = std::move (handler);
482+
483+ count = pOpDataVec.size ();
484+ poll_inst_cv.notify_one ();
485+ CpaStatus status = cpaCySymDpEnqueueOpBatch (pOpDataVec.size (), pOpDataVec.data (), CPA_TRUE);
489486
490- count = pOpDataVec.size ();
491- poll_inst_cv.notify_one ();
492- status = cpaCySymDpEnqueueOpBatch (pOpDataVec.size (), pOpDataVec.data (), CPA_TRUE);
487+ if (status != CPA_STATUS_SUCCESS) {
488+ boost::asio::post (bind_executor (ex,
489+ boost::asio::append (std::move (completion_handler), status)));
490+ }
491+ }, token, pOpDataVec);
492+ }
493493
494- if (status != CPA_STATUS_SUCCESS) {
495- completion_handler (status);
494+ void QatCrypto::complete () {
495+ if (--count == 0 ) {
496+ boost::asio::post (bind_executor (ex,
497+ boost::asio::append (std::move (completion_handler), CPA_STATUS_SUCCESS)));
496498 }
497- return init. result . get () ;
499+ return ;
498500}
499501
500502bool QccCrypto::symPerformOp (int avail_inst,
@@ -510,7 +512,7 @@ bool QccCrypto::symPerformOp(int avail_inst,
510512 Cpa32U iv_index = 0 ;
511513 size_t perform_retry_num = 0 ;
512514 for (Cpa32U off = 0 ; off < size; off += one_batch_size) {
513- QatCrypto helper;
515+ QatCrypto helper{my_pool. get_executor ()} ;
514516 boost::container::static_vector<CpaCySymDpOpData*, MAX_NUM_SYM_REQ_BATCH> pOpDataVec;
515517 for (Cpa32U offset = off, i = 0 ; offset < size && i < MAX_NUM_SYM_REQ_BATCH; offset += chunk_size, i++) {
516518 CpaCySymDpOpData *pOpData = qcc_op_mem[avail_inst].sym_op_data [i];
@@ -544,10 +546,10 @@ bool QccCrypto::symPerformOp(int avail_inst,
544546 do {
545547 poll_retry_num = RETRY_MAX_NUM;
546548 if (y) {
547- spawn ::yield_context yield = y.get_yield_context ();
548- status = helper.async_perform_op (avail_inst, std::span<CpaCySymDpOpData*>(pOpDataVec), yield);
549+ boost::asio ::yield_context yield = y.get_yield_context ();
550+ status = helper.async_perform_op (std::span<CpaCySymDpOpData*>(pOpDataVec), yield);
549551 } else {
550- auto result = helper.async_perform_op (avail_inst, std::span<CpaCySymDpOpData*>(pOpDataVec), boost::asio::use_future);
552+ auto result = helper.async_perform_op (std::span<CpaCySymDpOpData*>(pOpDataVec), boost::asio::use_future);
551553 status = result.get ();
552554 }
553555 if (status == CPA_STATUS_RETRY) {
0 commit comments