aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatthew Collins <[email protected]>2018-02-17 15:54:03 +0000
committerMatthew Collins <[email protected]>2018-02-17 15:54:03 +0000
commit06cc226be524fe7ea19db0b4426bfd0bed12e9fe (patch)
tree109f3c1c22205d99de3980afd724d41ba74a6a83 /src
parentUpdate deps (diff)
downloadarchived-steamworks-rs-06cc226be524fe7ea19db0b4426bfd0bed12e9fe.tar.xz
archived-steamworks-rs-06cc226be524fe7ea19db0b4426bfd0bed12e9fe.zip
Allow the use of the api across threads
Diffstat (limited to 'src')
-rw-r--r--src/app.rs2
-rw-r--r--src/friends.rs4
-rw-r--r--src/lib.rs23
-rw-r--r--src/utils.rs2
4 files changed, 18 insertions, 13 deletions
diff --git a/src/app.rs b/src/app.rs
index 2ba0b44..169bbed 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -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 {
diff --git a/src/lib.rs b/src/lib.rs
index 02b6c10..a3988f6 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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 {