Skip to main content

rustorb_core/
timeout.rs

1//! Client call timeouts: the defaults of the ORB, of the calling thread and
2//! of an object reference, and which one a call uses.
3
4use std::cell::Cell;
5use std::time::Duration;
6
7/// A stored timeout that is not set, so the next default applies.
8pub(crate) const UNSET: u64 = u64::MAX;
9
10/// Stores a timeout in an atomic: 0 for no limit, else units of 100
11/// nanoseconds (at least 1), the unit of `TimeBase::TimeT`, so that a
12/// `RelativeRoundtripTimeoutPolicy` reads back the value it was set to.
13pub(crate) fn encode(timeout: Option<Duration>) -> u64 {
14    match timeout {
15        None => 0,
16        Some(t) => u64::try_from(t.as_nanos() / 100)
17            .unwrap_or(UNSET - 1)
18            .clamp(1, UNSET - 1),
19    }
20}
21
22/// The timeout `encode` stored (not [`UNSET`]).
23pub(crate) fn decode(v: u64) -> Option<Duration> {
24    (v != 0).then(|| time_t_to_duration(v))
25}
26
27/// A `TimeBase::TimeT` (units of 100 nanoseconds) as a duration.
28pub(crate) fn time_t_to_duration(t: u64) -> Duration {
29    Duration::new(t / 10_000_000, (t % 10_000_000) as u32 * 100)
30}
31
32/// A duration as a `TimeBase::TimeT`, rounded down (saturating).
33pub(crate) fn duration_to_time_t(d: Duration) -> u64 {
34    u64::try_from(d.as_nanos() / 100).unwrap_or(u64::MAX)
35}
36
37thread_local! {
38    static THREAD_CALL_TIMEOUT: Cell<Option<Option<Duration>>> = const { Cell::new(None) };
39}
40
41/// Sets the call timeout of the calling thread, for every ORB: `Some(t)`
42/// (where `t` is `None` for no limit) overrides the ORB's
43/// [`call_timeout`](crate::Orb::call_timeout), but not the object
44/// reference's ([`ObjectRef::set_call_timeout`](crate::ObjectRef::set_call_timeout));
45/// `None` unsets it. Like omniORB's `setClientThreadCallTimeout`, which
46/// omniORB honours only with `-ORBsupportPerThreadTimeOut 1`; rustorb
47/// always does. Returns the previous setting.
48pub fn set_thread_call_timeout(timeout: Option<Option<Duration>>) -> Option<Option<Duration>> {
49    THREAD_CALL_TIMEOUT.with(|c| c.replace(timeout))
50}
51
52/// The setting of [`set_thread_call_timeout`] on the calling thread.
53pub fn thread_call_timeout() -> Option<Option<Duration>> {
54    THREAD_CALL_TIMEOUT.with(Cell::get)
55}