Commit fd5653ac1d

fd5653ac1dfe7f6dcceee7e47d0267e8888721af

parent: 06f76c3c7f

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2019-11-16 20:07 UTC

executor: Return JoinHandles instead of dropping them.

Layout: unified · split

src/executor.rs +6 −5
@@ -1,4 +1,4 @@
1use async_task; 1use async_task::JoinHandle;
2use pipe_channel::{channel, Receiver, Sender}; 2use pipe_channel::{channel, Receiver, Sender};
3use std::collections::VecDeque; 3use std::collections::VecDeque;
4use std::future::Future; 4use std::future::Future;
@@ -33,10 +33,10 @@ static mut _FUTURE_QUEUE: Option<FutureQueue> = None;
33 33
34type FutureQueue = Arc<Mutex<VecDeque<Job>>>; 34type FutureQueue = Arc<Mutex<VecDeque<Job>>>;
35 35
36pub fn spawn_weechat<F, T>(future: F) 36pub fn spawn_weechat<F, R>(future: F) -> JoinHandle<R, ()>
37where 37where
38 F: Future<Output = T> + 'static, 38 F: Future<Output = R> + 'static,
39 T: 'static, 39 R: 'static,
40{ 40{
41 let weechat = unsafe { Weechat::weechat() }; 41 let weechat = unsafe { Weechat::weechat() };
42 42
@@ -83,8 +83,9 @@ where
83 weechat_notify.send(()).unwrap(); 83 weechat_notify.send(()).unwrap();
84 }; 84 };
85 85
86 let (task, _handle) = async_task::spawn(future, schedule, ()); 86 let (task, handle) = async_task::spawn(future, schedule, ());
87 task.schedule(); 87 task.schedule();
88 handle
88} 89}
89 90
90pub fn cleanup_executor() { 91pub fn cleanup_executor() {
src/server.rs +6 −2
@@ -1,5 +1,6 @@
1use async_std::sync::channel as async_channel; 1use async_std::sync::channel as async_channel;
2use async_std::sync::{Receiver, Sender}; 2use async_std::sync::{Receiver, Sender};
3use async_task::JoinHandle;
3use std::collections::HashMap; 4use std::collections::HashMap;
4use std::time::Duration; 5use std::time::Duration;
5use tokio::runtime::Runtime; 6use tokio::runtime::Runtime;
@@ -50,6 +51,7 @@ pub struct LoginState {
50 51
51struct ConnectedState { 52struct ConnectedState {
52 client_channel: Sender<ServerMessage>, 53 client_channel: Sender<ServerMessage>,
54 sync_receiver: JoinHandle<(), ()>,
53 homeserver: Url, 55 homeserver: Url,
54 runtime: Runtime, 56 runtime: Runtime,
55} 57}
@@ -95,13 +97,14 @@ impl MatrixServer {
95 97
96 let (tx, rx) = async_channel(1000); 98 let (tx, rx) = async_channel(1000);
97 runtime.spawn(MatrixServer::sync_loop(client, tx)); 99 runtime.spawn(MatrixServer::sync_loop(client, tx));
98 spawn_weechat(MatrixServer::sync_receiver(rx)); 100 let sync_receiver_handle =
101 spawn_weechat(MatrixServer::sync_receiver(rx));
99 102
100 let (client_sender, client_receiver) = async_channel(10); 103 let (client_sender, client_receiver) = async_channel(10);
101
102 runtime.spawn(MatrixServer::send_loop(send_client, client_receiver)); 104 runtime.spawn(MatrixServer::send_loop(send_client, client_receiver));
103 105
104 self.connected_state = Some(ConnectedState { 106 self.connected_state = Some(ConnectedState {
107 sync_receiver: sync_receiver_handle,
105 client_channel: client_sender, 108 client_channel: client_sender,
106 homeserver, 109 homeserver,
107 runtime, 110 runtime,
@@ -112,6 +115,7 @@ impl MatrixServer {
112 let connected_state = self.connected_state.take(); 115 let connected_state = self.connected_state.take();
113 if let Some(s) = connected_state { 116 if let Some(s) = connected_state {
114 s.runtime.shutdown_now(); 117 s.runtime.shutdown_now();
118 s.sync_receiver.cancel();
115 } 119 }
116 } 120 }
117 121