Skip to content

Commit 531df96

Browse files
committedNov 27, 2023
Revert "New threads_limit and threads_strict_limit options to limit total threads running"
This reverts commit 7076f00.
1 parent 6df9fc2 commit 531df96

File tree

1 file changed

+29
-51
lines changed

1 file changed

+29
-51
lines changed
 

‎Native.xs

+29-51
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ typedef struct {
7171
int active_threads_cnt;
7272
int pool;
7373
char extra_thread;
74-
int threads_limit;
75-
char is_strict_limit;
7674
char notify_on_begin;
7775
int extra_threads_cnt;
7876
int busy_threads;
@@ -86,6 +84,7 @@ typedef struct {
8684
char *host;
8785
char *service;
8886
struct addrinfo *hints;
87+
char extra;
8988
char queued;
9089
DNS_result *res;
9190
} DNS_thread_arg;
@@ -127,7 +126,7 @@ void *DNS_getaddrinfo(void *v_arg) {
127126

128127
pthread_mutex_lock(&self->mutex);
129128
arg->res->arg = arg;
130-
if (!queued) self->extra_threads_cnt--;
129+
if (arg->extra) self->extra_threads_cnt--;
131130
write(arg->res->fd1, "2", 1);
132131
if (!queued) DNS_on_thread_finish(self);
133132
pthread_mutex_unlock(&self->mutex);
@@ -348,8 +347,6 @@ new(char* class, ...)
348347

349348
int i, rc;
350349
self->pool = 0;
351-
self->threads_limit = 0;
352-
self->is_strict_limit = 0;
353350
self->notify_on_begin = 0;
354351
self->extra_thread = 0;
355352
self->active_threads_cnt = 0;
@@ -372,10 +369,6 @@ new(char* class, ...)
372369
else if (strEQ(opt, "extra_thread")) {
373370
self->extra_thread = SvIV(ST(i+1));
374371
}
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-
}
379372
else if (strEQ(opt, "notify_on_begin")) {
380373
self->notify_on_begin = SvIV(ST(i+1));
381374
}
@@ -424,34 +417,31 @@ new(char* class, ...)
424417
#endif
425418
}
426419

427-
if (self->pool || self->threads_limit) {
420+
if (self->pool) {
428421
if (sem_init(&self->semaphore, 0, 0) != 0) {
429422
warn("sem_init(): %s", strerror(errno));
430423
goto FAIL;
431424
}
432425
sem_ok = 1;
433426

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++;
446434
}
447-
448-
if (j == 0) {
449-
goto FAIL;
435+
else {
436+
warn("Can't create thread #%d: %s", i+1, strerror(rc));
450437
}
451-
452-
self->pool = j;
453438
}
454439

440+
if (j == 0) {
441+
goto FAIL;
442+
}
443+
444+
self->pool = j;
455445
self->in_queue = queue_new();
456446
}
457447

@@ -548,48 +538,36 @@ _getaddrinfo(Net_DNS_Native *self, char *host, SV* sv_service, SV* sv_hints, int
548538
arg->host = strlen(host) ? savepv(host) : NULL;
549539
arg->service = strlen(service) ? savepv(service) : NULL;
550540
arg->hints = hints;
541+
arg->extra = 0;
551542
arg->queued = 0;
552543
arg->res = res;
553544

554545
pthread_mutex_lock(&self->mutex);
555546
DNS_free_timedout(self, 0);
556547
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-
562548
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;
564551
self->extra_threads_cnt++;
565552
}
566553
else {
567554
arg->queued = 1;
568-
allow_extra_worker = 0;
555+
queue_push(self->in_queue, arg);
556+
sem_post(&self->semaphore);
569557
}
570558
}
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-
}
577559
pthread_mutex_unlock(&self->mutex);
578560

579-
if (allow_extra_worker) {
561+
if (!self->pool || arg->extra) {
580562
pthread_t tid;
581563

582564
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 {
593571
pthread_mutex_unlock(&self->mutex);
594572
if (arg->host) Safefree(arg->host);
595573
if (arg->service) Safefree(arg->service);

0 commit comments

Comments
 (0)
Please sign in to comment.