@@ -71,8 +71,6 @@ typedef struct {
71
71
int active_threads_cnt ;
72
72
int pool ;
73
73
char extra_thread ;
74
- int threads_limit ;
75
- char is_strict_limit ;
76
74
char notify_on_begin ;
77
75
int extra_threads_cnt ;
78
76
int busy_threads ;
@@ -86,6 +84,7 @@ typedef struct {
86
84
char * host ;
87
85
char * service ;
88
86
struct addrinfo * hints ;
87
+ char extra ;
89
88
char queued ;
90
89
DNS_result * res ;
91
90
} DNS_thread_arg ;
@@ -127,7 +126,7 @@ void *DNS_getaddrinfo(void *v_arg) {
127
126
128
127
pthread_mutex_lock (& self -> mutex );
129
128
arg -> res -> arg = arg ;
130
- if (! queued ) self -> extra_threads_cnt -- ;
129
+ if (arg -> extra ) self -> extra_threads_cnt -- ;
131
130
write (arg -> res -> fd1 , "2" , 1 );
132
131
if (!queued ) DNS_on_thread_finish (self );
133
132
pthread_mutex_unlock (& self -> mutex );
@@ -348,8 +347,6 @@ new(char* class, ...)
348
347
349
348
int i , rc ;
350
349
self -> pool = 0 ;
351
- self -> threads_limit = 0 ;
352
- self -> is_strict_limit = 0 ;
353
350
self -> notify_on_begin = 0 ;
354
351
self -> extra_thread = 0 ;
355
352
self -> active_threads_cnt = 0 ;
@@ -372,10 +369,6 @@ new(char* class, ...)
372
369
else if (strEQ (opt , "extra_thread" )) {
373
370
self -> extra_thread = SvIV (ST (i + 1 ));
374
371
}
375
- else if (strEQ (opt , "threads_limit" ) || strEQ (opt , "threads_strict_limit" )) {
376
- self -> threads_limit = SvIV (ST (i + 1 ));
377
- self -> is_strict_limit = strEQ (opt , "threads_strict_limit" );
378
- }
379
372
else if (strEQ (opt , "notify_on_begin" )) {
380
373
self -> notify_on_begin = SvIV (ST (i + 1 ));
381
374
}
@@ -424,34 +417,31 @@ new(char* class, ...)
424
417
#endif
425
418
}
426
419
427
- if (self -> pool || self -> threads_limit ) {
420
+ if (self -> pool ) {
428
421
if (sem_init (& self -> semaphore , 0 , 0 ) != 0 ) {
429
422
warn ("sem_init(): %s" , strerror (errno ));
430
423
goto FAIL ;
431
424
}
432
425
sem_ok = 1 ;
433
426
434
- if (self -> pool ) {
435
- pthread_t tid ;
436
- int j = 0 ;
437
- for (i = 0 ; i < self -> pool ; i ++ ) {
438
- rc = pthread_create (& tid , & self -> thread_attrs , DNS_pool_worker , (void * )self );
439
- if (rc == 0 ) {
440
- self -> active_threads_cnt ++ ;
441
- j ++ ;
442
- }
443
- else {
444
- warn ("Can't create thread #%d: %s" , i + 1 , strerror (rc ));
445
- }
427
+ pthread_t tid ;
428
+ int j = 0 ;
429
+ for (i = 0 ; i < self -> pool ; i ++ ) {
430
+ rc = pthread_create (& tid , & self -> thread_attrs , DNS_pool_worker , (void * )self );
431
+ if (rc == 0 ) {
432
+ self -> active_threads_cnt ++ ;
433
+ j ++ ;
446
434
}
447
-
448
- if (j == 0 ) {
449
- goto FAIL ;
435
+ else {
436
+ warn ("Can't create thread #%d: %s" , i + 1 , strerror (rc ));
450
437
}
451
-
452
- self -> pool = j ;
453
438
}
454
439
440
+ if (j == 0 ) {
441
+ goto FAIL ;
442
+ }
443
+
444
+ self -> pool = j ;
455
445
self -> in_queue = queue_new ();
456
446
}
457
447
@@ -548,48 +538,36 @@ _getaddrinfo(Net_DNS_Native *self, char *host, SV* sv_service, SV* sv_hints, int
548
538
arg -> host = strlen (host ) ? savepv (host ) : NULL ;
549
539
arg -> service = strlen (service ) ? savepv (service ) : NULL ;
550
540
arg -> hints = hints ;
541
+ arg -> extra = 0 ;
551
542
arg -> queued = 0 ;
552
543
arg -> res = res ;
553
544
554
545
pthread_mutex_lock (& self -> mutex );
555
546
DNS_free_timedout (self , 0 );
556
547
bstree_put (self -> fd_map , fd [0 ], res );
557
- char allow_extra_worker = 1 ;
558
- if (self -> threads_limit && self -> active_threads_cnt == (self -> is_strict_limit ? self -> threads_limit : self -> threads_limit + queue_size (self -> tout_queue ))) {
559
- allow_extra_worker = 0 ;
560
- }
561
-
562
548
if (self -> pool ) {
563
- if (allow_extra_worker && self -> busy_threads == self -> pool && (self -> extra_thread || queue_size (self -> tout_queue ) > self -> extra_threads_cnt )) {
549
+ if (self -> busy_threads == self -> pool && (self -> extra_thread || queue_size (self -> tout_queue ) > self -> extra_threads_cnt )) {
550
+ arg -> extra = 1 ;
564
551
self -> extra_threads_cnt ++ ;
565
552
}
566
553
else {
567
554
arg -> queued = 1 ;
568
- allow_extra_worker = 0 ;
555
+ queue_push (self -> in_queue , arg );
556
+ sem_post (& self -> semaphore );
569
557
}
570
558
}
571
-
572
- if (arg -> queued || self -> threads_limit ) {
573
- arg -> queued = 1 ;
574
- queue_push (self -> in_queue , arg );
575
- sem_post (& self -> semaphore );
576
- }
577
559
pthread_mutex_unlock (& self -> mutex );
578
560
579
- if (allow_extra_worker ) {
561
+ if (! self -> pool || arg -> extra ) {
580
562
pthread_t tid ;
581
563
582
564
pthread_mutex_lock (& self -> mutex );
583
- ++ self -> active_threads_cnt ;
584
- pthread_mutex_unlock (& self -> mutex );
585
-
586
- int rc = self -> threads_limit ?
587
- pthread_create (& tid , & self -> thread_attrs , DNS_extra_worker , (void * )self ) :
588
- pthread_create (& tid , & self -> thread_attrs , DNS_getaddrinfo , (void * )arg );
589
-
590
- if (rc != 0 ) {
591
- pthread_mutex_lock (& self -> mutex );
592
- self -> active_threads_cnt -- ;
565
+ int rc = pthread_create (& tid , & self -> thread_attrs , DNS_getaddrinfo , (void * )arg );
566
+ if (rc == 0 ) {
567
+ ++ self -> active_threads_cnt ;
568
+ pthread_mutex_unlock (& self -> mutex );
569
+ }
570
+ else {
593
571
pthread_mutex_unlock (& self -> mutex );
594
572
if (arg -> host ) Safefree (arg -> host );
595
573
if (arg -> service ) Safefree (arg -> service );
0 commit comments