iceoryx2 0.9.0

iceoryx2: Lock-Free Zero-Copy Interprocess Communication
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Copyright (c) 2023 - 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! # Example
//!
//! ```
//! use iceoryx2::prelude::*;
//!
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
//! let node = NodeBuilder::new().create::<ipc::Service>()?;
//! let service = node.service_builder(&"My/Funk/ServiceName".try_into()?)
//!     .publish_subscribe::<u64>()
//!     .open_or_create()?;
//!
//! let subscriber = service.subscriber_builder().create()?;
//!
//! while let Some(sample) = subscriber.receive()? {
//!     println!("received: {:?}", *sample);
//! }
//!
//! # Ok(())
//! # }
//! ```

use core::any::TypeId;
use core::fmt::Debug;
use core::marker::PhantomData;
use core::ptr::NonNull;

use iceoryx2_bb_concurrency::atomic::Ordering;
use iceoryx2_bb_concurrency::cell::UnsafeCell;
use iceoryx2_bb_container::slotmap::SlotMap;
use iceoryx2_bb_container::vector::polymorphic_vec::*;
use iceoryx2_bb_elementary::CallbackProgression;
use iceoryx2_bb_elementary::cyclic_tagger::CyclicTagger;
use iceoryx2_bb_elementary_traits::non_null::NonNullCompat;
use iceoryx2_bb_elementary_traits::testing::abandonable::Abandonable;
use iceoryx2_bb_elementary_traits::zero_copy_send::ZeroCopySend;
use iceoryx2_bb_lock_free::mpmc::container::{ContainerHandle, ContainerState};
use iceoryx2_bb_memory::heap_allocator::HeapAllocator;
use iceoryx2_bb_posix::unique_system_id::UniqueSystemId;
use iceoryx2_cal::arc_sync_policy::ArcSyncPolicy;
use iceoryx2_cal::dynamic_storage::DynamicStorage;
use iceoryx2_cal::zero_copy_connection::{CHANNEL_STATE_OPEN, ChannelId};
use iceoryx2_log::{fail, warn};

use crate::port::update_connections::UpdateConnections;
use crate::service::builder::CustomPayloadMarker;
use crate::service::dynamic_config::publish_subscribe::{PublisherDetails, SubscriberDetails};
use crate::service::header::publish_subscribe::Header;
use crate::service::port_factory::subscriber::SubscriberConfig;
use crate::service::static_config::publish_subscribe::StaticConfig;
use crate::service::{NoResource, SharedServiceState};
use crate::{raw_sample::RawSample, sample::Sample, service};

use super::ReceiveError;
use super::details::chunk::Chunk;
use super::details::chunk_details::ChunkDetails;
use super::details::receiver::*;
use super::update_connections::ConnectionFailure;
use crate::identifiers::UniqueSubscriberId;

/// Describes the failures when a new [`Subscriber`] is created via the
/// [`crate::service::port_factory::subscriber::PortFactorySubscriber`].
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum SubscriberCreateError {
    /// The maximum amount of [`Subscriber`]s that can connect to a
    /// [`Service`](crate::service::Service) is
    /// defined in [`crate::config::Config`]. When this is exceeded no more [`Subscriber`]s
    /// can be created for a specific [`Service`](crate::service::Service).
    ExceedsMaxSupportedSubscribers,
    /// When the [`Subscriber`] requires a larger buffer size than the
    /// [`Service`](crate::service::Service) offers the creation will fail.
    BufferSizeExceedsMaxSupportedBufferSizeOfService,
    /// Caused by a failure when instantiating a [`ArcSyncPolicy`] defined in the
    /// [`Service`](crate::service::Service) as `ArcThreadSafetyPolicy`.
    FailedToDeployThreadsafetyPolicy,
    /// The tracking port tag, required for cleanup, could not be created.
    UnableToCreatePortTag,
}

impl core::fmt::Display for SubscriberCreateError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "SubscriberCreateError::{self:?}")
    }
}

impl core::error::Error for SubscriberCreateError {}

#[derive(Debug)]
pub(crate) struct SubscriberSharedState<Service: service::Service> {
    pub(crate) receiver: Receiver<Service>,
    pub(crate) publisher_list_state: UnsafeCell<ContainerState<PublisherDetails>>,
    // IMPORTANT!
    // Fields of a rust struct are dropped in declaration order. Since this tag is our marker that the
    // port exists and might require cleanup after a crash, the tag must be defined as last member of
    // the struct.
    // Otherwise the process might crash during cleanup, has already removed the tag but other resources
    // are still existing. This would make a cleanup from another process impossible.
    port_tag: Service::StaticStorage,
}

impl<Service: service::Service> Abandonable for SubscriberSharedState<Service> {
    unsafe fn abandon_in_place(mut this: NonNull<Self>) {
        let this = unsafe { this.as_mut() };
        unsafe { Receiver::abandon_in_place(NonNull::iox2_from_mut(&mut this.receiver)) };
        unsafe {
            Service::StaticStorage::abandon_in_place(NonNull::iox2_from_mut(&mut this.port_tag))
        };
    }
}

/// The receiving endpoint of a publish-subscribe communication.
#[derive(Debug)]
pub struct Subscriber<
    Service: service::Service,
    Payload: Debug + ZeroCopySend + ?Sized + 'static,
    UserHeader: Debug + ZeroCopySend,
> {
    dynamic_subscriber_handle: Option<ContainerHandle>,
    subscriber_shared_state: Service::ArcThreadSafetyPolicy<SubscriberSharedState<Service>>,

    _payload: PhantomData<Payload>,
    _user_header: PhantomData<UserHeader>,
}

unsafe impl<
    Service: service::Service,
    Payload: Debug + ZeroCopySend + ?Sized,
    UserHeader: Debug + ZeroCopySend,
> Send for Subscriber<Service, Payload, UserHeader>
where
    Service::ArcThreadSafetyPolicy<SubscriberSharedState<Service>>: Send + Sync,
{
}

unsafe impl<
    Service: service::Service,
    Payload: Debug + ZeroCopySend + ?Sized,
    UserHeader: Debug + ZeroCopySend,
> Sync for Subscriber<Service, Payload, UserHeader>
where
    Service::ArcThreadSafetyPolicy<SubscriberSharedState<Service>>: Send + Sync,
{
}

impl<
    Service: service::Service,
    Payload: Debug + ZeroCopySend + ?Sized,
    UserHeader: Debug + ZeroCopySend,
> Abandonable for Subscriber<Service, Payload, UserHeader>
{
    unsafe fn abandon_in_place(mut this: NonNull<Self>) {
        let this = unsafe { this.as_mut() };
        unsafe {
            Service::ArcThreadSafetyPolicy::abandon_in_place(NonNull::iox2_from_mut(
                &mut this.subscriber_shared_state,
            ))
        };
    }
}

impl<
    Service: service::Service,
    Payload: Debug + ZeroCopySend + ?Sized,
    UserHeader: Debug + ZeroCopySend,
> Drop for Subscriber<Service, Payload, UserHeader>
{
    fn drop(&mut self) {
        if let Some(handle) = self.dynamic_subscriber_handle {
            self.subscriber_shared_state
                .lock()
                .receiver
                .service_state
                .dynamic_storage()
                .get()
                .publish_subscribe()
                .release_subscriber_handle(handle)
        }
    }
}

impl<
    Service: service::Service,
    Payload: Debug + ZeroCopySend + ?Sized,
    UserHeader: Debug + ZeroCopySend,
> Subscriber<Service, Payload, UserHeader>
{
    pub(crate) fn new(
        service: SharedServiceState<Service, NoResource>,
        static_config: &StaticConfig,
        config: SubscriberConfig,
    ) -> Result<Self, SubscriberCreateError> {
        let msg = "Failed to create Subscriber port";
        let origin = "Subscriber::new()";
        let subscriber_id = UniqueSubscriberId::new();
        // !MUST! be the first thing that is created when a new port is instantiated otherwise the
        // port resources might leak if this process is killed in between.
        let port_tag = match service.shared_node().create_port_tag(
            origin,
            msg,
            subscriber_id.0.value(),
        ) {
            Ok(port_tag) => port_tag,
            Err(e) => {
                fail!(from origin, with SubscriberCreateError::UnableToCreatePortTag,
                        "{msg} since the port tag, that is required for cleanup, could not be created. [{e:?}]");
            }
        };

        let publisher_list = &service
            .dynamic_storage()
            .get()
            .publish_subscribe()
            .publishers;

        let buffer_size = match config.buffer_size {
            Some(buffer_size) => {
                if static_config.subscriber_max_buffer_size < buffer_size {
                    fail!(from origin, with SubscriberCreateError::BufferSizeExceedsMaxSupportedBufferSizeOfService,
                        "{} since the requested buffer size {} exceeds the maximum supported buffer size {} of the service.",
                        msg, buffer_size, static_config.subscriber_max_buffer_size);
                }
                buffer_size
            }
            None => static_config.subscriber_max_buffer_size,
        };

        let subscriber_max_borrowed_samples = static_config.subscriber_max_borrowed_samples;
        let subscriber_expired_connection_buffer = service
            .shared_node()
            .config()
            .defaults
            .publish_subscribe
            .subscriber_expired_connection_buffer;

        let number_of_to_be_removed_connections = if subscriber_expired_connection_buffer
            >= subscriber_max_borrowed_samples
        {
            subscriber_expired_connection_buffer
        } else {
            warn!(
                "Subscriber max borrowed samples is larger than expired connection buffer! Set buffer capacity to value of max borrowed samples."
            );
            subscriber_max_borrowed_samples
        };

        let number_of_active_connections = publisher_list.capacity();
        let number_of_connections =
            number_of_to_be_removed_connections + number_of_active_connections;

        let subscriber_shared_state = Service::ArcThreadSafetyPolicy::new(SubscriberSharedState {
            port_tag,
            publisher_list_state: UnsafeCell::new(unsafe { publisher_list.get_state() }),
            receiver: Receiver {
                connections: PolymorphicVec::from_fn(
                    HeapAllocator::global(),
                    number_of_active_connections,
                    |_| UnsafeCell::new(None),
                )
                .expect("Heap allocator provides memory."),
                receiver_port_id: subscriber_id.value(),
                service_state: service.clone(),
                message_type_details: static_config.message_type_details,
                receiver_max_borrowed_samples: subscriber_max_borrowed_samples,
                enable_safe_overflow: static_config.enable_safe_overflow,
                buffer_size,
                tagger: CyclicTagger::new(),
                to_be_removed_connections: Some(UnsafeCell::new(
                    PolymorphicVec::new(
                        HeapAllocator::global(),
                        number_of_to_be_removed_connections,
                    )
                    .expect("Heap allocator provides memory."),
                )),
                degradation_handler: config.degradation_handler,
                number_of_channels: 1,
                connection_storage: UnsafeCell::new(SlotMap::new(number_of_connections)),
                initial_channel_state: CHANNEL_STATE_OPEN,
            },
        });

        let subscriber_shared_state = match subscriber_shared_state {
            Ok(v) => v,
            Err(e) => {
                fail!(from origin,
                            with SubscriberCreateError::FailedToDeployThreadsafetyPolicy,
                            "{msg} since the threadsafety policy could not be instantiated ({e:?}).");
            }
        };

        let mut new_self = Self {
            subscriber_shared_state,
            dynamic_subscriber_handle: None,
            _payload: PhantomData,
            _user_header: PhantomData,
        };

        if let Err(e) = new_self.force_update_connections(&new_self.subscriber_shared_state.lock())
        {
            warn!(from new_self, "The new subscriber is unable to connect to every publisher, caused by {:?}.", e);
        }

        core::sync::atomic::compiler_fence(Ordering::SeqCst);

        // !MUST! be the last task otherwise a subscriber is added to the dynamic config without
        // the creation of all required channels
        let dynamic_subscriber_handle = match service
            .dynamic_storage()
            .get()
            .publish_subscribe()
            .add_subscriber_id(SubscriberDetails {
                subscriber_id,
                buffer_size,
                node_id: *service.shared_node().id(),
            }) {
            Some(unique_index) => unique_index,
            None => {
                fail!(from new_self, with SubscriberCreateError::ExceedsMaxSupportedSubscribers,
                                "{} since it would exceed the maximum supported amount of subscribers of {}.",
                                msg, service.static_config().publish_subscribe().max_subscribers);
            }
        };

        new_self.dynamic_subscriber_handle = Some(dynamic_subscriber_handle);

        Ok(new_self)
    }

    fn force_update_connections(
        &self,
        subscriber_shared_state: &SubscriberSharedState<Service>,
    ) -> Result<(), ConnectionFailure> {
        subscriber_shared_state
            .receiver
            .start_update_connection_cycle();

        let mut result = Ok(());
        unsafe {
            (*subscriber_shared_state.publisher_list_state.get()).for_each(|index, details| {
                let inner_result = subscriber_shared_state.receiver.update_connection(
                    index,
                    SenderDetails {
                        port_id: details.publisher_id.value(),
                        number_of_samples: details.number_of_samples,
                        max_number_of_segments: details.max_number_of_segments,
                        data_segment_type: details.data_segment_type,
                    },
                );

                if result.is_ok() {
                    result = inner_result;
                }
                CallbackProgression::Continue
            })
        };

        subscriber_shared_state
            .receiver
            .finish_update_connection_cycle();

        result
    }

    /// Returns the [`UniqueSubscriberId`] of the [`Subscriber`]
    pub fn id(&self) -> UniqueSubscriberId {
        UniqueSubscriberId(UniqueSystemId::from(
            self.subscriber_shared_state
                .lock()
                .receiver
                .receiver_port_id(),
        ))
    }

    /// Returns the internal buffer size of the [`Subscriber`].
    pub fn buffer_size(&self) -> usize {
        self.subscriber_shared_state.lock().receiver.buffer_size
    }

    /// Returns true if the [`Subscriber`] has samples in the buffer that can be received with [`Subscriber::receive`].
    pub fn has_samples(&self) -> Result<bool, ConnectionFailure> {
        fail!(from self, when self.update_connections(),
                "Some samples are not being received since not all connections to publishers could be established.");
        Ok(self
            .subscriber_shared_state
            .lock()
            .receiver
            .has_samples(ChannelId::new(0)))
    }

    fn receive_impl(&self) -> Result<Option<(ChunkDetails, Chunk)>, ReceiveError> {
        fail!(from self, when self.update_connections(),
                "Some samples are not being received since not all connections to publishers could be established.");

        self.subscriber_shared_state
            .lock()
            .receiver
            .receive(ChannelId::new(0))
    }
}

impl<
    Service: service::Service,
    Payload: Debug + ZeroCopySend + ?Sized,
    UserHeader: Debug + ZeroCopySend,
> UpdateConnections for Subscriber<Service, Payload, UserHeader>
{
    fn update_connections(&self) -> Result<(), ConnectionFailure> {
        let subscriber_shared_state = self.subscriber_shared_state.lock();
        if unsafe {
            subscriber_shared_state
                .receiver
                .service_state
                .dynamic_storage()
                .get()
                .publish_subscribe()
                .publishers
                .update_state(&mut *subscriber_shared_state.publisher_list_state.get())
        } {
            fail!(from self, when self.force_update_connections(&subscriber_shared_state),
                "Connections were updated only partially since at least one connection to a publisher failed.");
        }

        Ok(())
    }
}

impl<Service: service::Service, Payload: Debug + ZeroCopySend, UserHeader: Debug + ZeroCopySend>
    Subscriber<Service, Payload, UserHeader>
{
    /// Receives a [`crate::sample::Sample`] from [`crate::port::publisher::Publisher`]. If no sample could be
    /// received [`None`] is returned. If a failure occurs [`ReceiveError`] is returned.
    pub fn receive(&self) -> Result<Option<Sample<Service, Payload, UserHeader>>, ReceiveError> {
        Ok(self.receive_impl()?.map(|(details, chunk)| Sample {
            subscriber_shared_state: self.subscriber_shared_state.clone(),
            details,
            ptr: unsafe {
                RawSample::new_unchecked(
                    chunk.header.cast(),
                    chunk.user_header.cast(),
                    chunk.payload.cast(),
                )
            },
        }))
    }
}

impl<Service: service::Service, Payload: Debug + ZeroCopySend, UserHeader: Debug + ZeroCopySend>
    Subscriber<Service, [Payload], UserHeader>
{
    /// Receives a [`crate::sample::Sample`] from [`crate::port::publisher::Publisher`]. If no sample could be
    /// received [`None`] is returned. If a failure occurs [`ReceiveError`] is returned.
    pub fn receive(&self) -> Result<Option<Sample<Service, [Payload], UserHeader>>, ReceiveError> {
        debug_assert!(TypeId::of::<Payload>() != TypeId::of::<CustomPayloadMarker>());

        Ok(self.receive_impl()?.map(|(details, chunk)| {
            let header_ptr = chunk.header as *const Header;
            let number_of_elements = unsafe { (*header_ptr).number_of_elements() };

            Sample {
                subscriber_shared_state: self.subscriber_shared_state.clone(),
                details,
                ptr: unsafe {
                    RawSample::<Header, UserHeader, [Payload]>::new_slice_unchecked(
                        header_ptr,
                        chunk.user_header.cast(),
                        core::ptr::slice_from_raw_parts(
                            chunk.payload.cast(),
                            number_of_elements as _,
                        ),
                    )
                },
            }
        }))
    }
}

impl<Service: service::Service, UserHeader: Debug + ZeroCopySend>
    Subscriber<Service, [CustomPayloadMarker], UserHeader>
{
    /// # Safety
    ///
    ///  * The number_of_elements in the [`Header`](crate::service::header::publish_subscribe::Header)
    ///     corresponds to the payload type details that where overridden in
    ///     `MessageTypeDetails::payload.size`.
    ///     If the `payload.size == 8` a value for number_of_elements of 5 means that there are
    ///     5 elements of size 8 stored in the [`Sample`].
    ///  *  When the payload.size == 8 and the number of elements if 5, it means that the sample
    ///     will contain a slice of 8 * 5 = 40 [`CustomPayloadMarker`]s or 40 bytes.
    #[doc(hidden)]
    pub unsafe fn receive_custom_payload(
        &self,
    ) -> Result<Option<Sample<Service, [CustomPayloadMarker], UserHeader>>, ReceiveError> {
        Ok(self.receive_impl()?.map(|(details, chunk)| {
            let header_ptr = chunk.header as *const Header;
            let number_of_elements = unsafe { (*header_ptr).number_of_elements() };
            let number_of_bytes = number_of_elements as usize
                * self.subscriber_shared_state.lock().receiver.payload_size();

            Sample {
                subscriber_shared_state: self.subscriber_shared_state.clone(),
                details,
                ptr: unsafe {
                    RawSample::<Header, UserHeader, [CustomPayloadMarker]>::new_slice_unchecked(
                        header_ptr,
                        chunk.user_header.cast(),
                        core::ptr::slice_from_raw_parts(chunk.payload.cast(), number_of_bytes),
                    )
                },
            }
        }))
    }
}