@@ -4562,7 +4562,7 @@ bool ECDH::IsKeyPairValid() {
4562
4562
}
4563
4563
4564
4564
4565
- class PBKDF2Request : public AsyncWrap {
4565
+ class PBKDF2Request : public AsyncWrap , public ThreadPoolWork {
4566
4566
public:
4567
4567
PBKDF2Request (Environment* env,
4568
4568
Local<Object> object,
@@ -4572,6 +4572,7 @@ class PBKDF2Request : public AsyncWrap {
4572
4572
int keylen,
4573
4573
int iteration_count)
4574
4574
: AsyncWrap(env, object, AsyncWrap::PROVIDER_PBKDF2REQUEST),
4575
+ ThreadPoolWork (env),
4575
4576
digest_(digest),
4576
4577
success_(false ),
4577
4578
pass_(std::move(pass)),
@@ -4580,21 +4581,14 @@ class PBKDF2Request : public AsyncWrap {
4580
4581
iteration_count_(iteration_count) {
4581
4582
}
4582
4583
4583
- uv_work_t * work_req () {
4584
- return &work_req_;
4585
- }
4586
-
4587
4584
size_t self_size () const override { return sizeof (*this ); }
4588
4585
4589
- static void Work ( uv_work_t * work_req) ;
4590
- void Work () ;
4586
+ void DoThreadPoolWork () override ;
4587
+ void AfterThreadPoolWork ( int status) override ;
4591
4588
4592
- static void After (uv_work_t * work_req, int status);
4593
4589
void After (Local<Value> (*argv)[2]);
4594
- void After ();
4595
4590
4596
4591
private:
4597
- uv_work_t work_req_;
4598
4592
const EVP_MD* digest_;
4599
4593
bool success_;
4600
4594
MallocedBuffer<char > pass_;
@@ -4604,7 +4598,7 @@ class PBKDF2Request : public AsyncWrap {
4604
4598
};
4605
4599
4606
4600
4607
- void PBKDF2Request::Work () {
4601
+ void PBKDF2Request::DoThreadPoolWork () {
4608
4602
success_ =
4609
4603
PKCS5_PBKDF2_HMAC (
4610
4604
pass_.data , pass_.size ,
@@ -4617,12 +4611,6 @@ void PBKDF2Request::Work() {
4617
4611
}
4618
4612
4619
4613
4620
- void PBKDF2Request::Work (uv_work_t * work_req) {
4621
- PBKDF2Request* req = ContainerOf (&PBKDF2Request::work_req_, work_req);
4622
- req->Work ();
4623
- }
4624
-
4625
-
4626
4614
void PBKDF2Request::After (Local<Value> (*argv)[2]) {
4627
4615
if (success_) {
4628
4616
(*argv)[0 ] = Null (env ()->isolate ());
@@ -4635,7 +4623,12 @@ void PBKDF2Request::After(Local<Value> (*argv)[2]) {
4635
4623
}
4636
4624
4637
4625
4638
- void PBKDF2Request::After () {
4626
+ void PBKDF2Request::AfterThreadPoolWork (int status) {
4627
+ std::unique_ptr<PBKDF2Request> req (this );
4628
+ if (status == UV_ECANCELED)
4629
+ return ;
4630
+ CHECK_EQ (status, 0 );
4631
+
4639
4632
HandleScope handle_scope (env ()->isolate ());
4640
4633
Context::Scope context_scope (env ()->context ());
4641
4634
Local<Value> argv[2 ];
@@ -4644,17 +4637,6 @@ void PBKDF2Request::After() {
4644
4637
}
4645
4638
4646
4639
4647
- void PBKDF2Request::After (uv_work_t * work_req, int status) {
4648
- std::unique_ptr<PBKDF2Request> req (
4649
- ContainerOf (&PBKDF2Request::work_req_, work_req));
4650
- req->env ()->DecreaseWaitingRequestCounter ();
4651
- if (status == UV_ECANCELED)
4652
- return ;
4653
- CHECK_EQ (status, 0 );
4654
- req->After ();
4655
- }
4656
-
4657
-
4658
4640
void PBKDF2 (const FunctionCallbackInfo<Value>& args) {
4659
4641
Environment* env = Environment::GetCurrent (args);
4660
4642
@@ -4701,14 +4683,10 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
4701
4683
if (args[5 ]->IsFunction ()) {
4702
4684
obj->Set (env->context (), env->ondone_string (), args[5 ]).FromJust ();
4703
4685
4704
- env->IncreaseWaitingRequestCounter ();
4705
- uv_queue_work (env->event_loop (),
4706
- req.release ()->work_req (),
4707
- PBKDF2Request::Work,
4708
- PBKDF2Request::After);
4686
+ req.release ()->ScheduleWork ();
4709
4687
} else {
4710
4688
env->PrintSyncTrace ();
4711
- req->Work ();
4689
+ req->DoThreadPoolWork ();
4712
4690
Local<Value> argv[2 ];
4713
4691
req->After (&argv);
4714
4692
@@ -4721,7 +4699,7 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
4721
4699
4722
4700
4723
4701
// Only instantiate within a valid HandleScope.
4724
- class RandomBytesRequest : public AsyncWrap {
4702
+ class RandomBytesRequest : public AsyncWrap , public ThreadPoolWork {
4725
4703
public:
4726
4704
enum FreeMode { FREE_DATA, DONT_FREE_DATA };
4727
4705
@@ -4731,16 +4709,13 @@ class RandomBytesRequest : public AsyncWrap {
4731
4709
char * data,
4732
4710
FreeMode free_mode)
4733
4711
: AsyncWrap(env, object, AsyncWrap::PROVIDER_RANDOMBYTESREQUEST),
4712
+ ThreadPoolWork (env),
4734
4713
error_(0 ),
4735
4714
size_(size),
4736
4715
data_(data),
4737
4716
free_mode_(free_mode) {
4738
4717
}
4739
4718
4740
- uv_work_t * work_req () {
4741
- return &work_req_;
4742
- }
4743
-
4744
4719
inline size_t size () const {
4745
4720
return size_;
4746
4721
}
@@ -4778,7 +4753,8 @@ class RandomBytesRequest : public AsyncWrap {
4778
4753
4779
4754
size_t self_size () const override { return sizeof (*this ); }
4780
4755
4781
- uv_work_t work_req_;
4756
+ void DoThreadPoolWork () override ;
4757
+ void AfterThreadPoolWork (int status) override ;
4782
4758
4783
4759
private:
4784
4760
unsigned long error_; // NOLINT(runtime/int)
@@ -4788,21 +4764,17 @@ class RandomBytesRequest : public AsyncWrap {
4788
4764
};
4789
4765
4790
4766
4791
- void RandomBytesWork (uv_work_t * work_req) {
4792
- RandomBytesRequest* req =
4793
- ContainerOf (&RandomBytesRequest::work_req_, work_req);
4794
-
4767
+ void RandomBytesRequest::DoThreadPoolWork () {
4795
4768
// Ensure that OpenSSL's PRNG is properly seeded.
4796
4769
CheckEntropy ();
4797
4770
4798
- const int r = RAND_bytes (reinterpret_cast <unsigned char *>(req->data ()),
4799
- req->size ());
4771
+ const int r = RAND_bytes (reinterpret_cast <unsigned char *>(data_), size_);
4800
4772
4801
4773
// RAND_bytes() returns 0 on error.
4802
4774
if (r == 0 ) {
4803
- req-> set_error (ERR_get_error ()); // NOLINT(runtime/int)
4775
+ set_error (ERR_get_error ()); // NOLINT(runtime/int)
4804
4776
} else if (r == -1 ) {
4805
- req-> set_error (static_cast <unsigned long >(-1 )); // NOLINT(runtime/int)
4777
+ set_error (static_cast <unsigned long >(-1 )); // NOLINT(runtime/int)
4806
4778
}
4807
4779
}
4808
4780
@@ -4840,27 +4812,24 @@ void RandomBytesCheck(RandomBytesRequest* req, Local<Value> (*argv)[2]) {
4840
4812
}
4841
4813
4842
4814
4843
- void RandomBytesAfter (uv_work_t * work_req, int status) {
4844
- std::unique_ptr<RandomBytesRequest> req (
4845
- ContainerOf (&RandomBytesRequest::work_req_, work_req));
4846
- Environment* env = req->env ();
4847
- env->DecreaseWaitingRequestCounter ();
4815
+ void RandomBytesRequest::AfterThreadPoolWork (int status) {
4816
+ std::unique_ptr<RandomBytesRequest> req (this );
4848
4817
if (status == UV_ECANCELED)
4849
4818
return ;
4850
4819
CHECK_EQ (status, 0 );
4851
- HandleScope handle_scope (env->isolate ());
4852
- Context::Scope context_scope (env->context ());
4820
+ HandleScope handle_scope (env () ->isolate ());
4821
+ Context::Scope context_scope (env () ->context ());
4853
4822
Local<Value> argv[2 ];
4854
- RandomBytesCheck (req. get () , &argv);
4855
- req-> MakeCallback (env->ondone_string (), arraysize (argv), argv);
4823
+ RandomBytesCheck (this , &argv);
4824
+ MakeCallback (env () ->ondone_string (), arraysize (argv), argv);
4856
4825
}
4857
4826
4858
4827
4859
4828
void RandomBytesProcessSync (Environment* env,
4860
4829
std::unique_ptr<RandomBytesRequest> req,
4861
4830
Local<Value> (*argv)[2]) {
4862
4831
env->PrintSyncTrace ();
4863
- RandomBytesWork ( req->work_req () );
4832
+ req->DoThreadPoolWork ( );
4864
4833
RandomBytesCheck (req.get (), argv);
4865
4834
4866
4835
if (!(*argv)[0 ]->IsNull ())
@@ -4887,11 +4856,7 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
4887
4856
if (args[1 ]->IsFunction ()) {
4888
4857
obj->Set (env->context (), env->ondone_string (), args[1 ]).FromJust ();
4889
4858
4890
- env->IncreaseWaitingRequestCounter ();
4891
- uv_queue_work (env->event_loop (),
4892
- req.release ()->work_req (),
4893
- RandomBytesWork,
4894
- RandomBytesAfter);
4859
+ req.release ()->ScheduleWork ();
4895
4860
args.GetReturnValue ().Set (obj);
4896
4861
} else {
4897
4862
Local<Value> argv[2 ];
@@ -4927,11 +4892,7 @@ void RandomBytesBuffer(const FunctionCallbackInfo<Value>& args) {
4927
4892
if (args[3 ]->IsFunction ()) {
4928
4893
obj->Set (env->context (), env->ondone_string (), args[3 ]).FromJust ();
4929
4894
4930
- env->IncreaseWaitingRequestCounter ();
4931
- uv_queue_work (env->event_loop (),
4932
- req.release ()->work_req (),
4933
- RandomBytesWork,
4934
- RandomBytesAfter);
4895
+ req.release ()->ScheduleWork ();
4935
4896
args.GetReturnValue ().Set (obj);
4936
4897
} else {
4937
4898
Local<Value> argv[2 ];
0 commit comments