@@ -248,6 +248,11 @@ isc_netmgr_create(isc_mem_t *mctx, isc_loopmgr_t *loopmgr, isc_nm_t **netmgrp) {
248
248
249
249
isc_mem_attach (loop -> mctx , & worker -> mctx );
250
250
251
+ isc_mempool_create (worker -> mctx , sizeof (isc__nm_uvreq_t ),
252
+ & worker -> uvreq_pool );
253
+ isc_mempool_setfreemax (worker -> uvreq_pool ,
254
+ ISC_NM_REQS_STACK_SIZE );
255
+
251
256
isc_loop_attach (loop , & worker -> loop );
252
257
isc_loop_teardown (loop , networker_teardown , worker );
253
258
isc_refcount_init (& worker -> references , 1 );
@@ -596,7 +601,6 @@ nmsocket_cleanup(isc_nmsocket_t *sock) {
596
601
REQUIRE (!isc__nmsocket_active (sock ));
597
602
598
603
isc_nmhandle_t * handle = NULL ;
599
- isc__nm_uvreq_t * uvreq = NULL ;
600
604
isc__networker_t * worker = sock -> worker ;
601
605
602
606
isc_refcount_destroy (& sock -> references );
@@ -635,7 +639,8 @@ nmsocket_cleanup(isc_nmsocket_t *sock) {
635
639
isc___nmsocket_detach (& sock -> outer FLARG_PASS );
636
640
}
637
641
638
- while ((handle = isc_astack_pop (sock -> inactivehandles )) != NULL ) {
642
+ while ((handle = ISC_LIST_HEAD (sock -> inactive_handles )) != NULL ) {
643
+ ISC_LIST_DEQUEUE (sock -> inactive_handles , handle , inactive_link );
639
644
nmhandle_free (sock , handle );
640
645
}
641
646
@@ -645,14 +650,6 @@ nmsocket_cleanup(isc_nmsocket_t *sock) {
645
650
646
651
sock -> pquota = NULL ;
647
652
648
- isc_astack_destroy (sock -> inactivehandles );
649
-
650
- while ((uvreq = isc_astack_pop (sock -> inactivereqs )) != NULL ) {
651
- isc_mem_put (sock -> worker -> mctx , uvreq , sizeof (* uvreq ));
652
- }
653
-
654
- isc_astack_destroy (sock -> inactivereqs );
655
-
656
653
isc__nm_tls_cleanup_data (sock );
657
654
#if HAVE_LIBNGHTTP2
658
655
isc__nm_http_cleanup_data (sock );
@@ -855,10 +852,7 @@ isc___nmsocket_init(isc_nmsocket_t *sock, isc__networker_t *worker,
855
852
.type = type ,
856
853
.tid = worker -> loop -> tid ,
857
854
.fd = -1 ,
858
- .inactivehandles = isc_astack_new (worker -> mctx ,
859
- ISC_NM_HANDLES_STACK_SIZE ),
860
- .inactivereqs = isc_astack_new (worker -> mctx ,
861
- ISC_NM_REQS_STACK_SIZE ),
855
+ .inactive_handles = ISC_LIST_INITIALIZER ,
862
856
.result = ISC_R_UNSET ,
863
857
.active_handles = ISC_LIST_INITIALIZER ,
864
858
.active_link = ISC_LINK_INITIALIZER ,
@@ -988,27 +982,38 @@ alloc_handle(isc_nmsocket_t *sock) {
988
982
* handle = (isc_nmhandle_t ){
989
983
.magic = NMHANDLE_MAGIC ,
990
984
.active_link = ISC_LINK_INITIALIZER ,
985
+ .inactive_link = ISC_LINK_INITIALIZER ,
991
986
};
992
987
isc_refcount_init (& handle -> references , 1 );
993
988
994
989
return (handle );
995
990
}
996
991
992
+ static isc_nmhandle_t *
993
+ dequeue_handle (isc_nmsocket_t * sock ) {
994
+ #if !__SANITIZE_ADDRESS__ && !__SANITIZE_THREAD__
995
+ isc_nmhandle_t * handle = ISC_LIST_HEAD (sock -> inactive_handles );
996
+ if (handle != NULL ) {
997
+ ISC_LIST_DEQUEUE (sock -> inactive_handles , handle , inactive_link );
998
+
999
+ isc_refcount_init (& handle -> references , 1 );
1000
+ INSIST (VALID_NMHANDLE (handle ));
1001
+ return (handle );
1002
+ }
1003
+ #else
1004
+ UNUSED (sock );
1005
+ #endif /* !__SANITIZE_ADDRESS__ && !__SANITIZE_THREAD__ */
1006
+ return (NULL );
1007
+ }
1008
+
997
1009
isc_nmhandle_t *
998
1010
isc___nmhandle_get (isc_nmsocket_t * sock , isc_sockaddr_t const * peer ,
999
1011
isc_sockaddr_t const * local FLARG ) {
1000
- isc_nmhandle_t * handle = NULL ;
1001
-
1002
1012
REQUIRE (VALID_NMSOCK (sock ));
1003
1013
1004
- handle = isc_astack_pop (sock -> inactivehandles );
1005
-
1014
+ isc_nmhandle_t * handle = dequeue_handle (sock );
1006
1015
if (handle == NULL ) {
1007
1016
handle = alloc_handle (sock );
1008
- } else {
1009
- isc_refcount_init (& handle -> references , 1 );
1010
- INSIST (VALID_NMHANDLE (handle ));
1011
- ISC_LINK_INIT (handle , active_link );
1012
1017
}
1013
1018
1014
1019
NETMGR_TRACE_LOG (
@@ -1107,25 +1112,17 @@ nmhandle_free(isc_nmsocket_t *sock, isc_nmhandle_t *handle) {
1107
1112
1108
1113
static void
1109
1114
nmhandle_deactivate (isc_nmsocket_t * sock , isc_nmhandle_t * handle ) {
1110
- bool reuse = false ;
1111
- uint_fast32_t ah ;
1115
+ uint_fast32_t ah = atomic_fetch_sub ( & sock -> ah , 1 ) ;
1116
+ INSIST ( ah > 0 ) ;
1112
1117
1113
- /*
1114
- * We do all of this under lock to avoid races with socket
1115
- * destruction. We have to do this now, because at this point the
1116
- * socket is either unused or still attached to event->sock.
1117
- */
1118
1118
ISC_LIST_UNLINK (sock -> active_handles , handle , active_link );
1119
1119
1120
- ah = atomic_fetch_sub (& sock -> ah , 1 );
1121
- INSIST (ah > 0 );
1122
-
1123
1120
#if !__SANITIZE_ADDRESS__ && !__SANITIZE_THREAD__
1124
1121
if (atomic_load (& sock -> active )) {
1125
- reuse = isc_astack_trypush (sock -> inactivehandles , handle );
1126
- }
1122
+ ISC_LIST_APPEND (sock -> inactive_handles , handle , inactive_link );
1123
+ } else
1127
1124
#endif /* !__SANITIZE_ADDRESS__ && !__SANITIZE_THREAD__ */
1128
- if (! reuse ) {
1125
+ {
1129
1126
nmhandle_free (sock , handle );
1130
1127
}
1131
1128
}
@@ -1196,6 +1193,11 @@ nmhandle_detach_cb(isc_nmhandle_t **handlep FLARG) {
1196
1193
}
1197
1194
#endif
1198
1195
1196
+ if (handle == sock -> statichandle ) {
1197
+ /* statichandle is assigned, not attached. */
1198
+ sock -> statichandle = NULL ;
1199
+ }
1200
+
1199
1201
nmhandle_deactivate (sock , handle );
1200
1202
1201
1203
/*
@@ -1207,11 +1209,6 @@ nmhandle_detach_cb(isc_nmhandle_t **handlep FLARG) {
1207
1209
sock -> closehandle_cb (sock );
1208
1210
}
1209
1211
1210
- if (handle == sock -> statichandle ) {
1211
- /* statichandle is assigned, not attached. */
1212
- sock -> statichandle = NULL ;
1213
- }
1214
-
1215
1212
isc___nmsocket_detach (& sock FLARG_PASS );
1216
1213
}
1217
1214
@@ -1761,35 +1758,31 @@ isc___nm_uvreq_get(isc__networker_t *worker, isc_nmsocket_t *sock FLARG) {
1761
1758
1762
1759
REQUIRE (worker != NULL );
1763
1760
REQUIRE (VALID_NMSOCK (sock ));
1761
+ REQUIRE (sock -> tid == isc_tid ());
1764
1762
1765
- if (sock != NULL && isc__nmsocket_active (sock )) {
1766
- /* Try to reuse one */
1767
- req = isc_astack_pop (sock -> inactivereqs );
1768
- }
1769
-
1770
- if (req == NULL ) {
1771
- req = isc_mem_get (worker -> mctx , sizeof (* req ));
1772
- }
1773
-
1763
+ req = isc_mempool_get (worker -> uvreq_pool );
1774
1764
* req = (isc__nm_uvreq_t ){
1775
- .magic = 0 ,
1776
1765
.connect_tries = 3 ,
1766
+ .link = ISC_LINK_INITIALIZER ,
1767
+ .inactive_link = ISC_LINK_INITIALIZER ,
1768
+ .uv_req .req .data = req ,
1769
+ .magic = UVREQ_MAGIC ,
1777
1770
};
1778
- ISC_LINK_INIT (req , link );
1779
- req -> uv_req .req .data = req ;
1780
1771
isc___nmsocket_attach (sock , & req -> sock FLARG_PASS );
1781
- req -> magic = UVREQ_MAGIC ;
1782
1772
1783
1773
return (req );
1784
1774
}
1785
1775
1786
1776
void
1787
1777
isc___nm_uvreq_put (isc__nm_uvreq_t * * req0 , isc_nmsocket_t * sock FLARG ) {
1788
- isc__nm_uvreq_t * req = NULL ;
1789
- isc_nmhandle_t * handle = NULL ;
1790
-
1791
1778
REQUIRE (req0 != NULL );
1792
1779
REQUIRE (VALID_UVREQ (* req0 ));
1780
+ REQUIRE (VALID_NMSOCK (sock ));
1781
+ REQUIRE (sock -> tid == isc_tid ());
1782
+
1783
+ isc__nm_uvreq_t * req = NULL ;
1784
+ isc_nmhandle_t * handle = NULL ;
1785
+ isc__networker_t * worker = sock -> worker ;
1793
1786
1794
1787
req = * req0 ;
1795
1788
* req0 = NULL ;
@@ -1802,18 +1795,9 @@ isc___nm_uvreq_put(isc__nm_uvreq_t **req0, isc_nmsocket_t *sock FLARG) {
1802
1795
* We need to save this first to make sure that handle,
1803
1796
* sock, and the netmgr won't all disappear.
1804
1797
*/
1805
- handle = req -> handle ;
1806
- req -> handle = NULL ;
1798
+ ISC_SWAP (handle , req -> handle );
1807
1799
1808
- #if !__SANITIZE_ADDRESS__ && !__SANITIZE_THREAD__
1809
- if (!isc__nmsocket_active (sock ) ||
1810
- !isc_astack_trypush (sock -> inactivereqs , req ))
1811
- {
1812
- isc_mem_put (sock -> worker -> mctx , req , sizeof (* req ));
1813
- }
1814
- #else /* !__SANITIZE_ADDRESS__ && !__SANITIZE_THREAD__ */
1815
- isc_mem_put (sock -> worker -> mctx , req , sizeof (* req ));
1816
- #endif /* !__SANITIZE_ADDRESS__ && !__SANITIZE_THREAD__ */
1800
+ isc_mempool_put (worker -> uvreq_pool , req );
1817
1801
1818
1802
if (handle != NULL ) {
1819
1803
isc__nmhandle_detach (& handle FLARG_PASS );
@@ -2633,6 +2617,8 @@ isc__networker_destroy(isc__networker_t *worker) {
2633
2617
2634
2618
isc_loop_detach (& worker -> loop );
2635
2619
2620
+ isc_mempool_destroy (& worker -> uvreq_pool );
2621
+
2636
2622
isc_mem_put (worker -> mctx , worker -> sendbuf , ISC_NETMGR_SENDBUF_SIZE );
2637
2623
isc_mem_putanddetach (& worker -> mctx , worker -> recvbuf ,
2638
2624
ISC_NETMGR_RECVBUF_SIZE );
0 commit comments