Skip to content

Commit

Permalink
Auto merge of #100 - emilio:fix-inprocess-oneshot, r=emilio,pcwalton
Browse files Browse the repository at this point in the history
Fix inprocess oneshot

Rebase of #90.

@antrik sent a mail to the list saying he was not going to be active, so I took care of rebasing this.
  • Loading branch information
bors-servo authored Aug 31, 2016
2 parents 2422fde + db1f5a5 commit 65d8404
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/platform/inprocess/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ use std::usize;

use uuid::Uuid;

#[derive(Clone)]
struct ServerRecord {
sender: OsIpcSender,
conn_sender: mpsc::Sender<bool>,
conn_receiver: Mutex<mpsc::Receiver<bool>>,
conn_receiver: Arc<Mutex<mpsc::Receiver<bool>>>,
}

impl ServerRecord {
Expand All @@ -34,7 +35,7 @@ impl ServerRecord {
ServerRecord {
sender: sender,
conn_sender: tx,
conn_receiver: Mutex::new(rx),
conn_receiver: Arc::new(Mutex::new(rx)),
}
}

Expand Down Expand Up @@ -138,7 +139,7 @@ impl OsIpcSender {
}

pub fn connect(name: String) -> Result<OsIpcSender,MpscError> {
let record = ONE_SHOT_SERVERS.lock().unwrap().remove(&name).unwrap();
let record = ONE_SHOT_SERVERS.lock().unwrap().get(&name).unwrap().clone();
record.connect();
Ok(record.sender)
}
Expand Down Expand Up @@ -282,7 +283,9 @@ impl OsIpcOneShotServer {
Vec<OsOpaqueIpcChannel>,
Vec<OsIpcSharedMemory>),MpscError>
{
ONE_SHOT_SERVERS.lock().unwrap().get(&self.name).unwrap().accept();
let record = ONE_SHOT_SERVERS.lock().unwrap().get(&self.name).unwrap().clone();
record.accept();
ONE_SHOT_SERVERS.lock().unwrap().remove(&self.name).unwrap();
let receiver = self.receiver.borrow_mut().take().unwrap();
let (data, channels, shmems) = receiver.recv().unwrap();
Ok((receiver, data, channels, shmems))
Expand Down
25 changes: 21 additions & 4 deletions src/platform/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use std::thread;

#[cfg(not(any(feature = "force-inprocess", target_os = "windows", target_os = "android")))]
use libc;
#[cfg(not(any(feature = "force-inprocess", target_os = "windows", target_os = "android")))]
use platform::{OsIpcSender, OsIpcOneShotServer};
#[cfg(not(any(feature = "force-inprocess", target_os = "windows", target_os = "android")))]
use test::{fork, Wait};
Expand Down Expand Up @@ -396,10 +395,27 @@ fn receiver_set() {
}
}

#[cfg(not(any(feature = "force-inprocess", target_os = "windows", target_os = "android")))]
#[cfg(not(any(feature = "force-inprocess", target_os = "android")))]
#[test]
fn server_accept_first() {
let (server, name) = OsIpcOneShotServer::new().unwrap();
let data: &[u8] = b"1234567";

thread::spawn(move || {
thread::sleep(Duration::from_millis(30));
let tx = OsIpcSender::connect(name).unwrap();
tx.send(data, vec![], vec![]).unwrap();
});

let (_, mut received_data, received_channels, received_shared_memory_regions) =
server.accept().unwrap();
received_data.truncate(7);
assert_eq!((&received_data[..], received_channels, received_shared_memory_regions),
(data, vec![], vec![]));
}

#[test]
//XXXjdm This hangs indefinitely with inprocess impl and warrants further investigation.
fn server() {
fn server_connect_first() {
let (server, name) = OsIpcOneShotServer::new().unwrap();
let data: &[u8] = b"1234567";

Expand All @@ -408,6 +424,7 @@ fn server() {
tx.send(data, vec![], vec![]).unwrap();
});

thread::sleep(Duration::from_millis(30));
let (_, mut received_data, received_channels, received_shared_memory_regions) =
server.accept().unwrap();
received_data.truncate(7);
Expand Down

0 comments on commit 65d8404

Please sign in to comment.