From 4f5d013640d79a761b1539ac5454ff2a8e7ed4ff Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Tue, 21 Oct 2025 04:10:14 -0400 Subject: [PATCH] MT#55283 introduce socket use counter Change-Id: I2468f4faedd195c9ea52d6ee6c99eabf37b7e73e --- daemon/call.c | 6 ++++-- daemon/media_socket.c | 2 +- include/media_socket.h | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/daemon/call.c b/daemon/call.c index 0f2fb2d17..d512a9067 100644 --- a/daemon/call.c +++ b/daemon/call.c @@ -793,7 +793,7 @@ static bool __endpoint_map_truncate(struct endpoint_map *em, unsigned int num_in __auto_type il = l->data; while (il->list.length > num_ports) { __auto_type p = t_queue_pop_tail(&il->list); - stream_fd_release(p); + stream_fd_dec(p); } } @@ -925,6 +925,7 @@ static void __assign_stream_fds(struct call_media *media, sfd_intf_list_q *intf_ } sfd->stream = ps; + stream_fd_inc(sfd); t_queue_push_tail(&ps->sfds, sfd); if (ps->selected_sfd == sfd) @@ -3729,6 +3730,7 @@ static bool media_open_ports(struct call_media *media) { } __auto_type sfd = stream_fd_new(spl, media->call, em_il->local_intf); t_queue_push_tail(&em_il->list, sfd); // not referenced + stream_fd_inc(sfd); g_free(spl); // XXX eliminate this } } @@ -5631,7 +5633,7 @@ void monologue_destroy(struct call_monologue *monologue) { stream_fd *sfd; while ((sfd = t_queue_pop_head(&ps->sfds))) - stream_fd_release(sfd); + stream_fd_dec(sfd); } if (m->endpoint_map) t_queue_clear_full(&m->endpoint_map->intf_sfds, free_release_sfd_intf_list); diff --git a/daemon/media_socket.c b/daemon/media_socket.c index f37f18071..b6ed65b90 100644 --- a/daemon/media_socket.c +++ b/daemon/media_socket.c @@ -1396,7 +1396,7 @@ void free_sfd_intf_list(struct sfd_intf_list *il) { g_free(il); } void free_release_sfd_intf_list(struct sfd_intf_list *il) { - t_queue_clear_full(&il->list, stream_fd_release); + t_queue_clear_full(&il->list, stream_fd_dec); g_free(il); } diff --git a/include/media_socket.h b/include/media_socket.h index c60269cf6..4e3d691e0 100644 --- a/include/media_socket.h +++ b/include/media_socket.h @@ -242,10 +242,13 @@ struct stream_fd { int active_read_events; struct poller *poller; + unsigned int users; + bool kernelized:1, confirmed:1; }; + struct sink_attrs { // cannot be bit fields because G_STRUCT_OFFSET is used on them bool block_media; @@ -399,6 +402,22 @@ void stream_fd_release(stream_fd *); enum thread_looper_action release_closed_sockets(void); void append_thread_lpr_to_glob_lpr(void); + +// needs upper level locking +__attribute__((nonnull(1))) +INLINE void stream_fd_inc(stream_fd *sfd) { + sfd->users++; +} + +// needs upper level locking +__attribute__((nonnull(1))) +INLINE void stream_fd_dec(stream_fd *sfd) { + if (--sfd->users > 0) + return; + stream_fd_release(sfd); +} + + void free_sfd_intf_list(struct sfd_intf_list *il); void free_release_sfd_intf_list(struct sfd_intf_list *il); void free_socket_intf_list(struct socket_intf_list *il);