@@ -47,12 +47,6 @@ typedef struct cat_curl_multi_context_s {
4747 cat_curl_multi_event_t * events ;
4848} cat_curl_multi_context_t ;
4949
50- typedef struct cat_curl_multi_socket_context_s {
51- cat_curl_multi_context_t * context ;
52- curl_socket_t sockfd ;
53- uv_poll_t poll ;
54- } cat_curl_multi_socket_context_t ;
55-
5650RB_HEAD (cat_curl_multi_context_tree_s , cat_curl_multi_context_s );
5751
5852static int cat_curl__multi_context_compare (cat_curl_multi_context_t * c1 , cat_curl_multi_context_t * c2 )
@@ -72,10 +66,19 @@ RB_GENERATE_STATIC(cat_curl_multi_context_tree_s,
7266 cat_curl_multi_context_s , tree_entry ,
7367 cat_curl__multi_context_compare );
7468
69+
70+ typedef struct cat_curl_multi_socket_context_s {
71+ cat_queue_node_t node ;
72+ cat_curl_multi_context_t * context ;
73+ curl_socket_t sockfd ;
74+ uv_poll_t poll ;
75+ } cat_curl_multi_socket_context_t ;
76+
7577/* globals */
7678
7779CAT_GLOBALS_STRUCT_BEGIN (cat_curl ) {
7880 struct cat_curl_multi_context_tree_s multi_tree ;
81+ cat_queue_t socket_contexts ;
7982} CAT_GLOBALS_STRUCT_END (cat_curl );
8083
8184CAT_GLOBALS_DECLARE (cat_curl );
@@ -141,6 +144,13 @@ static void cat_curl_multi_socket_context_close_callback(uv_handle_t *handle)
141144 cat_free (socket_context );
142145}
143146
147+ static cat_always_inline void cat_curl_multi_socket_context_close (cat_curl_multi_socket_context_t * socket_context )
148+ {
149+ cat_queue_remove (& socket_context -> node );
150+ (void ) uv_poll_stop (& socket_context -> poll );
151+ uv_close ((uv_handle_t * ) & socket_context -> poll , cat_curl_multi_socket_context_close_callback );
152+ }
153+
144154static cat_always_inline void cat_curl_multi_socket_schedule (cat_curl_multi_context_t * context , curl_socket_t sockfd , int action )
145155{
146156 cat_bool_t found = cat_false ;
@@ -176,7 +186,7 @@ static void cat_curl_multi_socket_poll_callback(uv_poll_t *poll, int status, int
176186
177187 if (unlikely (context -> waiter == NULL )) {
178188 // no one cares about this event, stop polling
179- uv_poll_stop (& socket_context -> poll );
189+ ( void ) uv_poll_stop (& socket_context -> poll );
180190 }
181191
182192 CAT_LOG_DEBUG_VA_WITH_LEVEL (POLL , 2 , {
@@ -241,6 +251,8 @@ static int cat_curl_multi_socket_function(
241251# endif
242252#endif
243253 }
254+
255+ cat_queue_push_back (& CAT_CURL_G (socket_contexts ), & socket_context -> node );
244256 socket_context -> context = context ;
245257 socket_context -> sockfd = sockfd ;
246258 (void ) uv_poll_init_socket (& CAT_EVENT_G (loop ), & socket_context -> poll , sockfd );
@@ -257,9 +269,8 @@ static int cat_curl_multi_socket_function(
257269 }
258270 case CURL_POLL_REMOVE :
259271 if (socket_context != NULL ) {
260- uv_poll_stop (& socket_context -> poll );
261- uv_close ((uv_handle_t * ) & socket_context -> poll , cat_curl_multi_socket_context_close_callback );
262272 curl_multi_assign (multi , sockfd , NULL );
273+ cat_curl_multi_socket_context_close (socket_context );
263274 }
264275 break ;
265276 default :
@@ -444,7 +455,7 @@ static CURLMcode cat_curl_multi_wait_impl(
444455 // timeout or error
445456 break ;
446457 }
447- int socket_event_count = context -> events_count ;
458+ int socket_event_count = ( int ) context -> events_count ;
448459 for (size_t i = 0 ; i < context -> events_count ; i ++ ) {
449460 if (context -> events [i ].sockfd != CURL_SOCKET_TIMEOUT ) {
450461 socket_poll_event_count ++ ;
@@ -674,6 +685,21 @@ CAT_API cat_bool_t cat_curl_module_shutdown(void)
674685
675686CAT_API cat_bool_t cat_curl_runtime_init (void )
676687{
688+ RB_INIT (& CAT_CURL_G (multi_tree ));
689+ cat_queue_init (& CAT_CURL_G (socket_contexts ));
690+
691+ return cat_true ;
692+ }
693+
694+ CAT_API cat_bool_t cat_curl_runtime_shutdown (void )
695+ {
696+ cat_curl_multi_socket_context_t * socket_context ;
697+ while ((
698+ socket_context = (cat_curl_multi_socket_context_t * )
699+ cat_queue_front_data (& CAT_CURL_G (socket_contexts ), cat_curl_multi_socket_context_t , node )
700+ )) {
701+ cat_curl_multi_socket_context_close (socket_context );
702+ }
677703
678704 return cat_true ;
679705}
0 commit comments