Skip to content

Commit

Permalink
Implement From<Runtime(Ref)> for Thread{Local,Safe}
Browse files Browse the repository at this point in the history
This allows the creation of ThreadLocal and ThreadSafe from the Runtime
and RuntimeRef types.

This is required for manual creation of actors using ActorFutureBuilder
type and customer runtime.
  • Loading branch information
Thomasdezeeuw committed Mar 20, 2024
1 parent 9aa893c commit 1c7ee4a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
20 changes: 19 additions & 1 deletion rt/src/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use heph::{actor, sync, ActorRef, NewActor, Supervisor};
use crate::spawn::{ActorOptions, FutureOptions, Spawn};
use crate::timers::TimerToken;
use crate::trace::{self, Trace};
use crate::{shared, RuntimeRef};
use crate::{shared, Runtime, RuntimeRef};

/// Runtime Access Trait.
///
Expand Down Expand Up @@ -155,6 +155,12 @@ impl ThreadLocal {
}
}

impl From<RuntimeRef> for ThreadLocal {
fn from(rt: RuntimeRef) -> ThreadLocal {
ThreadLocal::new(rt)
}
}

impl Deref for ThreadLocal {
type Target = RuntimeRef;

Expand Down Expand Up @@ -286,6 +292,18 @@ impl ThreadSafe {
}
}

impl From<&Runtime> for ThreadSafe {
fn from(rt: &Runtime) -> ThreadSafe {
ThreadSafe::new(rt.internals.clone())
}
}

impl From<&RuntimeRef> for ThreadSafe {
fn from(rt: &RuntimeRef) -> ThreadSafe {
ThreadSafe::new(rt.clone_shared())
}
}

impl Access for ThreadSafe {}

impl PrivateAccess for ThreadSafe {
Expand Down
1 change: 0 additions & 1 deletion rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,6 @@ impl RuntimeRef {
}

/// Returns a copy of the shared internals.
#[cfg(any(test, feature = "test"))]
fn clone_shared(&self) -> Arc<shared::RuntimeInternals> {
self.internals.shared.clone()
}
Expand Down
1 change: 1 addition & 0 deletions rt/tests/functional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod util;

#[path = "functional"] // rustfmt can't find the files.
mod functional {
mod access;
mod actor;
mod actor_context;
mod actor_group;
Expand Down
53 changes: 53 additions & 0 deletions rt/tests/functional/access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//! Tests for the access module.

use heph_rt::{Runtime, RuntimeRef, ThreadLocal, ThreadSafe};

use crate::util::{assert_send, assert_sync};

#[test]
fn thread_safe_is_send_sync() {
assert_send::<ThreadSafe>();
assert_sync::<ThreadSafe>();
}

#[test]
fn thread_local_from_runtime_ref() {
let mut rt = Runtime::new().unwrap();
rt.run_on_workers(|runtime_ref| {
let thread_local = ThreadLocal::from(runtime_ref);
drop(thread_local);
Ok::<_, !>(())
})
.unwrap();
}

#[test]
fn thread_local_deref_as_runtime_ref() {
let mut rt = Runtime::new().unwrap();
rt.run_on_workers(|runtime_ref| {
let mut thread_local = ThreadLocal::from(runtime_ref);
let _runtime_ref: &mut RuntimeRef = &mut *thread_local;
drop(thread_local);
Ok::<_, !>(())
})
.unwrap();
}

#[test]
fn thread_safe_from_runtime() {
let rt = Runtime::new().unwrap();
let thread_safe = ThreadSafe::from(&rt);
drop(thread_safe);
drop(rt);
}

#[test]
fn thread_safe_from_runtime_ref() {
let mut rt = Runtime::new().unwrap();
rt.run_on_workers(|runtime_ref| {
let thread_safe = ThreadSafe::from(&runtime_ref);
drop(thread_safe);
Ok::<_, !>(())
})
.unwrap();
}

0 comments on commit 1c7ee4a

Please sign in to comment.