Skip to content

Commit ae75bd1

Browse files
committed
Chore: InstanceStorage method order
1 parent 8503df8 commit ae75bd1

File tree

1 file changed

+57
-55
lines changed

1 file changed

+57
-55
lines changed

godot-core/src/storage.rs

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,6 @@ mod single_threaded {
5454
}
5555
}
5656

57-
pub(crate) fn on_inc_ref(&self) {
58-
let refc = self.godot_ref_count.get() + 1;
59-
self.godot_ref_count.set(refc);
60-
61-
out!(
62-
" Storage::on_inc_ref (rc={}) <{}>", // -- {:?}",
63-
refc,
64-
type_name::<T>(),
65-
//self.user_instance
66-
);
67-
}
68-
69-
pub(crate) fn on_dec_ref(&self) {
70-
let refc = self.godot_ref_count.get() - 1;
71-
self.godot_ref_count.set(refc);
72-
73-
out!(
74-
" | Storage::on_dec_ref (rc={}) <{}>", // -- {:?}",
75-
refc,
76-
type_name::<T>(),
77-
//self.user_instance
78-
);
79-
}
80-
8157
pub fn is_bound(&self) -> bool {
8258
// Needs to borrow mutably, otherwise it succeeds if shared borrows are alive.
8359
self.user_instance.try_borrow_mut().is_err()
@@ -115,6 +91,30 @@ mod single_threaded {
11591
pub(super) fn godot_ref_count(&self) -> u32 {
11692
self.godot_ref_count.get()
11793
}
94+
95+
pub(crate) fn on_inc_ref(&self) {
96+
let refc = self.godot_ref_count.get() + 1;
97+
self.godot_ref_count.set(refc);
98+
99+
out!(
100+
" Storage::on_inc_ref (rc={}) <{}>", // -- {:?}",
101+
refc,
102+
type_name::<T>(),
103+
//self.user_instance
104+
);
105+
}
106+
107+
pub(crate) fn on_dec_ref(&self) {
108+
let refc = self.godot_ref_count.get() - 1;
109+
self.godot_ref_count.set(refc);
110+
111+
out!(
112+
" | Storage::on_dec_ref (rc={}) <{}>", // -- {:?}",
113+
refc,
114+
type_name::<T>(),
115+
//self.user_instance
116+
);
117+
}
118118
}
119119
}
120120

@@ -180,24 +180,6 @@ mod multi_threaded {
180180
}
181181
}
182182

183-
pub(crate) fn on_inc_ref(&self) {
184-
self.godot_ref_count.fetch_add(1, Ordering::Relaxed);
185-
out!(
186-
" Storage::on_inc_ref (rc={}) <{:?}>",
187-
self.godot_ref_count(),
188-
self.base,
189-
);
190-
}
191-
192-
pub(crate) fn on_dec_ref(&self) {
193-
self.godot_ref_count.fetch_sub(1, Ordering::Relaxed);
194-
out!(
195-
" | Storage::on_dec_ref (rc={}) <{:?}>",
196-
self.godot_ref_count(),
197-
self.base,
198-
);
199-
}
200-
201183
pub fn is_bound(&self) -> bool {
202184
// Needs to borrow mutably, otherwise it succeeds if shared borrows are alive.
203185
self.write_ignoring_poison().is_none()
@@ -225,6 +207,35 @@ mod multi_threaded {
225207
})
226208
}
227209

210+
pub fn get_gd(&self) -> Gd<T>
211+
where
212+
T: Inherits<<T as GodotClass>::Base>,
213+
{
214+
self.base.clone().cast()
215+
}
216+
217+
pub(super) fn godot_ref_count(&self) -> u32 {
218+
self.godot_ref_count.load(Ordering::Relaxed)
219+
}
220+
221+
pub(crate) fn on_inc_ref(&self) {
222+
self.godot_ref_count.fetch_add(1, Ordering::Relaxed);
223+
out!(
224+
" Storage::on_inc_ref (rc={}) <{:?}>",
225+
self.godot_ref_count(),
226+
self.base,
227+
);
228+
}
229+
230+
pub(crate) fn on_dec_ref(&self) {
231+
self.godot_ref_count.fetch_sub(1, Ordering::Relaxed);
232+
out!(
233+
" | Storage::on_dec_ref (rc={}) <{:?}>",
234+
self.godot_ref_count(),
235+
self.base,
236+
);
237+
}
238+
228239
/// Returns a write guard (even if poisoned), or `None` when the lock is held by another thread.
229240
/// This might need adjustment if threads should await locks.
230241
#[must_use]
@@ -247,17 +258,6 @@ mod multi_threaded {
247258
}
248259
}
249260

250-
pub fn get_gd(&self) -> Gd<T>
251-
where
252-
T: Inherits<<T as GodotClass>::Base>,
253-
{
254-
self.base.clone().cast()
255-
}
256-
257-
pub(super) fn godot_ref_count(&self) -> u32 {
258-
self.godot_ref_count.load(Ordering::Relaxed)
259-
}
260-
261261
// fn __static_type_check() {
262262
// enforce_sync::<InstanceStorage<T>>();
263263
// }
@@ -267,10 +267,12 @@ mod multi_threaded {
267267
// This type can be accessed concurrently from multiple threads, so it should be Sync. That implies however that T must be Sync too
268268
// (and possibly Send, because with `&mut` access, a `T` can be extracted as a value using mem::take() etc.).
269269
// Which again means that we need to infest half the codebase with T: Sync + Send bounds, *and* make it all conditional on
270-
// `#[cfg(feature = "experimental-threads")]`. Until the multi-threading design is clarified, we'll thus leave it as is.
270+
// `#[cfg(feature = "experimental-threads")]`.
271+
//
272+
// A better design would be a distinct Gds<T: Sync> pointer, which requires synchronized.
273+
// This needs more design on the multi-threading front (#18).
271274
//
272275
// The following code + __static_type_check() above would make sure that InstanceStorage is Sync.
273-
274276
// Make sure storage is Sync in multi-threaded case, as it can be concurrently accessed through aliased Gd<T> pointers.
275277
// fn enforce_sync<T: Sync>() {}
276278
}

0 commit comments

Comments
 (0)