@@ -4556,7 +4556,7 @@ bool ECDH::IsKeyPairValid() {
4556
4556
}
4557
4557
4558
4558
4559
- class PBKDF2Request : public AsyncWrap {
4559
+ class PBKDF2Request : public AsyncWrap , public ThreadPoolWork {
4560
4560
public:
4561
4561
PBKDF2Request (Environment* env,
4562
4562
Local<Object> object,
@@ -4566,6 +4566,7 @@ class PBKDF2Request : public AsyncWrap {
4566
4566
int keylen,
4567
4567
int iteration_count)
4568
4568
: AsyncWrap(env, object, AsyncWrap::PROVIDER_PBKDF2REQUEST),
4569
+ ThreadPoolWork (env),
4569
4570
digest_(digest),
4570
4571
success_(false ),
4571
4572
pass_(std::move(pass)),
@@ -4574,21 +4575,14 @@ class PBKDF2Request : public AsyncWrap {
4574
4575
iteration_count_(iteration_count) {
4575
4576
}
4576
4577
4577
- uv_work_t * work_req () {
4578
- return &work_req_;
4579
- }
4580
-
4581
4578
size_t self_size () const override { return sizeof (*this ); }
4582
4579
4583
- static void Work ( uv_work_t * work_req) ;
4584
- void Work () ;
4580
+ void DoThreadPoolWork () override ;
4581
+ void AfterThreadPoolWork ( int status) override ;
4585
4582
4586
- static void After (uv_work_t * work_req, int status);
4587
4583
void After (Local<Value> (*argv)[2]);
4588
- void After ();
4589
4584
4590
4585
private:
4591
- uv_work_t work_req_;
4592
4586
const EVP_MD* digest_;
4593
4587
bool success_;
4594
4588
MallocedBuffer<char > pass_;
@@ -4598,7 +4592,7 @@ class PBKDF2Request : public AsyncWrap {
4598
4592
};
4599
4593
4600
4594
4601
- void PBKDF2Request::Work () {
4595
+ void PBKDF2Request::DoThreadPoolWork () {
4602
4596
success_ =
4603
4597
PKCS5_PBKDF2_HMAC (
4604
4598
pass_.data , pass_.size ,
@@ -4611,12 +4605,6 @@ void PBKDF2Request::Work() {
4611
4605
}
4612
4606
4613
4607
4614
- void PBKDF2Request::Work (uv_work_t * work_req) {
4615
- PBKDF2Request* req = ContainerOf (&PBKDF2Request::work_req_, work_req);
4616
- req->Work ();
4617
- }
4618
-
4619
-
4620
4608
void PBKDF2Request::After (Local<Value> (*argv)[2]) {
4621
4609
if (success_) {
4622
4610
(*argv)[0 ] = Null (env ()->isolate ());
@@ -4629,7 +4617,12 @@ void PBKDF2Request::After(Local<Value> (*argv)[2]) {
4629
4617
}
4630
4618
4631
4619
4632
- void PBKDF2Request::After () {
4620
+ void PBKDF2Request::AfterThreadPoolWork (int status) {
4621
+ std::unique_ptr<PBKDF2Request> req (this );
4622
+ if (status == UV_ECANCELED)
4623
+ return ;
4624
+ CHECK_EQ (status, 0 );
4625
+
4633
4626
HandleScope handle_scope (env ()->isolate ());
4634
4627
Context::Scope context_scope (env ()->context ());
4635
4628
Local<Value> argv[2 ];
@@ -4638,17 +4631,6 @@ void PBKDF2Request::After() {
4638
4631
}
4639
4632
4640
4633
4641
- void PBKDF2Request::After (uv_work_t * work_req, int status) {
4642
- std::unique_ptr<PBKDF2Request> req (
4643
- ContainerOf (&PBKDF2Request::work_req_, work_req));
4644
- req->env ()->DecreaseWaitingRequestCounter ();
4645
- if (status == UV_ECANCELED)
4646
- return ;
4647
- CHECK_EQ (status, 0 );
4648
- req->After ();
4649
- }
4650
-
4651
-
4652
4634
void PBKDF2 (const FunctionCallbackInfo<Value>& args) {
4653
4635
Environment* env = Environment::GetCurrent (args);
4654
4636
@@ -4695,14 +4677,10 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
4695
4677
if (args[5 ]->IsFunction ()) {
4696
4678
obj->Set (env->context (), env->ondone_string (), args[5 ]).FromJust ();
4697
4679
4698
- env->IncreaseWaitingRequestCounter ();
4699
- uv_queue_work (env->event_loop (),
4700
- req.release ()->work_req (),
4701
- PBKDF2Request::Work,
4702
- PBKDF2Request::After);
4680
+ req.release ()->ScheduleWork ();
4703
4681
} else {
4704
4682
env->PrintSyncTrace ();
4705
- req->Work ();
4683
+ req->DoThreadPoolWork ();
4706
4684
Local<Value> argv[2 ];
4707
4685
req->After (&argv);
4708
4686
@@ -4715,7 +4693,7 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
4715
4693
4716
4694
4717
4695
// Only instantiate within a valid HandleScope.
4718
- class RandomBytesRequest : public AsyncWrap {
4696
+ class RandomBytesRequest : public AsyncWrap , public ThreadPoolWork {
4719
4697
public:
4720
4698
enum FreeMode { FREE_DATA, DONT_FREE_DATA };
4721
4699
@@ -4725,16 +4703,13 @@ class RandomBytesRequest : public AsyncWrap {
4725
4703
char * data,
4726
4704
FreeMode free_mode)
4727
4705
: AsyncWrap(env, object, AsyncWrap::PROVIDER_RANDOMBYTESREQUEST),
4706
+ ThreadPoolWork (env),
4728
4707
error_(0 ),
4729
4708
size_(size),
4730
4709
data_(data),
4731
4710
free_mode_(free_mode) {
4732
4711
}
4733
4712
4734
- uv_work_t * work_req () {
4735
- return &work_req_;
4736
- }
4737
-
4738
4713
inline size_t size () const {
4739
4714
return size_;
4740
4715
}
@@ -4772,7 +4747,8 @@ class RandomBytesRequest : public AsyncWrap {
4772
4747
4773
4748
size_t self_size () const override { return sizeof (*this ); }
4774
4749
4775
- uv_work_t work_req_;
4750
+ void DoThreadPoolWork () override ;
4751
+ void AfterThreadPoolWork (int status) override ;
4776
4752
4777
4753
private:
4778
4754
unsigned long error_; // NOLINT(runtime/int)
@@ -4782,21 +4758,17 @@ class RandomBytesRequest : public AsyncWrap {
4782
4758
};
4783
4759
4784
4760
4785
- void RandomBytesWork (uv_work_t * work_req) {
4786
- RandomBytesRequest* req =
4787
- ContainerOf (&RandomBytesRequest::work_req_, work_req);
4788
-
4761
+ void RandomBytesRequest::DoThreadPoolWork () {
4789
4762
// Ensure that OpenSSL's PRNG is properly seeded.
4790
4763
CheckEntropy ();
4791
4764
4792
- const int r = RAND_bytes (reinterpret_cast <unsigned char *>(req->data ()),
4793
- req->size ());
4765
+ const int r = RAND_bytes (reinterpret_cast <unsigned char *>(data_), size_);
4794
4766
4795
4767
// RAND_bytes() returns 0 on error.
4796
4768
if (r == 0 ) {
4797
- req-> set_error (ERR_get_error ()); // NOLINT(runtime/int)
4769
+ set_error (ERR_get_error ()); // NOLINT(runtime/int)
4798
4770
} else if (r == -1 ) {
4799
- req-> set_error (static_cast <unsigned long >(-1 )); // NOLINT(runtime/int)
4771
+ set_error (static_cast <unsigned long >(-1 )); // NOLINT(runtime/int)
4800
4772
}
4801
4773
}
4802
4774
@@ -4834,27 +4806,24 @@ void RandomBytesCheck(RandomBytesRequest* req, Local<Value> (*argv)[2]) {
4834
4806
}
4835
4807
4836
4808
4837
- void RandomBytesAfter (uv_work_t * work_req, int status) {
4838
- std::unique_ptr<RandomBytesRequest> req (
4839
- ContainerOf (&RandomBytesRequest::work_req_, work_req));
4840
- Environment* env = req->env ();
4841
- env->DecreaseWaitingRequestCounter ();
4809
+ void RandomBytesRequest::AfterThreadPoolWork (int status) {
4810
+ std::unique_ptr<RandomBytesRequest> req (this );
4842
4811
if (status == UV_ECANCELED)
4843
4812
return ;
4844
4813
CHECK_EQ (status, 0 );
4845
- HandleScope handle_scope (env->isolate ());
4846
- Context::Scope context_scope (env->context ());
4814
+ HandleScope handle_scope (env () ->isolate ());
4815
+ Context::Scope context_scope (env () ->context ());
4847
4816
Local<Value> argv[2 ];
4848
- RandomBytesCheck (req. get () , &argv);
4849
- req-> MakeCallback (env->ondone_string (), arraysize (argv), argv);
4817
+ RandomBytesCheck (this , &argv);
4818
+ MakeCallback (env () ->ondone_string (), arraysize (argv), argv);
4850
4819
}
4851
4820
4852
4821
4853
4822
void RandomBytesProcessSync (Environment* env,
4854
4823
std::unique_ptr<RandomBytesRequest> req,
4855
4824
Local<Value> (*argv)[2]) {
4856
4825
env->PrintSyncTrace ();
4857
- RandomBytesWork ( req->work_req () );
4826
+ req->DoThreadPoolWork ( );
4858
4827
RandomBytesCheck (req.get (), argv);
4859
4828
4860
4829
if (!(*argv)[0 ]->IsNull ())
@@ -4881,11 +4850,7 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
4881
4850
if (args[1 ]->IsFunction ()) {
4882
4851
obj->Set (env->context (), env->ondone_string (), args[1 ]).FromJust ();
4883
4852
4884
- env->IncreaseWaitingRequestCounter ();
4885
- uv_queue_work (env->event_loop (),
4886
- req.release ()->work_req (),
4887
- RandomBytesWork,
4888
- RandomBytesAfter);
4853
+ req.release ()->ScheduleWork ();
4889
4854
args.GetReturnValue ().Set (obj);
4890
4855
} else {
4891
4856
Local<Value> argv[2 ];
@@ -4921,11 +4886,7 @@ void RandomBytesBuffer(const FunctionCallbackInfo<Value>& args) {
4921
4886
if (args[3 ]->IsFunction ()) {
4922
4887
obj->Set (env->context (), env->ondone_string (), args[3 ]).FromJust ();
4923
4888
4924
- env->IncreaseWaitingRequestCounter ();
4925
- uv_queue_work (env->event_loop (),
4926
- req.release ()->work_req (),
4927
- RandomBytesWork,
4928
- RandomBytesAfter);
4889
+ req.release ()->ScheduleWork ();
4929
4890
args.GetReturnValue ().Set (obj);
4930
4891
} else {
4931
4892
Local<Value> argv[2 ];
0 commit comments