Single watcher thread
This commit is contained in:
parent
dd4210ad6f
commit
21cc37e80c
1 changed files with 66 additions and 51 deletions
|
@ -11,6 +11,7 @@ use std::cell::RefCell;
|
|||
use std::ops::DerefMut;
|
||||
use std::sync::{Arc, Mutex, MutexGuard};
|
||||
use std::thread;
|
||||
use std::thread::JoinHandle;
|
||||
use std::time;
|
||||
|
||||
macro_rules! check_option {
|
||||
|
@ -30,6 +31,7 @@ pub struct Song {
|
|||
pub struct MediaData {
|
||||
playlist: RefCell<Vec<Song>>,
|
||||
current_song: Mutex<RefCell<Option<LockedAudio>>>,
|
||||
watcher_thread: RefCell<Option<JoinHandle<()>>>,
|
||||
}
|
||||
|
||||
fn guild_id(channel_id: ChannelId) -> Option<GuildId> {
|
||||
|
@ -57,6 +59,10 @@ impl MediaData {
|
|||
self.current_song.lock().unwrap()
|
||||
}
|
||||
|
||||
fn reset_thread(&self) {
|
||||
*self.watcher_thread.borrow_mut() = None;
|
||||
}
|
||||
|
||||
fn start_thread(
|
||||
media: &Arc<MediaData>,
|
||||
channel_id: ChannelId,
|
||||
|
@ -67,7 +73,10 @@ impl MediaData {
|
|||
let media_clone = media.clone();
|
||||
let manager_clone = manager_lock.clone();
|
||||
|
||||
thread::spawn(move || loop {
|
||||
let mut watcher_mut = media.watcher_thread.borrow_mut();
|
||||
|
||||
if watcher_mut.is_none() {
|
||||
*watcher_mut = Some(thread::spawn(move || loop {
|
||||
{
|
||||
let song_lock = media_clone.song_mut();
|
||||
let mut borrow = song_lock.borrow_mut();
|
||||
|
@ -87,6 +96,8 @@ impl MediaData {
|
|||
.lock()
|
||||
.remove(check_option!(guild_id(channel_id)));
|
||||
|
||||
media_clone.reset_thread();
|
||||
|
||||
println!("left channel");
|
||||
|
||||
return;
|
||||
|
@ -120,6 +131,8 @@ impl MediaData {
|
|||
.lock()
|
||||
.remove(check_option!(guild_id(channel_id)));
|
||||
|
||||
media_clone.reset_thread();
|
||||
|
||||
println!("left channel");
|
||||
|
||||
return;
|
||||
|
@ -128,7 +141,8 @@ impl MediaData {
|
|||
|
||||
let five_sec = time::Duration::from_secs(5);
|
||||
thread::sleep(five_sec);
|
||||
});
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
fn next_song(
|
||||
|
@ -172,6 +186,7 @@ impl Default for MediaData {
|
|||
MediaData {
|
||||
playlist: RefCell::new(Vec::new()),
|
||||
current_song: Mutex::new(RefCell::new(None)),
|
||||
watcher_thread: RefCell::new(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue