Skip to content

Commit

Permalink
Merge pull request eclipse-iceoryx#489 from elfenpiff/iox2-488-waitse…
Browse files Browse the repository at this point in the history
…t-attachment-id-improvements

[eclipse-iceoryx#488] waitset attachment id improvements
  • Loading branch information
elfenpiff authored Oct 25, 2024
2 parents 72ff56e + 355b962 commit 98a4142
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 3 deletions.
9 changes: 8 additions & 1 deletion iceoryx2-ffi/cxx/include/iox2/waitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class WaitSetAttachmentId {
/// Returns true if the deadline for the attachment corresponding to [`WaitSetGuard`] was missed.
auto has_missed_deadline(const WaitSetGuard<S>& guard) const -> bool;

/// Returns the a non-secure hash for the [`WaitSetAttachmentId`].
auto hash() const -> std::size_t;

private:
explicit WaitSetAttachmentId(iox2_waitset_attachment_id_h handle);
template <ServiceType>
Expand All @@ -80,6 +83,8 @@ class WaitSetAttachmentId {
friend auto operator==(const WaitSetAttachmentId<ST>&, const WaitSetAttachmentId<ST>&) -> bool;
template <ServiceType ST>
friend auto operator<(const WaitSetAttachmentId<ST>&, const WaitSetAttachmentId<ST>&) -> bool;
template <ServiceType ST>
friend auto operator<<(std::ostream& stream, const WaitSetAttachmentId<ST>& self) -> std::ostream&;

void drop();

Expand All @@ -92,6 +97,9 @@ auto operator==(const WaitSetAttachmentId<S>& lhs, const WaitSetAttachmentId<S>&
template <ServiceType S>
auto operator<(const WaitSetAttachmentId<S>& lhs, const WaitSetAttachmentId<S>& rhs) -> bool;

template <ServiceType S>
auto operator<<(std::ostream& stream, const WaitSetAttachmentId<S>& self) -> std::ostream&;

/// The [`WaitSet`] implements a reactor pattern and allows to wait on multiple events in one
/// single call [`WaitSet::try_wait_and_process()`] until it wakes up or to run repeatedly with
/// [`WaitSet::wait_and_process()`] until the a interrupt or termination signal was received or the user
Expand Down Expand Up @@ -222,5 +230,4 @@ class WaitSetBuilder {
iox2_waitset_builder_h m_handle;
};
} // namespace iox2

#endif
32 changes: 32 additions & 0 deletions iceoryx2-ffi/cxx/include/iox2/waitset_hash.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 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

#ifndef IOX2_WAITSET_HASH_HPP
#define IOX2_WAITSET_HASH_HPP

#include "iox2/waitset.hpp"

template <>
struct std::hash<iox2::WaitSetAttachmentId<iox2::ServiceType::Ipc>> {
auto operator()(const iox2::WaitSetAttachmentId<iox2::ServiceType::Ipc>& self) -> std::size_t {
return self.hash();
}
};

template <>
struct std::hash<iox2::WaitSetAttachmentId<iox2::ServiceType::Local>> {
auto operator()(const iox2::WaitSetAttachmentId<iox2::ServiceType::Local>& self) -> std::size_t {
return self.hash();
}
};

#endif
19 changes: 19 additions & 0 deletions iceoryx2-ffi/cxx/src/waitset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ void WaitSetAttachmentId<S>::drop() {
}
}

template <ServiceType S>
auto WaitSetAttachmentId<S>::hash() const -> std::size_t {
auto len = iox2_waitset_attachment_id_debug_len(&m_handle);
std::string empty(len, '\0');
iox2_waitset_attachment_id_debug(&m_handle, empty.data(), len);
return std::hash<std::string> {}(empty);
}

template <ServiceType S>
auto operator==(const WaitSetAttachmentId<S>& lhs, const WaitSetAttachmentId<S>& rhs) -> bool {
return iox2_waitset_attachment_id_equal(&lhs.m_handle, &rhs.m_handle);
Expand All @@ -78,6 +86,14 @@ auto operator<(const WaitSetAttachmentId<S>& lhs, const WaitSetAttachmentId<S>&
return iox2_waitset_attachment_id_less(&lhs.m_handle, &rhs.m_handle);
}

template <ServiceType S>
auto operator<<(std::ostream& stream, const WaitSetAttachmentId<S>& self) -> std::ostream& {
auto len = iox2_waitset_attachment_id_debug_len(&self.m_handle);
std::string empty(len, '\0');
iox2_waitset_attachment_id_debug(&self.m_handle, empty.data(), len);
stream << empty;
return stream;
}

////////////////////////////
// END: WaitSetAttachmentId
Expand Down Expand Up @@ -328,4 +344,7 @@ template auto operator<(const WaitSetAttachmentId<ServiceType::Ipc>& lhs,
const WaitSetAttachmentId<ServiceType::Ipc>& rhs) -> bool;
template auto operator<(const WaitSetAttachmentId<ServiceType::Local>& lhs,
const WaitSetAttachmentId<ServiceType::Local>& rhs) -> bool;
template auto operator<<(std::ostream& stream, const WaitSetAttachmentId<ServiceType::Ipc>& self) -> std::ostream&;
template auto operator<<(std::ostream& stream, const WaitSetAttachmentId<ServiceType::Local>& self) -> std::ostream&;

} // namespace iox2
60 changes: 59 additions & 1 deletion iceoryx2-ffi/ffi/src/api/waitset_attachment_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

#![allow(non_camel_case_types)]

use std::mem::ManuallyDrop;
use crate::c_size_t;
use std::{ffi::c_char, mem::ManuallyDrop};

use iceoryx2::{
prelude::WaitSetAttachmentId,
Expand Down Expand Up @@ -303,4 +304,61 @@ pub unsafe extern "C" fn iox2_waitset_attachment_id_from_guard(

*attachment_id_handle_ptr = (*attachment_id_struct_ptr).as_handle();
}

/// Stores the debug output in the provided `debug_output` variable that must provide enough
/// memory to store the content. The content length can be acquired with
/// [`iox2_waitset_attachment_id_debug_len()`]
///
/// # Safety
/// * `handle` must be valid and non-null.
/// * `debug_output` must be valid and provide enough memory
/// * `debug_len` the provided memory length of `debug_output`
#[no_mangle]
pub unsafe extern "C" fn iox2_waitset_attachment_id_debug(
handle: iox2_waitset_attachment_id_h_ref,
debug_output: *mut c_char,
debug_len: c_size_t,
) -> bool {
handle.assert_non_null();
debug_assert!(!debug_output.is_null());

let attachment_id = &mut *handle.as_type();

let raw_str = match attachment_id.service_type {
iox2_service_type_e::IPC => format!("{:?}\0", *attachment_id.value.as_mut().ipc),
iox2_service_type_e::LOCAL => format!("{:?}\0", *attachment_id.value.as_mut().local),
};

if debug_len < raw_str.len() {
return false;
}

std::ptr::copy_nonoverlapping(
raw_str.as_bytes().as_ptr().cast(),
debug_output,
raw_str.len(),
);

true
}

/// Returns the length of the debug output. Shall be used before calling
/// [`iox2_waitset_attachment_id_debug()`] to acquire enough memory to store the output.
///
/// # Safety
/// * `handle` must be valid and non-null.
#[no_mangle]
pub unsafe extern "C" fn iox2_waitset_attachment_id_debug_len(
handle: iox2_waitset_attachment_id_h_ref,
) -> c_size_t {
handle.assert_non_null();

let attachment_id = &mut *handle.as_type();

match attachment_id.service_type {
iox2_service_type_e::IPC => format!("{:?}\0", *attachment_id.value.as_mut().ipc).len(),
iox2_service_type_e::LOCAL => format!("{:?}\0", *attachment_id.value.as_mut().local).len(),
}
}

// END C API
13 changes: 12 additions & 1 deletion iceoryx2/src/port/waitset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,23 @@ enum AttachmentIdType {
}

/// Represents an attachment to the [`WaitSet`]
#[derive(Debug, Clone, Copy)]
#[derive(Clone, Copy)]
pub struct WaitSetAttachmentId<Service: crate::service::Service> {
attachment_type: AttachmentIdType,
_data: PhantomData<Service>,
}

impl<Service: crate::service::Service> Debug for WaitSetAttachmentId<Service> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"WaitSetAttachmentId<{}> {{ attachment_type: {:?} }}",
core::any::type_name::<Service>(),
self.attachment_type
)
}
}

impl<Service: crate::service::Service> WaitSetAttachmentId<Service> {
/// Creates an [`WaitSetAttachmentId`] from a [`WaitSetGuard`] that was returned via
/// [`WaitSet::attach_interval()`], [`WaitSet::attach_notification()`] or
Expand Down

0 comments on commit 98a4142

Please sign in to comment.