feat: wait for threads to be ready on linux
parent
70384e56bb
commit
18ea974db7
|
@ -1,4 +1,8 @@
|
||||||
use std::{fs, sync::Arc, thread};
|
use std::{
|
||||||
|
fs,
|
||||||
|
sync::{atomic::AtomicUsize, Arc},
|
||||||
|
thread,
|
||||||
|
};
|
||||||
|
|
||||||
use cancellation::CancellationTokenSource;
|
use cancellation::CancellationTokenSource;
|
||||||
|
|
||||||
|
@ -12,6 +16,7 @@ pub static mut CANCELLATION_TOKEN: Option<Arc<CancellationTokenSource>> = None;
|
||||||
|
|
||||||
pub static mut STARTED: bool = false;
|
pub static mut STARTED: bool = false;
|
||||||
pub static mut ERROR: Option<Error> = None;
|
pub static mut ERROR: Option<Error> = None;
|
||||||
|
pub static mut ARC_READY_COUNT: Option<Arc<AtomicUsize>> = None;
|
||||||
|
|
||||||
pub fn start(callback: fn(Event)) -> Result<(), Error> {
|
pub fn start(callback: fn(Event)) -> Result<(), Error> {
|
||||||
if unsafe { STARTED } {
|
if unsafe { STARTED } {
|
||||||
|
@ -35,8 +40,11 @@ pub fn start(callback: fn(Event)) -> Result<(), Error> {
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
CANCELLATION_TOKEN = Some(Arc::new(cts));
|
CANCELLATION_TOKEN = Some(Arc::new(cts));
|
||||||
|
ARC_READY_COUNT = Some(Arc::new(AtomicUsize::new(0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut count = 0;
|
||||||
|
|
||||||
for path in dir {
|
for path in dir {
|
||||||
let filename = path.expect("Failed to get dir entry").file_name();
|
let filename = path.expect("Failed to get dir entry").file_name();
|
||||||
let filename: String = match filename.to_str() {
|
let filename: String = match filename.to_str() {
|
||||||
|
@ -50,7 +58,6 @@ pub fn start(callback: fn(Event)) -> Result<(), Error> {
|
||||||
.name("SkyHook".into())
|
.name("SkyHook".into())
|
||||||
.spawn(move || {
|
.spawn(move || {
|
||||||
if let Err(err) = start_reader(format!("/dev/input/{}", filename), callback) {
|
if let Err(err) = start_reader(format!("/dev/input/{}", filename), callback) {
|
||||||
println!("{:?}", err);
|
|
||||||
unsafe {
|
unsafe {
|
||||||
ERROR = Some(err);
|
ERROR = Some(err);
|
||||||
}
|
}
|
||||||
|
@ -63,6 +70,8 @@ pub fn start(callback: fn(Event)) -> Result<(), Error> {
|
||||||
message: format!("Failed to spawn thread: {:?}", err),
|
message: format!("Failed to spawn thread: {:?}", err),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
count += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,6 +81,23 @@ pub fn start(callback: fn(Event)) -> Result<(), Error> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while unsafe {
|
||||||
|
ERROR.is_none()
|
||||||
|
&& !{
|
||||||
|
if let Some(arc) = &ARC_READY_COUNT {
|
||||||
|
arc.load(std::sync::atomic::Ordering::Relaxed) < count
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} {
|
||||||
|
thread::yield_now();
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(err) = unsafe { &ERROR } {
|
||||||
|
return Err(err.clone());
|
||||||
|
}
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
STARTED = true;
|
STARTED = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use std::{fs::File, io::Read, time::SystemTime};
|
||||||
|
|
||||||
use crate::types::{Error, Event, EventData};
|
use crate::types::{Error, Event, EventData};
|
||||||
|
|
||||||
use super::CANCELLATION_TOKEN;
|
use super::{ARC_READY_COUNT, CANCELLATION_TOKEN};
|
||||||
|
|
||||||
fn convert_bit(bits: &[u8]) -> u16 {
|
fn convert_bit(bits: &[u8]) -> u16 {
|
||||||
let mut result: u16 = 0;
|
let mut result: u16 = 0;
|
||||||
|
@ -23,6 +23,12 @@ pub fn start_reader(file_path: String, callback: fn(Event)) -> Result<(), Error>
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
if let Some(arc) = &ARC_READY_COUNT {
|
||||||
|
arc.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut buffer = [0; 24];
|
let mut buffer = [0; 24];
|
||||||
|
|
||||||
|
|
Reference in New Issue