Skip to content

Commit 7153cf7

Browse files
committed
trying to move away from arc<mutex>
1 parent 2ce6063 commit 7153cf7

File tree

3 files changed

+25
-49
lines changed

3 files changed

+25
-49
lines changed

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "swaddle"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

src/main.rs

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{
77
collections::HashMap,
88
error::Error,
99
process::{Child, Command},
10+
sync::atomic::AtomicBool,
1011
sync::{Arc, Mutex},
1112
thread::sleep,
1213
time::{Duration, Instant},
@@ -16,20 +17,17 @@ trait DBusInterface {
1617
fn add_match(&self) -> Result<(), Box<dyn Error>>;
1718
}
1819

19-
trait CommandCaller {
20-
fn execute_command(&self) -> Result<(), Box<dyn Error>>;
21-
}
2220
struct DBusRunner {
2321
connection: Arc<Connection>,
24-
good_to_send: Arc<Mutex<bool>>,
22+
good_to_send: Arc<AtomicBool>,
2523
}
2624

2725
impl DBusRunner {
2826
pub fn new() -> Result<Self, Box<dyn Error>> {
2927
let connection = Connection::new_session()?;
3028
Ok(DBusRunner {
3129
connection: Arc::new(connection),
32-
good_to_send: Arc::new(Mutex::new(false)),
30+
good_to_send: Arc::new(AtomicBool::new(false)),
3331
})
3432
}
3533
}
@@ -40,21 +38,17 @@ impl DBusInterface for DBusRunner {
4038
.with_interface(INTERFACE_NAME)
4139
.with_namespaced_path(DBUS_NAMESPACE);
4240

43-
let sending_clone = Arc::clone(&self.good_to_send);
41+
let good_to_send = Arc::clone(&self.good_to_send);
4442
self.connection.add_match(rule, move |_: (), _, msg| {
4543
let items: HashMap<String, Variant<Box<dyn RefArg>>> =
4644
msg.read3::<String, HashMap<_, _>, Vec<String>>().unwrap().1;
4745
if let Some(playback_status) = items.get("PlaybackStatus") {
4846
if let Some(status) = playback_status.0.as_str() {
4947
if status == "Paused" {
50-
if let Ok(mut send_it) = sending_clone.lock() {
51-
*send_it = false;
52-
}
48+
good_to_send.store(false, std::sync::atomic::Ordering::SeqCst);
5349
}
5450
if status == "Playing" {
55-
if let Ok(mut send_it) = sending_clone.lock() {
56-
*send_it = true;
57-
}
51+
good_to_send.store(true, std::sync::atomic::Ordering::SeqCst);
5852
}
5953
}
6054
}
@@ -101,19 +95,21 @@ impl IdleApp {
10195
.process(Duration::from_millis(1000))
10296
{
10397
Ok(_) => {
104-
if let Ok(block) = self.dbus_runner.good_to_send.lock() {
105-
if *block {
106-
match self.run_cmd() {
107-
Ok(child) => inhibit.0 = Some(child),
108-
Err(e) => {
109-
eprintln!("unable to block swayidle :: {:?}", e)
110-
}
111-
}
112-
} else if !*block {
113-
if let Some(mut killing) = inhibit.0.take() {
114-
let _ = killing.kill();
98+
let block = self
99+
.dbus_runner
100+
.good_to_send
101+
.load(std::sync::atomic::Ordering::SeqCst);
102+
if block {
103+
match self.run_cmd() {
104+
Ok(child) => inhibit.0 = Some(child),
105+
Err(e) => {
106+
eprintln!("unable to block swayidle :: {:?}", e)
115107
}
116108
}
109+
} else if !block {
110+
if let Some(mut killing) = inhibit.0.take() {
111+
let _ = killing.kill();
112+
}
117113
}
118114
}
119115
Err(e) => eprintln!("Error handling D-Bus message: {:?}", e),
@@ -199,23 +195,3 @@ mod idle_app_tests {
199195
assert!(app.is_ok());
200196
}
201197
}
202-
203-
#[cfg(test)]
204-
mod command_caller_tests {
205-
use super::*;
206-
207-
struct MockCommandCaller;
208-
209-
impl CommandCaller for MockCommandCaller {
210-
fn execute_command(&self) -> Result<(), Box<dyn Error>> {
211-
Ok(())
212-
}
213-
}
214-
215-
#[test]
216-
fn test_execute_command() {
217-
let mock_caller = MockCommandCaller;
218-
let result = mock_caller.execute_command();
219-
assert!(result.is_ok());
220-
}
221-
}

0 commit comments

Comments
 (0)