@@ -4,7 +4,9 @@ mod shm_io;
44
55use std:: { env:: temp_dir, fs:: File , io, mem:: MaybeUninit , ops:: Deref , path:: PathBuf , sync:: Arc } ;
66
7- use fspy_shm:: Shm ;
7+ #[ cfg( target_os = "linux" ) ]
8+ pub use fspy_shm:: ShmBroker ;
9+ use fspy_shm:: { CreatedShm , Shm } ;
810pub use shm_io:: FrameMut ;
911use shm_io:: { ShmReader , ShmWriter } ;
1012use tracing:: debug;
@@ -54,16 +56,31 @@ pub struct ChannelConf {
5456 shm_size : usize ,
5557}
5658
59+ /// A newly created IPC channel and its platform service.
60+ pub struct CreatedChannel {
61+ /// The serializable configuration used to create senders.
62+ pub conf : ChannelConf ,
63+ /// The receiving side of the channel.
64+ pub receiver : Receiver ,
65+ /// The Linux service future that makes the channel mapping available to senders.
66+ #[ cfg( target_os = "linux" ) ]
67+ pub broker : ShmBroker ,
68+ }
69+
5770/// Creates a mpsc IPC channel with one receiver and a `ChannelConf` that can be passed around processes and used to create multiple senders
5871#[ expect(
5972 clippy:: missing_errors_doc,
6073 reason = "non-vite crate: cannot use vite_str/vite_path types"
6174) ]
62- pub fn channel ( capacity : usize ) -> io:: Result < ( ChannelConf , Receiver ) > {
75+ pub fn channel ( capacity : usize ) -> io:: Result < CreatedChannel > {
6376 // Initialize the lock file with a unique name.
6477 let lock_file_path = temp_dir ( ) . join ( format ! ( "fspy_ipc_{}.lock" , Uuid :: new_v4( ) ) ) ;
6578
66- let shm = fspy_shm:: create ( capacity) ?;
79+ let CreatedShm {
80+ shm,
81+ #[ cfg( target_os = "linux" ) ]
82+ broker,
83+ } = fspy_shm:: create ( capacity) ?;
6784
6885 let conf = ChannelConf {
6986 lock_file_path : lock_file_path. as_os_str ( ) . into ( ) ,
@@ -72,7 +89,12 @@ pub fn channel(capacity: usize) -> io::Result<(ChannelConf, Receiver)> {
7289 } ;
7390
7491 let receiver = Receiver :: new ( lock_file_path, shm) ?;
75- Ok ( ( conf, receiver) )
92+ Ok ( CreatedChannel {
93+ conf,
94+ receiver,
95+ #[ cfg( target_os = "linux" ) ]
96+ broker,
97+ } )
7698}
7799
78100impl ChannelConf {
@@ -206,7 +228,9 @@ mod tests {
206228
207229 #[ test]
208230 fn smoke ( ) {
209- let ( conf, receiver) = channel ( 100 ) . unwrap ( ) ;
231+ let created = channel ( 100 ) . unwrap ( ) ;
232+ let conf = created. conf ;
233+ let receiver = created. receiver ;
210234 let cmd = command_for_fn ! ( conf, |conf: ChannelConf | {
211235 let sender = conf. sender( ) . unwrap( ) ;
212236 let frame_size = NonZeroUsize :: new( 2 ) . unwrap( ) ;
@@ -227,7 +251,9 @@ mod tests {
227251 #[ test]
228252 #[ expect( clippy:: print_stdout, reason = "test diagnostics" ) ]
229253 fn forbid_new_senders_after_locked ( ) {
230- let ( conf, receiver) = channel ( 42 ) . unwrap ( ) ;
254+ let created = channel ( 42 ) . unwrap ( ) ;
255+ let conf = created. conf ;
256+ let receiver = created. receiver ;
231257 let _lock = receiver. lock ( ) . unwrap ( ) ;
232258
233259 let cmd = command_for_fn ! ( conf, |conf: ChannelConf | {
@@ -240,7 +266,9 @@ mod tests {
240266 #[ test]
241267 #[ expect( clippy:: print_stdout, reason = "test diagnostics" ) ]
242268 fn forbid_new_senders_after_receiver_dropped ( ) {
243- let ( conf, receiver) = channel ( 42 ) . unwrap ( ) ;
269+ let created = channel ( 42 ) . unwrap ( ) ;
270+ let conf = created. conf ;
271+ let receiver = created. receiver ;
244272 drop ( receiver) ;
245273
246274 let cmd = command_for_fn ! ( conf, |conf: ChannelConf | {
@@ -252,7 +280,9 @@ mod tests {
252280
253281 #[ test]
254282 fn concurrent_senders ( ) {
255- let ( conf, receiver) = channel ( 8192 ) . unwrap ( ) ;
283+ let created = channel ( 8192 ) . unwrap ( ) ;
284+ let conf = created. conf ;
285+ let receiver = created. receiver ;
256286 for i in 0u16 ..200 {
257287 let cmd = command_for_fn ! ( ( conf. clone( ) , i) , |( conf, i) : ( ChannelConf , u16 ) | {
258288 let sender = conf. sender( ) . unwrap( ) ;
0 commit comments