Added mutex-based locking mechanism as in imgui-rs

This commit is contained in:
4bb4 2020-10-11 15:42:30 +02:00
parent 74833663a9
commit 5d4d570507
3 changed files with 27 additions and 1 deletions

View file

@ -12,6 +12,8 @@ categories = ["gui", "api-bindings"]
[dependencies] [dependencies]
implot-sys = { version = "0.1.0", path = "implot-sys" } implot-sys = { version = "0.1.0", path = "implot-sys" }
bitflags = "1.0" bitflags = "1.0"
parking_lot = "0.11"
lazy_static = "1.1"
[workspace] [workspace]

View file

@ -1,3 +1,5 @@
use parking_lot::ReentrantMutex;
// TODO(4bb4) Do this properly. // TODO(4bb4) Do this properly.
// I already added a simple Context struct that can be created once and used as long as it is not // I already added a simple Context struct that can be created once and used as long as it is not
// dropped here for initial tests - this is of course neither threadsafe nor otherwise safe to use // dropped here for initial tests - this is of course neither threadsafe nor otherwise safe to use
@ -20,7 +22,7 @@
// I think I'll call this PlotUi to mimmick imgui-rs' Ui. // I think I'll call this PlotUi to mimmick imgui-rs' Ui.
// - Think about what this means in terms of the stacks and things like is_plot_hovered() - // - Think about what this means in terms of the stacks and things like is_plot_hovered() -
// they should also only work when there is a context available. // they should also only work when there is a context available.
//
/// An implot context. /// An implot context.
/// ///
/// A context is required to do most of the things this library provides. While this was created /// A context is required to do most of the things this library provides. While this was created
@ -29,9 +31,26 @@ pub struct Context {
raw: *mut sys::ImPlotContext, raw: *mut sys::ImPlotContext,
} }
lazy_static! {
// This mutex needs to be used to guard all public functions that can affect the underlying
// ImPlot active context
static ref CTX_MUTEX: ReentrantMutex<()> = ReentrantMutex::new(());
}
fn no_current_context() -> bool {
let ctx = unsafe { sys::ImPlot_GetCurrentContext() };
ctx.is_null()
}
impl Context { impl Context {
/// Create a context. /// Create a context.
pub fn create() -> Self { pub fn create() -> Self {
let _guard = CTX_MUTEX.lock();
assert!(
no_current_context(),
"A new active context cannot be created, because another one already exists"
);
let ctx = unsafe { sys::ImPlot_CreateContext() }; let ctx = unsafe { sys::ImPlot_CreateContext() };
unsafe { unsafe {
sys::ImPlot_SetCurrentContext(ctx); sys::ImPlot_SetCurrentContext(ctx);
@ -42,6 +61,7 @@ impl Context {
impl Drop for Context { impl Drop for Context {
fn drop(&mut self) { fn drop(&mut self) {
let _guard = CTX_MUTEX.lock();
unsafe { unsafe {
sys::ImPlot_DestroyContext(self.raw); sys::ImPlot_DestroyContext(self.raw);
} }

View file

@ -8,6 +8,10 @@
//! //!
//! //!
pub extern crate implot_sys as sys; pub extern crate implot_sys as sys;
#[macro_use]
extern crate lazy_static;
pub use sys::imgui::Condition; pub use sys::imgui::Condition;
// TODO(4bb4) facade-wrap these // TODO(4bb4) facade-wrap these
pub use sys::{ImPlotLimits, ImPlotPoint, ImPlotRange, ImVec2, ImVec4}; pub use sys::{ImPlotLimits, ImPlotPoint, ImPlotRange, ImVec2, ImVec4};