Commit 61a3db2fae

61a3db2fae6800f383b62044c7ac415546a8a9f6

parent: 2c7a42368e

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-01-18 10:07 UTC

executor: Move the executor to the rust-weechat repo.

Layout: unified · split

Cargo.toml +2 −5
@@ -11,15 +11,12 @@ crate-type = ["dylib"]
11 11
12[dependencies] 12[dependencies]
13libc = "0.2.66" 13libc = "0.2.66"
14futures = "0.3.1"
15url = "2.1.1" 14url = "2.1.1"
16tokio = { version = "0.2.9", features = [ "rt-threaded" ] } 15tokio = { version = "0.2.9", features = [ "rt-threaded" ] }
17async-std = { version = "1.4.0", features = [ "unstable" ] } 16async-std = { version = "1.4.0", features = [ "unstable" ] }
18async-task = "1.2.1"
19pipe-channel = "1.2.2"
20clap = "2.33.0" 17clap = "2.33.0"
21 18
22matrix-nio = { git = "https://github.com/poljar/nio-rust" } 19matrix-nio = { git = "https://github.com/poljar/nio-rust" }
23 20
24weechat = { git = "https://github.com/poljar/rust-weechat/" } 21weechat = { path = "/home/poljar/werk/weechat/rust-weechat/weechat-rs/", features = ["async-executor"] }
25weechat-sys = { git = "https://github.com/poljar/rust-weechat/" } 22weechat-sys = { path = "/home/poljar/werk/weechat/rust-weechat/weechat-sys" }
Makefile +1 −1
@@ -3,7 +3,7 @@ PREFIX ?= $(WEECHAT_HOME)
3 3
4.PHONY: install install-dir phony lint 4.PHONY: install install-dir phony lint
5 5
6target/debug/libmatrix.so: src/lib.rs src/server.rs src/room_buffer.rs src/executor.rs src/commands.rs src/config.rs 6target/debug/libmatrix.so: src/lib.rs src/server.rs src/room_buffer.rs src/commands.rs src/config.rs
7 cargo build 7 cargo build
8 8
9install: install-dir target/debug/libmatrix.so 9install: install-dir target/debug/libmatrix.so
src/executor.rs deleted −117
@@ -1,117 +0,0 @@
1use async_task::JoinHandle;
2use pipe_channel::{channel, Receiver, Sender};
3use std::collections::VecDeque;
4use std::future::Future;
5use std::sync::{Arc, Mutex, Weak};
6
7use weechat::{FdHook, FdHookMode, Weechat};
8
9static mut _EXECUTOR: Option<WeechatExecutor> = None;
10
11type Job = async_task::Task<()>;
12type FutureQueue = Arc<Mutex<VecDeque<Job>>>;
13type FutureQueueHandle = Weak<Mutex<VecDeque<Job>>>;
14
15pub struct WeechatExecutor {
16 _hook: Option<FdHook<FutureQueueHandle, Receiver<()>>>,
17 sender: Arc<Mutex<Sender<()>>>,
18 futures: FutureQueue,
19}
20
21impl WeechatExecutor {
22 fn new() -> Self {
23 let weechat = unsafe { Weechat::weechat() };
24
25 let (sender, receiver) = channel();
26 let sender = Arc::new(Mutex::new(sender));
27 let queue = Arc::new(Mutex::new(VecDeque::new()));
28
29 let hook = weechat
30 .hook_fd(
31 receiver,
32 FdHookMode::Read,
33 WeechatExecutor::executor_cb,
34 Some(Arc::downgrade(&queue)),
35 )
36 .expect("Can't create executor FD hook");
37
38 WeechatExecutor {
39 _hook: Some(hook),
40 sender,
41 futures: queue,
42 }
43 }
44
45 fn executor_cb(future_queue: &FutureQueueHandle, receiver: &mut Receiver<()>) {
46 if receiver.recv().is_err() {
47 return;
48 }
49
50 let futures = future_queue.upgrade();
51
52 if let Some(q) = futures {
53 let mut futures = q.lock().unwrap();
54 let task = futures.pop_front();
55
56 // Drop the lock here so we can spawn new futures from the currently
57 // running one.
58 drop(futures);
59
60 if let Some(task) = task {
61 task.run();
62 }
63 }
64 }
65
66 pub fn free() {
67 unsafe {
68 _EXECUTOR.take();
69 }
70 }
71}
72
73pub fn spawn_weechat<F, R>(future: F) -> JoinHandle<R, ()>
74where
75 F: Future<Output = R> + 'static,
76 R: 'static,
77{
78 let executor = unsafe {
79 if _EXECUTOR.is_none() {
80 let executor = WeechatExecutor::new();
81 _EXECUTOR = Some(executor);
82 }
83
84 _EXECUTOR.as_ref().expect("Executor wasn't started")
85 };
86
87 let sender = Arc::downgrade(&executor.sender);
88 let queue = Arc::downgrade(&executor.futures);
89
90 let schedule = move |task| {
91 let sender = sender.upgrade();
92 let queue = queue.upgrade();
93
94 if let Some(q) = queue {
95 let sender = sender
96 .expect("Futures queue exists but the channel got dropped");
97 let mut weechat_notify = sender
98 .lock()
99 .expect("Weechat notification sender lock is poisoned");
100
101 let mut queue = q.lock().expect(
102 "Lock of the future queue of the Weechat executor is poisoned",
103 );
104
105 queue.push_back(task);
106 weechat_notify
107 .send(())
108 .expect("Can't notify Weechat to run a future");
109 }
110 };
111
112 let (task, handle) = async_task::spawn_local(future, schedule, ());
113
114 task.schedule();
115
116 handle
117}
src/lib.rs +1 −5
@@ -2,7 +2,6 @@
2 2
3mod commands; 3mod commands;
4mod config; 4mod config;
5mod executor;
6mod room_buffer; 5mod room_buffer;
7mod server; 6mod server;
8 7
@@ -16,7 +15,6 @@ use weechat::{
16 15
17use crate::commands::Commands; 16use crate::commands::Commands;
18use crate::config::Config; 17use crate::config::Config;
19use crate::executor::{spawn_weechat, WeechatExecutor};
20use crate::server::MatrixServer; 18use crate::server::MatrixServer;
21 19
22const PLUGIN_NAME: &str = "matrix"; 20const PLUGIN_NAME: &str = "matrix";
@@ -111,7 +109,7 @@ impl WeechatPlugin for Matrix {
111 } 109 }
112 } 110 }
113 111
114 spawn_weechat(async move { 112 Weechat::spawn(async move {
115 let mut servers = servers.borrow_mut(); 113 let mut servers = servers.borrow_mut();
116 Matrix::autoconnect(&mut servers); 114 Matrix::autoconnect(&mut servers);
117 }); 115 });
@@ -126,8 +124,6 @@ impl Drop for Matrix {
126 for server in servers.values_mut() { 124 for server in servers.values_mut() {
127 server.disconnect(); 125 server.disconnect();
128 } 126 }
129
130 WeechatExecutor::free();
131 } 127 }
132} 128}
133 129
src/room_buffer.rs +1 −2
@@ -6,7 +6,6 @@ use matrix_nio::events::room::message::{
6use matrix_nio::Room; 6use matrix_nio::Room;
7use url::Url; 7use url::Url;
8 8
9use crate::executor::spawn_weechat;
10use crate::server::Connection; 9use crate::server::Connection;
11use crate::{Config, PLUGIN_NAME}; 10use crate::{Config, PLUGIN_NAME};
12use std::borrow::Cow; 11use std::borrow::Cow;
@@ -98,7 +97,7 @@ impl RoomBuffer {
98 s.send_message(&room_id, &input).await; 97 s.send_message(&room_id, &input).await;
99 } 98 }
100 }; 99 };
101 spawn_weechat(task); 100 Weechat::spawn(task);
102 } 101 }
103 102
104 pub fn close_callback(data: &String, buffer: &Buffer) {} 103 pub fn close_callback(data: &String, buffer: &Buffer) {}
src/server.rs +2 −3
@@ -1,6 +1,5 @@
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;
4use std::cell::RefCell; 3use std::cell::RefCell;
5use std::collections::HashMap; 4use std::collections::HashMap;
6use std::rc::{Rc, Weak}; 5use std::rc::{Rc, Weak};
@@ -19,12 +18,12 @@ use matrix_nio::{
19 AsyncClient, AsyncClientConfig, SyncSettings, 18 AsyncClient, AsyncClientConfig, SyncSettings,
20}; 19};
21 20
21use weechat::JoinHandle;
22use weechat::config::{ 22use weechat::config::{
23 BooleanOptionSettings, ConfigSection, StringOptionSettings, 23 BooleanOptionSettings, ConfigSection, StringOptionSettings,
24}; 24};
25use weechat::Weechat; 25use weechat::Weechat;
26 26
27use crate::executor::spawn_weechat;
28use crate::room_buffer::RoomBuffer; 27use crate::room_buffer::RoomBuffer;
29use crate::Config; 28use crate::Config;
30use crate::PLUGIN_NAME; 29use crate::PLUGIN_NAME;
@@ -336,7 +335,7 @@ impl MatrixServer {
336 335
337 let (tx, rx) = async_channel(1000); 336 let (tx, rx) = async_channel(1000);
338 runtime.spawn(MatrixServer::sync_loop(client.clone(), tx.clone())); 337 runtime.spawn(MatrixServer::sync_loop(client.clone(), tx.clone()));
339 let response_receiver = spawn_weechat(MatrixServer::response_receiver( 338 let response_receiver = Weechat::spawn(MatrixServer::response_receiver(
340 rx, 339 rx,
341 Rc::downgrade(&self.inner), 340 Rc::downgrade(&self.inner),
342 )); 341 ));