diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.rs | 2 | ||||
| -rw-r--r-- | src/friends.rs | 4 | ||||
| -rw-r--r-- | src/lib.rs | 23 | ||||
| -rw-r--r-- | src/utils.rs | 2 |
4 files changed, 18 insertions, 13 deletions
@@ -5,7 +5,7 @@ pub struct AppId(pub u32); pub struct Apps { pub(crate) apps: *mut sys::ISteamApps, - pub(crate) _client: Rc<ClientInner>, + pub(crate) _client: Arc<ClientInner>, } impl Apps { diff --git a/src/friends.rs b/src/friends.rs index 4e82a37..5885e6f 100644 --- a/src/friends.rs +++ b/src/friends.rs @@ -43,7 +43,7 @@ bitflags! { } pub struct Friends { pub(crate) friends: *mut sys::ISteamFriends, - pub(crate) _client: Rc<ClientInner>, + pub(crate) _client: Arc<ClientInner>, } impl Friends { @@ -105,7 +105,7 @@ unsafe impl Callback for PersonaStateChange { pub struct Friend { id: SteamId, friends: *mut sys::ISteamFriends, - _client: Rc<ClientInner>, + _client: Arc<ClientInner>, } impl Debug for Friend { @@ -17,23 +17,28 @@ pub use app::*; mod friends; pub use friends::*; -use std::rc::Rc; -use std::cell::RefCell; +use std::sync::{Arc, Mutex}; use std::ffi::{CString, CStr}; use std::borrow::Cow; use std::fmt::{ Debug, Formatter, self }; +// A note about thread-safety: +// The steam api is assumed to be thread safe unless +// the documentation for a method states otherwise, +// however this is never stated anywhere in the docs +// that I could see. + pub struct Client { - inner: Rc<ClientInner>, + inner: Arc<ClientInner>, } struct ClientInner { client: *mut sys::ISteamClient, pipe: sys::HSteamPipe, - callbacks: RefCell<Vec<*mut libc::c_void>>, + callbacks: Mutex<Vec<*mut libc::c_void>>, } impl Client { @@ -43,10 +48,10 @@ impl Client { bail!(ErrorKind::InitFailed); } let client = sys::SteamInternal_CreateInterface(sys::STEAMCLIENT_INTERFACE_VERSION.as_ptr() as *const _); - let client = Rc::new(ClientInner { + let client = Arc::new(ClientInner { client: client, pipe: sys::SteamAPI_ISteamClient_CreateSteamPipe(client), - callbacks: RefCell::new(Vec::new()), + callbacks: Mutex::new(Vec::new()), }); Ok(Client { inner: client, @@ -62,7 +67,7 @@ impl Client { pub fn register_callback<C, F>(&self, f: F) where C: Callback, - F: FnMut(C) + 'static + F: FnMut(C) + 'static + Send + Sync { unsafe { let userdata = Box::into_raw(Box::new(f)); @@ -92,7 +97,7 @@ impl Client { dealloc::<C, F>, C::id() as _ ); - let mut cbs = self.inner.callbacks.borrow_mut(); + let mut cbs = self.inner.callbacks.lock().unwrap(); cbs.push(ptr); } } @@ -146,7 +151,7 @@ impl Client { impl Drop for ClientInner { fn drop(&mut self) { unsafe { - for cb in &**self.callbacks.borrow() { + for cb in &**self.callbacks.lock().unwrap() { sys::unregister_rust_steam_callback(*cb); } debug_assert!(sys::SteamAPI_ISteamClient_BReleaseSteamPipe(self.client, self.pipe) != 0); diff --git a/src/utils.rs b/src/utils.rs index 416b9ee..f95719f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,7 +3,7 @@ use super::*; pub struct Utils { pub(crate) utils: *mut sys::ISteamUtils, - pub(crate) _client: Rc<ClientInner>, + pub(crate) _client: Arc<ClientInner>, } impl Utils { |