Debian Codegraph @

core/object/ref_counted.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* ref_counted.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "ref_counted.h"
32
33 #include "core/object/script_language.h"
34
35 bool RefCounted::init_ref() {
36 if (reference()) {
37 if (!is_referenced() && refcount_init.unref()) {
38 unreference(); // first referencing is already 1, so compensate for the ref above
39 }
40
41 return true;
42 } else {
43 return false;
44 }
45 }
46
47 void RefCounted::_bind_methods() {
48 ClassDB::bind_method(D_METHOD("init_ref"), &RefCounted::init_ref);
49 ClassDB::bind_method(D_METHOD("reference"), &RefCounted::reference);
50 ClassDB::bind_method(D_METHOD("unreference"), &RefCounted::unreference);
51 ClassDB::bind_method(D_METHOD("get_reference_count"), &RefCounted::get_reference_count);
52 }
53
54 int RefCounted::get_reference_count() const {
55 return refcount.get();
56 }
57
58 bool RefCounted::reference() {
59 uint32_t rc_val = refcount.refval();
60 bool success = rc_val != 0;
61
62 if (success && rc_val <= 2 /* higher is not relevant */) {
63 if (get_script_instance()) {
64 get_script_instance()->refcount_incremented();
65 }
66 if (_get_extension() && _get_extension()->reference) {
67 _get_extension()->reference(_get_extension_instance());
68 }
69
70 _instance_binding_reference(true);
71 }
72
73 return success;
74 }
75
76 bool RefCounted::unreference() {
77 dereference_count.increment();
78 uint32_t rc_val = refcount.unrefval();
79 bool die = rc_val == 0;
80
81 if (rc_val <= 1 /* higher is not relevant */) {
82 if (get_script_instance()) {
83 bool script_ret = get_script_instance()->refcount_decremented();
84 die = die && script_ret;
85 }
86 if (_get_extension() && _get_extension()->unreference) {
87 _get_extension()->unreference(_get_extension_instance());
88 }
89
90 bool binding_ret = _instance_binding_reference(false);
91 die = die && binding_ret;
92 }
93
94 dereference_count.decrement();
95
96 // If we are going to be destroyed we need to ensure that no other thread
97 // is still inside our critical section. If they are they might see
98 // a (partially) destroyed Object for get_script_instance, _get_extension,
99 // or _instance_binding_reference.
100 if (die) {
101 // It is unlikely that we will spin here for very long.
102 // Only threads that see die == true will spin, which should only
103 // ever be one. Only threads seeing rc_val == 1 and rc_val == 0
104 // will do anything at all in the critical section.
105 while (dereference_count.get()) {
106 // Spin
107 }
108 }
109
110 return die;
111 }
112
113 RefCounted::RefCounted() :
114 Object(true) {
115 _define_ancestry(AncestralClass::REF_COUNTED);
116 refcount.init();
117 refcount_init.init();
118 dereference_count.set(0);
119 }
120
121 Variant WeakRef::get_ref() const {
122 if (ref.is_null()) {
123 return Variant();
124 }
125
126 Object *obj = ObjectDB::get_instance(ref);
127 if (!obj) {
128 return Variant();
129 }
130 RefCounted *r = cast_to<RefCounted>(obj);
131 if (r) {
132 return Ref<RefCounted>(r);
133 }
134
135 return obj;
136 }
137
138 void WeakRef::set_obj(Object *p_object) {
139 ref = p_object ? p_object->get_instance_id() : ObjectID();
140 }
141
142 void WeakRef::set_ref(const Ref<RefCounted> &p_ref) {
143 ref = p_ref.is_valid() ? p_ref->get_instance_id() : ObjectID();
144 }
145
146 void WeakRef::_bind_methods() {
147 ClassDB::bind_method(D_METHOD("get_ref"), &WeakRef::get_ref);
148 }