msg_socket: impl for Arc and Mutex

These types are simple wrappers, and so this change just adds trivial
passthroughs.

For data sent over a socket, the data is not Arc-ed or Mutex-ed on the
other end with the same ref count or lock. Sending over the data is more
like a deep copy, rather than a simple ref count increase.

TEST=cargo test -p msg_socket
BUG=None

Change-Id: I519096b0b3b6ab75e79c37addf0d60ce3da4a717
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2168586
Commit-Queue: Zach Reizner <zachr@chromium.org>
Tested-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
This commit is contained in:
Zach Reizner 2020-03-25 01:16:38 -07:00 committed by Commit Bot
parent 34c00465d5
commit 8b3ee41b30
3 changed files with 48 additions and 0 deletions

1
Cargo.lock generated
View file

@ -447,6 +447,7 @@ dependencies = [
"futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
"msg_on_socket_derive 0.1.0", "msg_on_socket_derive 0.1.0",
"sync 0.1.0",
"sys_util 0.1.0", "sys_util 0.1.0",
] ]

View file

@ -11,3 +11,4 @@ futures = "*"
libc = "*" libc = "*"
msg_on_socket_derive = { path = "msg_on_socket_derive" } msg_on_socket_derive = { path = "msg_on_socket_derive" }
sys_util = { path = "../sys_util" } sys_util = { path = "../sys_util" }
sync = { path = "../sync" }

View file

@ -10,8 +10,10 @@ use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
use std::os::unix::net::{UnixDatagram, UnixListener, UnixStream}; use std::os::unix::net::{UnixDatagram, UnixListener, UnixStream};
use std::ptr::drop_in_place; use std::ptr::drop_in_place;
use std::result; use std::result;
use std::sync::Arc;
use data_model::*; use data_model::*;
use sync::Mutex;
use sys_util::{Error as SysError, EventFd}; use sys_util::{Error as SysError, EventFd};
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -209,6 +211,50 @@ impl<T: MsgOnSocket> MsgOnSocket for Option<T> {
} }
} }
impl<T: MsgOnSocket> MsgOnSocket for Mutex<T> {
fn uses_fd() -> bool {
T::uses_fd()
}
fn msg_size(&self) -> usize {
self.lock().msg_size()
}
fn fd_count(&self) -> usize {
self.lock().fd_count()
}
unsafe fn read_from_buffer(buffer: &[u8], fds: &[RawFd]) -> MsgResult<(Self, usize)> {
T::read_from_buffer(buffer, fds).map(|(v, count)| (Mutex::new(v), count))
}
fn write_to_buffer(&self, buffer: &mut [u8], fds: &mut [RawFd]) -> MsgResult<usize> {
self.lock().write_to_buffer(buffer, fds)
}
}
impl<T: MsgOnSocket> MsgOnSocket for Arc<T> {
fn uses_fd() -> bool {
T::uses_fd()
}
fn msg_size(&self) -> usize {
(**self).msg_size()
}
fn fd_count(&self) -> usize {
(**self).fd_count()
}
unsafe fn read_from_buffer(buffer: &[u8], fds: &[RawFd]) -> MsgResult<(Self, usize)> {
T::read_from_buffer(buffer, fds).map(|(v, count)| (Arc::new(v), count))
}
fn write_to_buffer(&self, buffer: &mut [u8], fds: &mut [RawFd]) -> MsgResult<usize> {
(**self).write_to_buffer(buffer, fds)
}
}
impl MsgOnSocket for () { impl MsgOnSocket for () {
fn fixed_size() -> Option<usize> { fn fixed_size() -> Option<usize> {
Some(0) Some(0)