-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathref_count.odin
More file actions
50 lines (42 loc) · 1.29 KB
/
ref_count.odin
File metadata and controls
50 lines (42 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package gpu
// Core
import "base:runtime"
import intr "base:intrinsics"
Refcount_Type :: distinct u32
Ref_Count :: struct {
count: Refcount_Type,
}
ref_count_init :: proc "contextless" (ref: ^Ref_Count, loc: runtime.Source_Code_Location) {
old_value := intr.atomic_add(&ref.count, 1)
assert_contextless(old_value == 0, "Reference counter already initialized", loc)
}
ref_count_add :: #force_inline proc "contextless" (
ref: ^Ref_Count,
loc: runtime.Source_Code_Location,
) {
old_count := intr.atomic_add(&ref.count, 1)
assert_contextless(
old_count > 0,
"Attempting to add reference to destroyed/invalid object",
loc,
)
}
ref_count_sub :: #force_inline proc "contextless" (
ref: ^Ref_Count,
loc: runtime.Source_Code_Location,
) -> (
should_release: bool,
) {
old_count := intr.atomic_sub(&ref.count, 1)
assert_contextless(old_count > 0, "Reference count underflow", loc)
should_release = old_count == 1 // Was this the last reference?
return
}
@(require_results)
ref_count_get :: #force_inline proc "contextless" (ref: ^Ref_Count) -> Refcount_Type {
return intr.atomic_load(&ref.count)
}
@(require_results)
ref_count_is_unique :: #force_inline proc "contextless" (ref: ^Ref_Count) -> bool {
return ref_count_get(ref) == 1
}