void Application::InitializeMono() { //this will override the internal assembly search logic. //do it in case you package mono in a different structure //mono_set_assemblies_path(dllDir.c_str()); mono_config_parse(NULL); // initialize the root domain which will hold corlib and will always be alive domain = mono_jit_init_version("CCubed Root Domain", "v4.0.30319"); // soft debugger needs this mono_thread_set_main(mono_thread_current()); // add icalls mono_add_internal_call("EmbedThings.EntryPoint::reload", reload); // run the c# bits Run(); // we're exiting the whole thing, cleanup mono_jit_cleanup(mono_domain_get()); }
MonoThread *get_current_thread() { return mono_thread_current(); }
void detach_current_thread() { ERR_FAIL_COND(!GDMono::get_singleton()->is_runtime_initialized()); MonoThread *mono_thread = mono_thread_current(); ERR_FAIL_NULL(mono_thread); mono_thread_detach(mono_thread); }
static void async_invoke_thread (gpointer data) { MonoDomain *domain; MonoWSQ *wsq; ThreadPool *tp; gboolean must_die; tp = data; wsq = NULL; if (!tp->is_io) wsq = add_wsq (); set_tp_thread_info (tp); if (tp_start_func) tp_start_func (tp_hooks_user_data); data = NULL; for (;;) { MonoAsyncResult *ar; MonoClass *klass; gboolean is_io_task; gboolean is_socket; int n_naps = 0; is_io_task = FALSE; ar = (MonoAsyncResult *) data; if (ar) { InterlockedIncrement (&tp->busy_threads); domain = ((MonoObject *)ar)->vtable->domain; #ifndef DISABLE_SOCKETS klass = ((MonoObject *) data)->vtable->klass; is_io_task = !is_corlib_asyncresult (domain, klass); is_socket = FALSE; if (is_io_task) { MonoSocketAsyncResult *state = (MonoSocketAsyncResult *) data; is_socket = is_socketasyncresult (domain, klass); ar = state->ares; switch (state->operation) { case AIO_OP_RECEIVE: state->total = ICALL_RECV (state); break; case AIO_OP_SEND: state->total = ICALL_SEND (state); break; } } #endif /* worker threads invokes methods in different domains, * so we need to set the right domain here */ g_assert (domain); if (mono_domain_is_unloading (domain) || mono_runtime_is_shutting_down ()) { threadpool_jobs_dec ((MonoObject *)ar); data = NULL; ar = NULL; InterlockedDecrement (&tp->busy_threads); } else { mono_thread_push_appdomain_ref (domain); if (threadpool_jobs_dec ((MonoObject *)ar)) { data = NULL; ar = NULL; mono_thread_pop_appdomain_ref (); InterlockedDecrement (&tp->busy_threads); continue; } if (mono_domain_set (domain, FALSE)) { MonoObject *exc; if (tp_item_begin_func) tp_item_begin_func (tp_item_user_data); exc = mono_async_invoke (tp, ar); if (tp_item_end_func) tp_item_end_func (tp_item_user_data); if (exc) mono_internal_thread_unhandled_exception (exc); if (is_socket && tp->is_io) { MonoSocketAsyncResult *state = (MonoSocketAsyncResult *) data; if (state->completed && state->callback) { MonoAsyncResult *cb_ares; cb_ares = create_simple_asyncresult ((MonoObject *) state->callback, (MonoObject *) state); icall_append_job ((MonoObject *) cb_ares); } } mono_domain_set (mono_get_root_domain (), TRUE); } mono_thread_pop_appdomain_ref (); InterlockedDecrement (&tp->busy_threads); clear_thread_state (); } } ar = NULL; data = NULL; must_die = should_i_die (tp); if (must_die) { mono_wsq_suspend (wsq); } else { if (tp->is_io || !mono_wsq_local_pop (&data)) dequeue_or_steal (tp, &data, wsq); } n_naps = 0; while (!must_die && !data && n_naps < 4) { gboolean res; InterlockedIncrement (&tp->waiting); // Another thread may have added a job into its wsq since the last call to dequeue_or_steal // Check all the queues again before entering the wait loop dequeue_or_steal (tp, &data, wsq); if (data) { InterlockedDecrement (&tp->waiting); break; } mono_gc_set_skip_thread (TRUE); #if defined(__OpenBSD__) while (mono_cq_count (tp->queue) == 0 && (res = mono_sem_wait (&tp->new_job, TRUE)) == -1) {// && errno == EINTR) { #else while (mono_cq_count (tp->queue) == 0 && (res = mono_sem_timedwait (&tp->new_job, 2000, TRUE)) == -1) {// && errno == EINTR) { #endif if (mono_runtime_is_shutting_down ()) break; check_for_interruption_critical (); } InterlockedDecrement (&tp->waiting); mono_gc_set_skip_thread (FALSE); if (mono_runtime_is_shutting_down ()) break; must_die = should_i_die (tp); dequeue_or_steal (tp, &data, wsq); n_naps++; } if (!data && !tp->is_io && !mono_runtime_is_shutting_down ()) { mono_wsq_local_pop (&data); if (data && must_die) { InterlockedCompareExchange (&tp->destroy_thread, 1, 0); pulse_on_new_job (tp); } } if (!data) { gint nt; gboolean down; while (1) { nt = tp->nthreads; down = mono_runtime_is_shutting_down (); if (!down && nt <= tp->min_threads) break; if (down || InterlockedCompareExchange (&tp->nthreads, nt - 1, nt) == nt) { #ifndef DISABLE_PERFCOUNTERS mono_perfcounter_update_value (tp->pc_nthreads, TRUE, -1); #endif if (!tp->is_io) { remove_wsq (wsq); } fire_profiler_thread_end (); if (tp_finish_func) tp_finish_func (tp_hooks_user_data); if (!tp->is_io) { if (threads) { mono_mutex_lock (&threads_lock); if (threads) g_ptr_array_remove_fast (threads, mono_thread_current ()->internal_thread); mono_mutex_unlock (&threads_lock); } } return; } } } } g_assert_not_reached (); } void ves_icall_System_Threading_ThreadPool_GetAvailableThreads (gint *workerThreads, gint *completionPortThreads) { *workerThreads = async_tp.max_threads - async_tp.busy_threads; *completionPortThreads = async_io_tp.max_threads - async_io_tp.busy_threads; }
void mono_init_virt () { const char *error; #ifndef MONO_AGENT char * cfg_mono_root_path; char * cfg_mono_cfg_dir; char * cfg_mono_path; char * cfg_mono_trace; /*g_log_set_always_fatal (G_LOG_LEVEL_ERROR); g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);*/ #ifndef VIRT_MINT if (virtuoso_cfg_getstring ("Mono", "MONO_TRACE", &cfg_mono_trace) != -1) mono_jit_trace_calls = strcmp (cfg_mono_trace, "On") ? FALSE : TRUE; #endif if (virtuoso_cfg_getstring ("Mono", "MONO_PATH", &cfg_mono_path) != -1) setenv ("MONO_PATH", cfg_mono_path, 1); if (virtuoso_cfg_getstring ("Mono", "MONO_ROOT", &cfg_mono_root_path) != -1) setenv ("MONO_ROOT", cfg_mono_root_path, 1); if (virtuoso_cfg_getstring ("Mono", "MONO_CFG_DIR", &cfg_mono_cfg_dir) != -1) setenv ("MONO_CFG_DIR", cfg_mono_cfg_dir, 1); if (virtuoso_cfg_getstring ("Mono", "virtclr.dll", &VIRTCLR_NAME) == -1) VIRTCLR_NAME = "virtclr.dll"; #endif #ifdef WIN32 /* mono initialization on win32 has to be done sooner than later */ #ifdef VIRT_MINT virtuoso_domain = mono_interp_init ("virtuoso"); #else virtuoso_domain = mono_jit_init ("virtuoso"); #endif if (cfg_mono_root_path) mono_assembly_setrootdir (cfg_mono_root_path); #endif #ifndef VIRT_MINT mono_jit_trace_calls = FALSE; #endif { char *path = getenv ("MONO_ROOT"); if (path) mono_assembly_setrootdir (path); } /* mono_debug_init (1); */ #ifndef WIN32 setlocale(LC_ALL, ""); #endif g_log_set_always_fatal (G_LOG_LEVEL_ERROR); g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR); g_set_printerr_handler (dummy_print); #ifndef WIN32 #ifdef VIRT_MINT virtuoso_domain = mono_interp_init ("virtuoso"); #else virtuoso_domain = mono_jit_init ("virtuoso"); #endif mono_config_parse (NULL); #ifdef OLD_KIT if (NULL != (error = mono_verify_corlib ())) #elif !defined (OLD_KIT_1_1_5) if (NULL != (error = mono_check_corlib_version ())) #endif { log_error ("Mono Corlib not in sync with this runtime: %s", error); exit (-1); } #ifndef VIRT_MINT #ifdef OLD_KIT_1_1_5 mono_thread_attach_aborted_cb = virt_mono_throw_unhandled_exception; #else mono_thread_set_attach_aborted_cb (virt_mono_throw_unhandled_exception); #endif #endif #endif mono_add_internal_call ("VInvoke::LoadAssemblyFromVirtuoso(string)", ves_icall_VInvoke_LoadAssemblyFromVirtuoso); #ifndef MONO_AGENT mono_set_port (); #endif #ifndef MONO_AGENT #ifdef OLD_KIT_1_1_4 log_debug ("Mono config path [%s]", mono_cfg_dir); #else log_debug ("Mono config path [%s]", mono_get_config_dir ()); #endif #endif #ifndef WIN32 setlocale (LC_ALL, "C"); #endif #ifndef OLD_KIT_1_1_5 mono_thread_manage (); mono_thread_set_main (mono_thread_current()); #endif }