[Mono-devel-list] Multiple application domains

Casper Hornstrup chorns at users.sourceforge.net
Tue Apr 22 16:28:55 EDT 2003


This test is performed using Mono compiled from CVS HEAD as of today.
However, I also have Mono installed from latest rpm release.

I'm trying to run a Mono executable in a separate application domain
created using mono_domain_create().

If I run the application below, then it will work as expected. It will
print
"Test..." on the console. However, if I change the second #if
conditional
from #if 1 to #if 0, then the application hangs before displaing
"Test...".
Changing the first #if conditional has no effect.

Is multiple application domains not supported or am I doing this
incorrectly ?

Also, is mono_jit_cleanup() supposed to be called for each value
returned
by mono_domain_create() ?



#!/bin/sh
export PATH=/home/cvs/mono/install/bin:$PATH
gcc -v -L/home/cvs/mono/install/lib -I/home/cvs/mono/install/include \
  -g -o teste teste.c `pkg-config --cflags --libs mono` -lm
mcs test.cs
./teste test.exe



Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --host=i386-redhat-linux --with-system-zlib
--enable-__cxa_atexit
Thread model: posix
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
 /usr/lib/gcc-lib/i386-redhat-linux/3.2/cc1 -lang-c -v
-I/home/cvs/mono/install/include -I/usr/include/glib-2.0
-I/usr/lib/glib-2.0/include -D__GNUC__=3 -D__GNUC_MINOR__=2
-D__GNUC_PATCHLEVEL__=0 -D__GXX_ABI_VERSION=102 -D__ELF__ -Dunix
-D__gnu_linux__ -Dlinux -D__ELF__ -D__unix__ -D__gnu_linux__ -D__linux__
-D__unix -D__linux -Asystem=posix -D__NO_INLINE__ -D__STDC_HOSTED__=1
-Acpu=i386 -Amachine=i386 -Di386 -D__i386 -D__i386__ -D__tune_i386__
-DMONO_USE_EXC_TABLES -D_REENTRANT teste.c -quiet -dumpbase teste.c -g
-version -fexceptions -o /tmp/ccFbBEpg.s
GNU CPP version 3.2 20020903 (Red Hat Linux 8.0 3.2-7) (cpplib) (i386
Linux/ELF)
GNU C version 3.2 20020903 (Red Hat Linux 8.0 3.2-7) (i386-redhat-linux)
        compiled by GNU C version 3.2 20020903 (Red Hat Linux 8.0
3.2-7).
ignoring nonexistent directory "/usr/i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /home/cvs/mono/install/include
 /usr/include/glib-2.0
 /usr/lib/glib-2.0/include
 /usr/local/include
 /usr/lib/gcc-lib/i386-redhat-linux/3.2/include
 /usr/include
End of search list.
 as -V -Qy -o /tmp/ccpTBYwF.o /tmp/ccFbBEpg.s
GNU assembler version 2.13.90.0.2 (i386-redhat-linux) using BFD version
2.13.90.0.2 20020802
 /usr/lib/gcc-lib/i386-redhat-linux/3.2/collect2 --eh-frame-hdr -m
elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o teste
/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../crt1.o
/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../crti.o
/usr/lib/gcc-lib/i386-redhat-linux/3.2/crtbegin.o
-L/home/cvs/mono/install/lib -L/usr/lib/gcc-lib/i386-redhat-linux/3.2
-L/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../.. /tmp/ccpTBYwF.o
--export-dynamic -lmono -lpthread -lm -lgmodule-2.0 -ldl -lglib-2.0 -lm
-lgcc -lgcc_eh -lc -lgcc -lgcc_eh
/usr/lib/gcc-lib/i386-redhat-linux/3.2/crtend.o
/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../crtn.o
Compilation succeeded
Test...



// teste.c BEGIN
#include <mono/jit/jit.h>
#include <mono/metadata/environment.h>

/*
 * Very simple mono embedding example.
 * Compile with: 
 * 	gcc -o teste teste.c `pkg-config --cflags --libs mono` -lm
 * 	mcs test.cs
 * Run with:
 * 	./teste test.exe
 */

typedef struct
{
	MonoDomain *domain;
	const char *file;
	int argc;
	char **argv;
} MainThreadArgs;

static void main_thread_handler (gpointer user_data)
{
	MainThreadArgs *main_args=(MainThreadArgs *)user_data;
	MonoAssembly *assembly;

	assembly = mono_domain_assembly_open (main_args->domain,
					      main_args->file);
	if (!assembly)
		exit (2);
	/*
	 * mono_jit_exec() will run the Main() method in the assembly.
	 * The return value needs to be looked up from
	 * System.Environment.ExitCode.
	 */
	mono_jit_exec (main_args->domain, assembly, main_args->argc,
		       main_args->argv);
}


int 
main(int argc, char* argv[]) {
	MonoDomain *master_domain;
	MonoDomain *secondary_domain;
	const char *file;
	int retval;
	MainThreadArgs main_args;
	
	if (argc < 2){
		fprintf (stderr, "Please provide an assembly to load");
		return 1;
	}
	file = argv [1];
	/*
	 * mono_jit_init() creates a domain: each assembly is
	 * loaded and run in a MonoDomain.
	 */
	master_domain = mono_jit_init (file);

	secondary_domain = mono_domain_create ();

#if 0
  // works
	main_args.domain=master_domain;
#else
  // works
  main_args.domain=secondary_domain;
#endif
	main_args.file=file;
	main_args.argc=argc-1;
	main_args.argv=argv+1;

#if 1
  // works
	mono_runtime_exec_managed_code (master_domain,
main_thread_handler,
					&main_args);
#else
  // fails
	mono_runtime_exec_managed_code (secondary_domain,
main_thread_handler,
					&main_args);

#endif

	retval=mono_environment_exitcode_get ();
	
  //mono_jit_cleanup (secondary_domain);
	mono_jit_cleanup (master_domain);
	return retval;
}
// teste.c END



// test.cs BEGIN
using System;

class Test {
	static void Main() {
    Console.WriteLine("Test...");
	}
}
// test.cs END




More information about the Mono-devel-list mailing list