gd_mono.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /*************************************************************************/
  2. /* gd_mono.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  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. #include "gd_mono.h"
  31. #include <mono/metadata/mono-config.h>
  32. #include <mono/metadata/mono-debug.h>
  33. #include <mono/metadata/mono-gc.h>
  34. #include "os/dir_access.h"
  35. #include "os/file_access.h"
  36. #include "os/os.h"
  37. #include "os/thread.h"
  38. #include "project_settings.h"
  39. #include "../csharp_script.h"
  40. #include "../utils/path_utils.h"
  41. #include "gd_mono_utils.h"
  42. #ifdef TOOLS_ENABLED
  43. #include "../editor/godotsharp_editor.h"
  44. #endif
  45. #ifdef MONO_PRINT_HANDLER_ENABLED
  46. void gdmono_MonoPrintCallback(const char *string, mono_bool is_stdout) {
  47. if (is_stdout) {
  48. OS::get_singleton()->print(string);
  49. } else {
  50. OS::get_singleton()->printerr(string);
  51. }
  52. }
  53. #endif
  54. GDMono *GDMono::singleton = NULL;
  55. #ifdef DEBUG_ENABLED
  56. static bool _wait_for_debugger_msecs(uint32_t p_msecs) {
  57. do {
  58. if (mono_is_debugger_attached())
  59. return true;
  60. int last_tick = OS::get_singleton()->get_ticks_msec();
  61. OS::get_singleton()->delay_usec((p_msecs < 25 ? p_msecs : 25) * 1000);
  62. int tdiff = OS::get_singleton()->get_ticks_msec() - last_tick;
  63. if (tdiff > p_msecs) {
  64. p_msecs = 0;
  65. } else {
  66. p_msecs -= tdiff;
  67. }
  68. } while (p_msecs > 0);
  69. return mono_is_debugger_attached();
  70. }
  71. #endif
  72. #ifdef TOOLS_ENABLED
  73. // temporary workaround. should be provided from Main::setup/setup2 instead
  74. bool _is_project_manager_requested() {
  75. List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();
  76. for (List<String>::Element *E = cmdline_args.front(); E; E = E->next()) {
  77. const String &arg = E->get();
  78. if (arg == "-p" || arg == "--project-manager")
  79. return true;
  80. }
  81. return false;
  82. }
  83. #endif
  84. #ifdef DEBUG_ENABLED
  85. void gdmono_debug_init() {
  86. mono_debug_init(MONO_DEBUG_FORMAT_MONO);
  87. int da_port = GLOBAL_DEF("mono/debugger_agent/port", 23685);
  88. bool da_suspend = GLOBAL_DEF("mono/debugger_agent/wait_for_debugger", false);
  89. int da_timeout = GLOBAL_DEF("mono/debugger_agent/wait_timeout", 3000);
  90. #ifdef TOOLS_ENABLED
  91. if (Engine::get_singleton()->is_editor_hint() ||
  92. ProjectSettings::get_singleton()->get_resource_path().empty() ||
  93. _is_project_manager_requested()) {
  94. return;
  95. }
  96. #endif
  97. CharString da_args = String("--debugger-agent=transport=dt_socket,address=127.0.0.1:" + itos(da_port) +
  98. ",embedding=1,server=y,suspend=" + (da_suspend ? "y,timeout=" + itos(da_timeout) : "n"))
  99. .utf8();
  100. // --debugger-agent=help
  101. const char *options[] = {
  102. "--soft-breakpoints",
  103. da_args.get_data()
  104. };
  105. mono_jit_parse_options(2, (char **)options);
  106. }
  107. #endif
  108. void GDMono::initialize() {
  109. ERR_FAIL_NULL(Engine::get_singleton());
  110. OS::get_singleton()->print("Mono: Initializing module...\n");
  111. #ifdef DEBUG_METHODS_ENABLED
  112. _initialize_and_check_api_hashes();
  113. #endif
  114. GDMonoLog::get_singleton()->initialize();
  115. #ifdef MONO_PRINT_HANDLER_ENABLED
  116. mono_trace_set_print_handler(gdmono_MonoPrintCallback);
  117. mono_trace_set_printerr_handler(gdmono_MonoPrintCallback);
  118. #endif
  119. #ifdef WINDOWS_ENABLED
  120. mono_reg_info = MonoRegUtils::find_mono();
  121. CharString assembly_dir;
  122. CharString config_dir;
  123. if (mono_reg_info.assembly_dir.length() && DirAccess::exists(mono_reg_info.assembly_dir)) {
  124. assembly_dir = mono_reg_info.assembly_dir.utf8();
  125. }
  126. if (mono_reg_info.config_dir.length() && DirAccess::exists(mono_reg_info.config_dir)) {
  127. config_dir = mono_reg_info.config_dir.utf8();
  128. }
  129. mono_set_dirs(assembly_dir.length() ? assembly_dir.get_data() : NULL,
  130. config_dir.length() ? config_dir.get_data() : NULL);
  131. #else
  132. mono_set_dirs(NULL, NULL);
  133. #endif
  134. GDMonoAssembly::initialize();
  135. #ifdef DEBUG_ENABLED
  136. gdmono_debug_init();
  137. #endif
  138. mono_config_parse(NULL);
  139. root_domain = mono_jit_init_version("GodotEngine.RootDomain", "v4.0.30319");
  140. ERR_EXPLAIN("Mono: Failed to initialize runtime");
  141. ERR_FAIL_NULL(root_domain);
  142. GDMonoUtils::set_main_thread(GDMonoUtils::get_current_thread());
  143. runtime_initialized = true;
  144. OS::get_singleton()->print("Mono: Runtime initialized\n");
  145. // mscorlib assembly MUST be present at initialization
  146. ERR_EXPLAIN("Mono: Failed to load mscorlib assembly");
  147. ERR_FAIL_COND(!_load_corlib_assembly());
  148. #ifdef TOOLS_ENABLED
  149. // The tools domain must be loaded here, before the scripts domain.
  150. // Otherwise domain unload on the scripts domain will hang indefinitely.
  151. ERR_EXPLAIN("Mono: Failed to load tools domain");
  152. ERR_FAIL_COND(_load_tools_domain() != OK);
  153. // TODO move to editor init callback, and do it lazily when required before editor init (e.g.: bindings generation)
  154. ERR_EXPLAIN("Mono: Failed to load Editor Tools assembly");
  155. ERR_FAIL_COND(!_load_editor_tools_assembly());
  156. #endif
  157. ERR_EXPLAIN("Mono: Failed to load scripts domain");
  158. ERR_FAIL_COND(_load_scripts_domain() != OK);
  159. #ifdef DEBUG_ENABLED
  160. bool debugger_attached = _wait_for_debugger_msecs(500);
  161. if (!debugger_attached && OS::get_singleton()->is_stdout_verbose())
  162. OS::get_singleton()->printerr("Mono: Debugger wait timeout\n");
  163. #endif
  164. _register_internal_calls();
  165. // The following assemblies are not required at initialization
  166. _load_all_script_assemblies();
  167. OS::get_singleton()->print("Mono: ALL IS GOOD\n");
  168. }
  169. #ifndef MONO_GLUE_DISABLED
  170. namespace GodotSharpBindings {
  171. uint64_t get_core_api_hash();
  172. uint64_t get_editor_api_hash();
  173. void register_generated_icalls();
  174. } // namespace GodotSharpBindings
  175. #endif
  176. void GDMono::_register_internal_calls() {
  177. #ifndef MONO_GLUE_DISABLED
  178. GodotSharpBindings::register_generated_icalls();
  179. #endif
  180. #ifdef TOOLS_ENABLED
  181. GodotSharpBuilds::_register_internal_calls();
  182. #endif
  183. }
  184. #ifdef DEBUG_METHODS_ENABLED
  185. void GDMono::_initialize_and_check_api_hashes() {
  186. api_core_hash = ClassDB::get_api_hash(ClassDB::API_CORE);
  187. #ifndef MONO_GLUE_DISABLED
  188. if (api_core_hash != GodotSharpBindings::get_core_api_hash()) {
  189. ERR_PRINT("Mono: Core API hash mismatch!");
  190. }
  191. #endif
  192. #ifdef TOOLS_ENABLED
  193. api_editor_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR);
  194. #ifndef MONO_GLUE_DISABLED
  195. if (api_editor_hash != GodotSharpBindings::get_editor_api_hash()) {
  196. ERR_PRINT("Mono: Editor API hash mismatch!");
  197. }
  198. #endif
  199. #endif // TOOLS_ENABLED
  200. }
  201. #endif // DEBUG_METHODS_ENABLED
  202. void GDMono::add_assembly(uint32_t p_domain_id, GDMonoAssembly *p_assembly) {
  203. assemblies[p_domain_id][p_assembly->get_name()] = p_assembly;
  204. }
  205. GDMonoAssembly **GDMono::get_loaded_assembly(const String &p_name) {
  206. MonoDomain *domain = mono_domain_get();
  207. uint32_t domain_id = domain ? mono_domain_get_id(domain) : 0;
  208. return assemblies[domain_id].getptr(p_name);
  209. }
  210. bool GDMono::_load_assembly(const String &p_name, GDMonoAssembly **r_assembly) {
  211. CRASH_COND(!r_assembly);
  212. if (OS::get_singleton()->is_stdout_verbose())
  213. OS::get_singleton()->print((String() + "Mono: Loading assembly " + p_name + "...\n").utf8());
  214. MonoImageOpenStatus status = MONO_IMAGE_OK;
  215. MonoAssemblyName *aname = mono_assembly_name_new(p_name.utf8());
  216. MonoAssembly *assembly = mono_assembly_load_full(aname, NULL, &status, false);
  217. mono_assembly_name_free(aname);
  218. if (!assembly)
  219. return false;
  220. uint32_t domain_id = mono_domain_get_id(mono_domain_get());
  221. GDMonoAssembly **stored_assembly = assemblies[domain_id].getptr(p_name);
  222. ERR_FAIL_COND_V(status != MONO_IMAGE_OK, false);
  223. ERR_FAIL_COND_V(stored_assembly == NULL, false);
  224. ERR_FAIL_COND_V((*stored_assembly)->get_assembly() != assembly, false);
  225. *r_assembly = *stored_assembly;
  226. if (OS::get_singleton()->is_stdout_verbose())
  227. OS::get_singleton()->print(String("Mono: Assembly " + p_name + " loaded from path: " + (*r_assembly)->get_path() + "\n").utf8());
  228. return true;
  229. }
  230. bool GDMono::_load_corlib_assembly() {
  231. if (corlib_assembly)
  232. return true;
  233. bool success = _load_assembly("mscorlib", &corlib_assembly);
  234. if (success)
  235. GDMonoUtils::update_corlib_cache();
  236. return success;
  237. }
  238. bool GDMono::_load_core_api_assembly() {
  239. if (api_assembly)
  240. return true;
  241. bool success = _load_assembly(API_ASSEMBLY_NAME, &api_assembly);
  242. if (success)
  243. GDMonoUtils::update_godot_api_cache();
  244. return success;
  245. }
  246. #ifdef TOOLS_ENABLED
  247. bool GDMono::_load_editor_api_assembly() {
  248. if (editor_api_assembly)
  249. return true;
  250. return _load_assembly(EDITOR_API_ASSEMBLY_NAME, &editor_api_assembly);
  251. }
  252. #endif
  253. #ifdef TOOLS_ENABLED
  254. bool GDMono::_load_editor_tools_assembly() {
  255. if (editor_tools_assembly)
  256. return true;
  257. _GDMONO_SCOPE_DOMAIN_(tools_domain)
  258. return _load_assembly(EDITOR_TOOLS_ASSEMBLY_NAME, &editor_tools_assembly);
  259. }
  260. #endif
  261. bool GDMono::_load_project_assembly() {
  262. if (project_assembly)
  263. return true;
  264. String project_assembly_name = ProjectSettings::get_singleton()->get("application/config/name");
  265. bool success = _load_assembly(project_assembly_name, &project_assembly);
  266. if (success)
  267. mono_assembly_set_main(project_assembly->get_assembly());
  268. return success;
  269. }
  270. bool GDMono::_load_all_script_assemblies() {
  271. #ifndef MONO_GLUE_DISABLED
  272. if (!_load_core_api_assembly()) {
  273. if (OS::get_singleton()->is_stdout_verbose())
  274. OS::get_singleton()->printerr("Mono: Failed to load Core API assembly\n");
  275. return false;
  276. } else {
  277. #ifdef TOOLS_ENABLED
  278. if (!_load_editor_api_assembly()) {
  279. if (OS::get_singleton()->is_stdout_verbose())
  280. OS::get_singleton()->printerr("Mono: Failed to load Editor API assembly\n");
  281. return false;
  282. }
  283. #endif
  284. }
  285. if (!_load_project_assembly()) {
  286. if (OS::get_singleton()->is_stdout_verbose())
  287. OS::get_singleton()->printerr("Mono: Failed to load project assembly\n");
  288. return false;
  289. }
  290. return true;
  291. #else
  292. if (OS::get_singleton()->is_stdout_verbose())
  293. OS::get_singleton()->print("Mono: Glue disbled, ignoring script assemblies\n");
  294. return true;
  295. #endif
  296. }
  297. Error GDMono::_load_scripts_domain() {
  298. ERR_FAIL_COND_V(scripts_domain != NULL, ERR_BUG);
  299. if (OS::get_singleton()->is_stdout_verbose()) {
  300. OS::get_singleton()->print("Mono: Loading scripts domain...\n");
  301. }
  302. scripts_domain = GDMonoUtils::create_domain("GodotEngine.ScriptsDomain");
  303. ERR_EXPLAIN("Mono: Could not create scripts app domain");
  304. ERR_FAIL_NULL_V(scripts_domain, ERR_CANT_CREATE);
  305. mono_domain_set(scripts_domain, true);
  306. return OK;
  307. }
  308. Error GDMono::_unload_scripts_domain() {
  309. ERR_FAIL_NULL_V(scripts_domain, ERR_BUG);
  310. if (OS::get_singleton()->is_stdout_verbose()) {
  311. OS::get_singleton()->print("Mono: Unloading scripts domain...\n");
  312. }
  313. _GodotSharp::get_singleton()->_dispose_callback();
  314. if (mono_domain_get() != root_domain)
  315. mono_domain_set(root_domain, true);
  316. mono_gc_collect(mono_gc_max_generation());
  317. finalizing_scripts_domain = true;
  318. mono_domain_finalize(scripts_domain, 2000);
  319. finalizing_scripts_domain = false;
  320. mono_gc_collect(mono_gc_max_generation());
  321. _domain_assemblies_cleanup(mono_domain_get_id(scripts_domain));
  322. api_assembly = NULL;
  323. project_assembly = NULL;
  324. #ifdef TOOLS_ENABLED
  325. editor_api_assembly = NULL;
  326. #endif
  327. MonoDomain *domain = scripts_domain;
  328. scripts_domain = NULL;
  329. _GodotSharp::get_singleton()->_dispose_callback();
  330. MonoObject *ex = NULL;
  331. mono_domain_try_unload(domain, &ex);
  332. if (ex) {
  333. ERR_PRINT("Exception thrown when unloading scripts domain:");
  334. mono_print_unhandled_exception(ex);
  335. return FAILED;
  336. }
  337. return OK;
  338. }
  339. #ifdef TOOLS_ENABLED
  340. Error GDMono::_load_tools_domain() {
  341. ERR_FAIL_COND_V(tools_domain != NULL, ERR_BUG);
  342. if (OS::get_singleton()->is_stdout_verbose()) {
  343. OS::get_singleton()->print("Mono: Loading tools domain...\n");
  344. }
  345. tools_domain = GDMonoUtils::create_domain("GodotEngine.ToolsDomain");
  346. ERR_EXPLAIN("Mono: Could not create tools app domain");
  347. ERR_FAIL_NULL_V(tools_domain, ERR_CANT_CREATE);
  348. return OK;
  349. }
  350. #endif
  351. #ifdef TOOLS_ENABLED
  352. Error GDMono::reload_scripts_domain() {
  353. ERR_FAIL_COND_V(!runtime_initialized, ERR_BUG);
  354. if (scripts_domain) {
  355. Error err = _unload_scripts_domain();
  356. if (err != OK) {
  357. ERR_PRINT("Mono: Failed to unload scripts domain");
  358. return err;
  359. }
  360. }
  361. Error err = _load_scripts_domain();
  362. if (err != OK) {
  363. ERR_PRINT("Mono: Failed to load scripts domain");
  364. return err;
  365. }
  366. if (!_load_all_script_assemblies()) {
  367. if (OS::get_singleton()->is_stdout_verbose())
  368. OS::get_singleton()->printerr("Mono: Failed to load script assemblies\n");
  369. return ERR_CANT_OPEN;
  370. }
  371. return OK;
  372. }
  373. #endif
  374. GDMonoClass *GDMono::get_class(MonoClass *p_raw_class) {
  375. MonoImage *image = mono_class_get_image(p_raw_class);
  376. if (image == corlib_assembly->get_image())
  377. return corlib_assembly->get_class(p_raw_class);
  378. uint32_t domain_id = mono_domain_get_id(mono_domain_get());
  379. HashMap<String, GDMonoAssembly *> &domain_assemblies = assemblies[domain_id];
  380. const String *k = NULL;
  381. while ((k = domain_assemblies.next(k))) {
  382. GDMonoAssembly *assembly = domain_assemblies.get(*k);
  383. if (assembly->get_image() == image) {
  384. GDMonoClass *klass = assembly->get_class(p_raw_class);
  385. if (klass)
  386. return klass;
  387. }
  388. }
  389. return NULL;
  390. }
  391. void GDMono::_domain_assemblies_cleanup(uint32_t p_domain_id) {
  392. HashMap<String, GDMonoAssembly *> &domain_assemblies = assemblies[p_domain_id];
  393. const String *k = NULL;
  394. while ((k = domain_assemblies.next(k))) {
  395. memdelete(domain_assemblies.get(*k));
  396. }
  397. assemblies.erase(p_domain_id);
  398. }
  399. GDMono::GDMono() {
  400. singleton = this;
  401. gdmono_log = memnew(GDMonoLog);
  402. runtime_initialized = false;
  403. finalizing_scripts_domain = false;
  404. root_domain = NULL;
  405. scripts_domain = NULL;
  406. #ifdef TOOLS_ENABLED
  407. tools_domain = NULL;
  408. #endif
  409. corlib_assembly = NULL;
  410. api_assembly = NULL;
  411. project_assembly = NULL;
  412. #ifdef TOOLS_ENABLED
  413. editor_api_assembly = NULL;
  414. editor_tools_assembly = NULL;
  415. #endif
  416. #ifdef DEBUG_METHODS_ENABLED
  417. api_core_hash = 0;
  418. #ifdef TOOLS_ENABLED
  419. api_editor_hash = 0;
  420. #endif
  421. #endif
  422. }
  423. GDMono::~GDMono() {
  424. if (runtime_initialized) {
  425. if (scripts_domain) {
  426. Error err = _unload_scripts_domain();
  427. if (err != OK) {
  428. WARN_PRINT("Mono: Failed to unload scripts domain");
  429. }
  430. }
  431. const uint32_t *k = NULL;
  432. while ((k = assemblies.next(k))) {
  433. HashMap<String, GDMonoAssembly *> &domain_assemblies = assemblies.get(*k);
  434. const String *kk = NULL;
  435. while ((kk = domain_assemblies.next(kk))) {
  436. memdelete(domain_assemblies.get(*kk));
  437. }
  438. }
  439. assemblies.clear();
  440. GDMonoUtils::clear_cache();
  441. OS::get_singleton()->print("Mono: Runtime cleanup...\n");
  442. runtime_initialized = false;
  443. mono_jit_cleanup(root_domain);
  444. }
  445. if (gdmono_log)
  446. memdelete(gdmono_log);
  447. }
  448. _GodotSharp *_GodotSharp::singleton = NULL;
  449. void _GodotSharp::_dispose_object(Object *p_object) {
  450. if (p_object->get_script_instance()) {
  451. CSharpInstance *cs_instance = CAST_CSHARP_INSTANCE(p_object->get_script_instance());
  452. if (cs_instance) {
  453. cs_instance->mono_object_disposed();
  454. return;
  455. }
  456. }
  457. // Unsafe refcount decrement. The managed instance also counts as a reference.
  458. // See: CSharpLanguage::alloc_instance_binding_data(Object *p_object)
  459. if (Object::cast_to<Reference>(p_object)->unreference()) {
  460. memdelete(p_object);
  461. }
  462. }
  463. void _GodotSharp::_dispose_callback() {
  464. #ifndef NO_THREADS
  465. queue_mutex->lock();
  466. #endif
  467. for (List<Object *>::Element *E = obj_delete_queue.front(); E; E = E->next()) {
  468. _dispose_object(E->get());
  469. }
  470. for (List<NodePath *>::Element *E = np_delete_queue.front(); E; E = E->next()) {
  471. memdelete(E->get());
  472. }
  473. for (List<RID *>::Element *E = rid_delete_queue.front(); E; E = E->next()) {
  474. memdelete(E->get());
  475. }
  476. obj_delete_queue.clear();
  477. np_delete_queue.clear();
  478. rid_delete_queue.clear();
  479. queue_empty = true;
  480. #ifndef NO_THREADS
  481. queue_mutex->unlock();
  482. #endif
  483. }
  484. void _GodotSharp::attach_thread() {
  485. GDMonoUtils::attach_current_thread();
  486. }
  487. void _GodotSharp::detach_thread() {
  488. GDMonoUtils::detach_current_thread();
  489. }
  490. bool _GodotSharp::is_finalizing_domain() {
  491. return GDMono::get_singleton()->is_finalizing_scripts_domain();
  492. }
  493. bool _GodotSharp::is_domain_loaded() {
  494. return GDMono::get_singleton()->get_scripts_domain() != NULL;
  495. }
  496. #define ENQUEUE_FOR_DISPOSAL(m_queue, m_inst) \
  497. m_queue.push_back(m_inst); \
  498. if (queue_empty) { \
  499. queue_empty = false; \
  500. call_deferred("_dispose_callback"); \
  501. }
  502. void _GodotSharp::queue_dispose(Object *p_object) {
  503. if (Thread::get_main_id() == Thread::get_caller_id() && !GDMono::get_singleton()->is_finalizing_scripts_domain()) {
  504. _dispose_object(p_object);
  505. } else {
  506. #ifndef NO_THREADS
  507. queue_mutex->lock();
  508. #endif
  509. ENQUEUE_FOR_DISPOSAL(obj_delete_queue, p_object);
  510. #ifndef NO_THREADS
  511. queue_mutex->unlock();
  512. #endif
  513. }
  514. }
  515. void _GodotSharp::queue_dispose(NodePath *p_node_path) {
  516. if (Thread::get_main_id() == Thread::get_caller_id() && !GDMono::get_singleton()->is_finalizing_scripts_domain()) {
  517. memdelete(p_node_path);
  518. } else {
  519. #ifndef NO_THREADS
  520. queue_mutex->lock();
  521. #endif
  522. ENQUEUE_FOR_DISPOSAL(np_delete_queue, p_node_path);
  523. #ifndef NO_THREADS
  524. queue_mutex->unlock();
  525. #endif
  526. }
  527. }
  528. void _GodotSharp::queue_dispose(RID *p_rid) {
  529. if (Thread::get_main_id() == Thread::get_caller_id() && !GDMono::get_singleton()->is_finalizing_scripts_domain()) {
  530. memdelete(p_rid);
  531. } else {
  532. #ifndef NO_THREADS
  533. queue_mutex->lock();
  534. #endif
  535. ENQUEUE_FOR_DISPOSAL(rid_delete_queue, p_rid);
  536. #ifndef NO_THREADS
  537. queue_mutex->unlock();
  538. #endif
  539. }
  540. }
  541. void _GodotSharp::_bind_methods() {
  542. ClassDB::bind_method(D_METHOD("attach_thread"), &_GodotSharp::attach_thread);
  543. ClassDB::bind_method(D_METHOD("detach_thread"), &_GodotSharp::detach_thread);
  544. ClassDB::bind_method(D_METHOD("is_finalizing_domain"), &_GodotSharp::is_finalizing_domain);
  545. ClassDB::bind_method(D_METHOD("is_domain_loaded"), &_GodotSharp::is_domain_loaded);
  546. ClassDB::bind_method(D_METHOD("_dispose_callback"), &_GodotSharp::_dispose_callback);
  547. }
  548. _GodotSharp::_GodotSharp() {
  549. singleton = this;
  550. queue_empty = true;
  551. #ifndef NO_THREADS
  552. queue_mutex = Mutex::create();
  553. #endif
  554. }
  555. _GodotSharp::~_GodotSharp() {
  556. singleton = NULL;
  557. if (queue_mutex) {
  558. memdelete(queue_mutex);
  559. }
  560. }