-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
149 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
pub mod blockers; | ||
/// Contains modules for working with journal log files providing readers for individual files or | ||
/// all files at once. | ||
pub mod logs; | ||
pub mod journal_dir; | ||
|
||
/// Contains structs and enums which are shared across events. Things like commodity and material | ||
/// names, ship types, exobiology data etc. can be found here. | ||
pub mod shared; | ||
|
||
/// Provides different implementations for blocking current execution, whether it be synchronous or | ||
/// asynchronous. | ||
mod blockers; | ||
|
||
/// Provides some utility functions and macros that are used internally. | ||
mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,46 @@ | ||
use std::sync::{Arc, Mutex}; | ||
use tokio::sync::mpsc::{channel, Sender}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct AsyncBlocker { | ||
waiting_sender: Arc<Mutex<(Option<Sender<()>>,)>>, | ||
} | ||
|
||
impl AsyncBlocker { | ||
pub fn new() -> Self { | ||
AsyncBlocker { | ||
waiting_sender: Arc::new(Mutex::new((None,))), | ||
} | ||
} | ||
|
||
pub async fn block(&self) { | ||
let (sender, mut receiver) = channel(2); | ||
|
||
{ | ||
let mut guard = self.waiting_sender.lock() | ||
.expect("to gotten lock"); | ||
|
||
guard.0 = Some(sender); | ||
} | ||
|
||
receiver.recv().await | ||
.expect("Failed to perform async block"); | ||
} | ||
|
||
pub fn unblock_blocking(&self) { | ||
let mut guard = self.waiting_sender | ||
.lock() | ||
.expect("Should have been locked"); | ||
|
||
if let Some(sender) = guard.0.as_ref() { | ||
if sender.is_closed() { | ||
return; | ||
} | ||
|
||
sender.blocking_send(()) | ||
.expect("Failed to send"); | ||
|
||
guard.0 = None; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
mod sync_blocker; | ||
mod async_blocker; | ||
pub mod sync_blocker; | ||
pub mod async_blocker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,37 @@ | ||
use std::sync::{Arc, Mutex}; | ||
use std::thread; | ||
use std::thread::Thread; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct SyncBlocker { | ||
|
||
waiting_thread: Arc<Mutex<(Option<Thread>,)>>, | ||
} | ||
|
||
impl SyncBlocker { | ||
pub fn new() -> Self { | ||
SyncBlocker { | ||
waiting_thread: Arc::new(Mutex::new((None,))), | ||
} | ||
} | ||
|
||
pub fn unblock(&self) { | ||
{ | ||
let mut guard = self.waiting_thread.lock().expect("to have gotten a lock"); | ||
|
||
guard.0 = Some(thread::current()); | ||
} | ||
|
||
thread::park(); | ||
} | ||
|
||
pub fn block(&self) { | ||
let mut guard = self.waiting_thread | ||
.lock() | ||
.expect("Should have been locked"); | ||
|
||
if let Some(thread) = guard.0.as_ref() { | ||
thread.unpark(); | ||
guard.0 = None; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.