Skip to content
This repository was archived by the owner on Aug 13, 2025. It is now read-only.

Commit 2037b1d

Browse files
committed
Merge branch 'ondrej/remove-udp-and-tcp-listen-lock' into 'main'
Get rid of locking during UDP and TCP listen See merge request isc-projects/bind9!7343
2 parents 7900cbd + d06602f commit 2037b1d

File tree

3 files changed

+48
-72
lines changed

3 files changed

+48
-72
lines changed

lib/isc/netmgr/netmgr.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,6 @@ nmsocket_cleanup(isc_nmsocket_t *sock) {
663663

664664
sock->magic = 0;
665665

666-
isc_mutex_destroy(&sock->lock);
667-
668666
/* Don't free child socket */
669667
if (sock->parent == NULL) {
670668
REQUIRE(sock->tid == isc_tid());
@@ -858,8 +856,6 @@ isc___nmsocket_init(isc_nmsocket_t *sock, isc__networker_t *worker,
858856
.active_link = ISC_LINK_INITIALIZER,
859857
};
860858

861-
isc_mutex_init(&sock->lock);
862-
863859
if (iface != NULL) {
864860
family = iface->type.sa.sa_family;
865861
sock->iface = *iface;

lib/isc/netmgr/tcp.c

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -417,20 +417,23 @@ isc_nm_listentcp(isc_nm_t *mgr, uint32_t workers, isc_sockaddr_t *iface,
417417
fd = isc__nm_tcp_lb_socket(mgr, iface->type.sa.sa_family);
418418
}
419419

420+
start_tcp_child(mgr, iface, sock, fd, 0);
421+
result = sock->children[0].result;
422+
INSIST(result != ISC_R_UNSET);
423+
420424
for (size_t i = 1; i < sock->nchildren; i++) {
421425
start_tcp_child(mgr, iface, sock, fd, i);
422426
}
423427

424-
start_tcp_child(mgr, iface, sock, fd, 0);
428+
isc_barrier_wait(&sock->listen_barrier);
425429

426430
if (!mgr->load_balance_sockets) {
427431
isc__nm_closesocket(fd);
428432
}
429433

430-
LOCK(&sock->lock);
431-
result = sock->result;
432-
UNLOCK(&sock->lock);
433-
INSIST(result != ISC_R_UNSET);
434+
for (size_t i = 1; i < sock->nchildren; i++) {
435+
INSIST(result == sock->children[i].result);
436+
}
434437

435438
atomic_store(&sock->active, true);
436439

@@ -457,14 +460,12 @@ isc__nm_async_tcplisten(isc__networker_t *worker, isc__netievent_t *ev0) {
457460
isc_result_t result = ISC_R_UNSET;
458461

459462
REQUIRE(VALID_NMSOCK(ievent->sock));
460-
REQUIRE(ievent->sock->tid == isc_tid());
461463
REQUIRE(VALID_NMSOCK(ievent->sock->parent));
462464

463465
sock = ievent->sock;
464466
sa_family = sock->iface.type.sa.sa_family;
465467

466468
REQUIRE(sock->type == isc_nm_tcpsocket);
467-
REQUIRE(sock->parent != NULL);
468469
REQUIRE(sock->tid == isc_tid());
469470

470471
(void)isc__nm_socket_min_mtu(sock->fd, sa_family);
@@ -499,25 +500,17 @@ isc__nm_async_tcplisten(isc__networker_t *worker, isc__netievent_t *ev0) {
499500
isc__nm_incstats(sock, STATID_BINDFAIL);
500501
goto done;
501502
}
502-
} else {
503-
LOCK(&sock->parent->lock);
504-
if (sock->parent->fd == -1) {
505-
r = isc__nm_tcp_freebind(&sock->uv_handle.tcp,
506-
&sock->iface.type.sa, flags);
507-
if (r < 0) {
508-
isc__nm_incstats(sock, STATID_BINDFAIL);
509-
UNLOCK(&sock->parent->lock);
510-
goto done;
511-
}
512-
sock->parent->uv_handle.tcp.flags =
513-
sock->uv_handle.tcp.flags;
514-
sock->parent->fd = sock->fd;
515-
} else {
516-
/* The socket is already bound, just copy the flags */
517-
sock->uv_handle.tcp.flags =
518-
sock->parent->uv_handle.tcp.flags;
503+
} else if (sock->tid == 0) {
504+
r = isc__nm_tcp_freebind(&sock->uv_handle.tcp,
505+
&sock->iface.type.sa, flags);
506+
if (r < 0) {
507+
isc__nm_incstats(sock, STATID_BINDFAIL);
508+
goto done;
519509
}
520-
UNLOCK(&sock->parent->lock);
510+
sock->parent->uv_handle.tcp.flags = sock->uv_handle.tcp.flags;
511+
} else {
512+
/* The socket is already bound, just copy the flags */
513+
sock->uv_handle.tcp.flags = sock->parent->uv_handle.tcp.flags;
521514
}
522515

523516
isc__nm_set_network_buffers(sock->worker->netmgr,
@@ -546,16 +539,13 @@ isc__nm_async_tcplisten(isc__networker_t *worker, isc__netievent_t *ev0) {
546539
sock->pquota = NULL;
547540
}
548541

549-
LOCK(&sock->parent->lock);
550-
if (sock->parent->result == ISC_R_UNSET) {
551-
sock->parent->result = result;
552-
} else {
553-
REQUIRE(sock->parent->result == result);
554-
}
555-
UNLOCK(&sock->parent->lock);
542+
sock->result = result;
556543

557544
REQUIRE(!worker->loop->paused);
558-
isc_barrier_wait(&sock->parent->listen_barrier);
545+
546+
if (sock->tid != 0) {
547+
isc_barrier_wait(&sock->parent->listen_barrier);
548+
}
559549
}
560550

561551
static void

lib/isc/netmgr/udp.c

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,23 @@ isc_nm_listenudp(isc_nm_t *mgr, uint32_t workers, isc_sockaddr_t *iface,
163163
fd = isc__nm_udp_lb_socket(mgr, iface->type.sa.sa_family);
164164
}
165165

166+
start_udp_child(mgr, iface, sock, fd, 0);
167+
result = sock->children[0].result;
168+
INSIST(result != ISC_R_UNSET);
169+
166170
for (size_t i = 1; i < sock->nchildren; i++) {
167171
start_udp_child(mgr, iface, sock, fd, i);
168172
}
169173

170-
start_udp_child(mgr, iface, sock, fd, 0);
174+
isc_barrier_wait(&sock->listen_barrier);
171175

172176
if (!mgr->load_balance_sockets) {
173177
isc__nm_closesocket(fd);
174178
}
175179

176-
LOCK(&sock->lock);
177-
result = sock->result;
178-
UNLOCK(&sock->lock);
179-
INSIST(result != ISC_R_UNSET);
180+
for (size_t i = 1; i < sock->nchildren; i++) {
181+
INSIST(result == sock->children[i].result);
182+
}
180183

181184
atomic_store(&sock->active, true);
182185

@@ -332,15 +335,13 @@ isc__nm_async_udplisten(isc__networker_t *worker, isc__netievent_t *ev0) {
332335
isc_nm_t *mgr = NULL;
333336

334337
REQUIRE(VALID_NMSOCK(ievent->sock));
335-
REQUIRE(ievent->sock->tid == isc_tid());
336338
REQUIRE(VALID_NMSOCK(ievent->sock->parent));
337339

338340
sock = ievent->sock;
339341
sa_family = sock->iface.type.sa.sa_family;
340342
mgr = sock->worker->netmgr;
341343

342344
REQUIRE(sock->type == isc_nm_udpsocket);
343-
REQUIRE(sock->parent != NULL);
344345
REQUIRE(sock->tid == isc_tid());
345346

346347
(void)isc__nm_socket_min_mtu(sock->fd, sa_family);
@@ -379,27 +380,19 @@ isc__nm_async_udplisten(isc__networker_t *worker, isc__netievent_t *ev0) {
379380
isc__nm_incstats(sock, STATID_BINDFAIL);
380381
goto done;
381382
}
382-
} else {
383-
LOCK(&sock->parent->lock);
384-
if (sock->parent->fd == -1) {
385-
/* This thread is first, bind the socket */
386-
r = isc__nm_udp_freebind(&sock->uv_handle.udp,
387-
&sock->parent->iface.type.sa,
388-
uv_bind_flags);
389-
if (r < 0) {
390-
isc__nm_incstats(sock, STATID_BINDFAIL);
391-
UNLOCK(&sock->parent->lock);
392-
goto done;
393-
}
394-
sock->parent->uv_handle.udp.flags =
395-
sock->uv_handle.udp.flags;
396-
sock->parent->fd = sock->fd;
397-
} else {
398-
/* The socket is already bound, just copy the flags */
399-
sock->uv_handle.udp.flags =
400-
sock->parent->uv_handle.udp.flags;
383+
} else if (sock->tid == 0) {
384+
/* This thread is first, bind the socket */
385+
r = isc__nm_udp_freebind(&sock->uv_handle.udp,
386+
&sock->parent->iface.type.sa,
387+
uv_bind_flags);
388+
if (r < 0) {
389+
isc__nm_incstats(sock, STATID_BINDFAIL);
390+
goto done;
401391
}
402-
UNLOCK(&sock->parent->lock);
392+
sock->parent->uv_handle.udp.flags = sock->uv_handle.udp.flags;
393+
} else {
394+
/* The socket is already bound, just copy the flags */
395+
sock->uv_handle.udp.flags = sock->parent->uv_handle.udp.flags;
403396
}
404397

405398
isc__nm_set_network_buffers(mgr, &sock->uv_handle.handle);
@@ -417,16 +410,13 @@ isc__nm_async_udplisten(isc__networker_t *worker, isc__netievent_t *ev0) {
417410
result = isc_uverr2result(r);
418411
atomic_fetch_add(&sock->parent->rchildren, 1);
419412

420-
LOCK(&sock->parent->lock);
421-
if (sock->parent->result == ISC_R_UNSET) {
422-
sock->parent->result = result;
423-
} else {
424-
REQUIRE(sock->parent->result == result);
425-
}
426-
UNLOCK(&sock->parent->lock);
413+
sock->result = result;
427414

428415
REQUIRE(!worker->loop->paused);
429-
isc_barrier_wait(&sock->parent->listen_barrier);
416+
417+
if (sock->tid != 0) {
418+
isc_barrier_wait(&sock->parent->listen_barrier);
419+
}
430420
}
431421

432422
static void

0 commit comments

Comments
 (0)