100-exercises-to-learn-rust/exercises/07_threads/11_locks/src/lib.rs

81 lines
2.3 KiB
Rust
Raw Normal View History

2024-05-13 04:21:03 +08:00
// TODO: Fill in the missing methods for `TicketStore`.
// Notice how we no longer need a separate update command: `Get` now returns a handle to the ticket
// which allows the caller to both modify and read the ticket.
use std::sync::mpsc::{sync_channel, Receiver, SyncSender, TrySendError};
use std::sync::{Arc, Mutex};
use crate::data::{Ticket, TicketDraft};
use crate::store::{TicketId, TicketStore};
pub mod data;
pub mod store;
#[derive(Clone)]
pub struct TicketStoreClient {
sender: SyncSender<Command>,
}
impl TicketStoreClient {
pub fn insert(&self, draft: TicketDraft) -> Result<TicketId, TrySendError<Command>> {
let (response_sender, response_receiver) = sync_channel(1);
self.sender.try_send(Command::Insert {
draft,
response_channel: response_sender,
})?;
Ok(response_receiver.recv().unwrap())
}
pub fn get(&self, id: TicketId) -> Result<Option<Arc<Mutex<Ticket>>>, TrySendError<Command>> {
let (response_sender, response_receiver) = sync_channel(1);
self.sender.try_send(Command::Get {
id,
response_channel: response_sender,
})?;
Ok(response_receiver.recv().unwrap())
}
}
pub fn launch(capacity: usize) -> TicketStoreClient {
let (sender, receiver) = sync_channel(capacity);
std::thread::spawn(move || server(receiver));
TicketStoreClient { sender }
}
enum Command {
Insert {
draft: TicketDraft,
response_channel: SyncSender<TicketId>,
},
Get {
id: TicketId,
response_channel: SyncSender<Option<Arc<Mutex<Ticket>>>>,
},
}
pub fn server(receiver: Receiver<Command>) {
let mut store = TicketStore::new();
loop {
match receiver.recv() {
Ok(Command::Insert {
draft,
response_channel,
}) => {
let id = store.add_ticket(draft);
let _ = response_channel.send(id);
}
Ok(Command::Get {
id,
response_channel,
}) => {
let ticket = store.get(id);
let _ = response_channel.send(ticket);
}
Err(_) => {
// There are no more senders, so we can safely break
// and shut down the server.
break;
}
}
}
}