From 43b2e02534f4e4d28e2ccc34dba1a7a4c5324b8a Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 29 Mar 2010 02:23:21 -0400 Subject: [PATCH] vm: split up TLS code and add a dummy implementation for a dummy OS known as NetBSD --- GNUmakefile | 1 + Nmakefile | 2 ++ vm/Config.freebsd | 2 +- vm/Config.linux | 2 +- vm/Config.macosx | 2 +- vm/Config.netbsd | 2 +- vm/Config.openbsd | 2 +- vm/Config.windows.nt | 2 +- vm/factor.cpp | 27 +-------------------------- vm/factor.hpp | 2 +- vm/master.hpp | 1 + vm/mvm-none.cpp | 28 ++++++++++++++++++++++++++++ vm/mvm-unix.cpp | 26 ++++++++++++++++++++++++++ vm/mvm-windows-nt.cpp | 27 +++++++++++++++++++++++++++ vm/mvm.cpp | 29 +++++++++++++++++++++++++++++ vm/mvm.hpp | 12 ++++++++++++ vm/os-unix.cpp | 20 -------------------- vm/os-unix.hpp | 5 ----- vm/os-windows-nt.cpp | 21 --------------------- vm/os-windows-nt.hpp | 4 ---- vm/vm.hpp | 2 -- 21 files changed, 134 insertions(+), 85 deletions(-) create mode 100644 vm/mvm-none.cpp create mode 100644 vm/mvm-unix.cpp create mode 100644 vm/mvm-windows-nt.cpp create mode 100644 vm/mvm.cpp create mode 100644 vm/mvm.hpp diff --git a/GNUmakefile b/GNUmakefile index eac1c696df..12ca388f87 100755 --- a/GNUmakefile +++ b/GNUmakefile @@ -52,6 +52,7 @@ ifdef CONFIG vm/io.o \ vm/jit.o \ vm/math.o \ + vm/mvm.o \ vm/nursery_collector.o \ vm/object_start_map.o \ vm/objects.o \ diff --git a/Nmakefile b/Nmakefile index 7349deae23..a73a59d0f5 100755 --- a/Nmakefile +++ b/Nmakefile @@ -38,6 +38,8 @@ DLL_OBJS = vm\os-windows-nt.obj \ vm\io.obj \ vm\jit.obj \ vm\math.obj \ + vm\mvm.obj \ + vm\mvm-windows-nt.obj \ vm\nursery_collector.obj \ vm\object_start_map.obj \ vm\objects.obj \ diff --git a/vm/Config.freebsd b/vm/Config.freebsd index a0dbe228e5..4dc56cfaed 100644 --- a/vm/Config.freebsd +++ b/vm/Config.freebsd @@ -1,4 +1,4 @@ include vm/Config.unix -PLAF_DLL_OBJS += vm/os-genunix.o vm/os-freebsd.o +PLAF_DLL_OBJS += vm/os-genunix.o vm/os-freebsd.o vm/mvm-unix.o CFLAGS += -export-dynamic LIBS = -L/usr/local/lib/ -lm -lrt $(X11_UI_LIBS) diff --git a/vm/Config.linux b/vm/Config.linux index 4a859b1216..00ff73522a 100644 --- a/vm/Config.linux +++ b/vm/Config.linux @@ -1,4 +1,4 @@ include vm/Config.unix -PLAF_DLL_OBJS += vm/os-genunix.o vm/os-linux.o +PLAF_DLL_OBJS += vm/os-genunix.o vm/os-linux.o vm/mvm-unix.o CFLAGS += -export-dynamic LIBS = -ldl -lm -lrt -lpthread $(X11_UI_LIBS) diff --git a/vm/Config.macosx b/vm/Config.macosx index 89fe239668..5b9de7f5cf 100644 --- a/vm/Config.macosx +++ b/vm/Config.macosx @@ -1,7 +1,7 @@ include vm/Config.unix CFLAGS += -fPIC -PLAF_DLL_OBJS += vm/os-macosx.o vm/mach_signal.o +PLAF_DLL_OBJS += vm/os-macosx.o vm/mach_signal.o vm/mvm-unix.o DLL_EXTENSION = .dylib SHARED_DLL_EXTENSION = .dylib diff --git a/vm/Config.netbsd b/vm/Config.netbsd index 72a4056c90..2838f9d4c5 100644 --- a/vm/Config.netbsd +++ b/vm/Config.netbsd @@ -1,5 +1,5 @@ include vm/Config.unix -PLAF_DLL_OBJS += vm/os-genunix.o vm/os-netbsd.o +PLAF_DLL_OBJS += vm/os-genunix.o vm/os-netbsd.o vm/mvm-none.o CFLAGS += -export-dynamic LIBPATH = -L/usr/X11R7/lib -Wl,-rpath,/usr/X11R7/lib -L/usr/pkg/lib -Wl,-rpath,/usr/pkg/lib LIBS = -lm -lrt -lssl -lcrypto $(X11_UI_LIBS) diff --git a/vm/Config.openbsd b/vm/Config.openbsd index c7d2672e6b..6983223b74 100644 --- a/vm/Config.openbsd +++ b/vm/Config.openbsd @@ -1,5 +1,5 @@ include vm/Config.unix -PLAF_DLL_OBJS += vm/os-genunix.o vm/os-openbsd.o +PLAF_DLL_OBJS += vm/os-genunix.o vm/os-openbsd.o vm/mvm-unix.o CC = egcc CPP = eg++ CFLAGS += -export-dynamic -fno-inline-functions diff --git a/vm/Config.windows.nt b/vm/Config.windows.nt index ffaa899fe1..322649dc06 100644 --- a/vm/Config.windows.nt +++ b/vm/Config.windows.nt @@ -1,7 +1,7 @@ LIBS = -lm EXE_SUFFIX= DLL_SUFFIX= -PLAF_DLL_OBJS += vm/os-windows-nt.o +PLAF_DLL_OBJS += vm/os-windows-nt.o vm/mvm-windows-nt.o PLAF_EXE_OBJS += vm/resources.o PLAF_EXE_OBJS += vm/main-windows-nt.o CFLAGS += -mwindows diff --git a/vm/factor.cpp b/vm/factor.cpp index c33db440a0..e726ebf6da 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -3,11 +3,9 @@ namespace factor { -std::map thread_vms; - void init_globals() { - init_platform_globals(); + init_mvm(); } void factor_vm::default_parameters(vm_parameters *p) @@ -205,11 +203,6 @@ void factor_vm::start_standalone_factor(int argc, vm_char **argv) start_factor(&p); } -struct startargs { - int argc; - vm_char **argv; -}; - factor_vm *new_factor_vm() { factor_vm *newvm = new factor_vm(); @@ -219,28 +212,10 @@ factor_vm *new_factor_vm() return newvm; } -// arg must be new'ed because we're going to delete it! -void *start_standalone_factor_thread(void *arg) -{ - factor_vm *newvm = new_factor_vm(); - startargs *args = (startargs*) arg; - int argc = args->argc; vm_char **argv = args->argv; - delete args; - newvm->start_standalone_factor(argc, argv); - return 0; -} - VM_C_API void start_standalone_factor(int argc, vm_char **argv) { factor_vm *newvm = new_factor_vm(); return newvm->start_standalone_factor(argc,argv); } -VM_C_API THREADHANDLE start_standalone_factor_in_new_thread(int argc, vm_char **argv) -{ - startargs *args = new startargs; - args->argc = argc; args->argv = argv; - return start_thread(start_standalone_factor_thread,args); -} - } diff --git a/vm/factor.hpp b/vm/factor.hpp index cec59bcc5c..f2dd6af0bf 100755 --- a/vm/factor.hpp +++ b/vm/factor.hpp @@ -2,7 +2,7 @@ namespace factor { VM_C_API void init_globals(); +factor_vm *new_factor_vm(); VM_C_API void start_standalone_factor(int argc, vm_char **argv); -VM_C_API THREADHANDLE start_standalone_factor_in_new_thread(int argc, vm_char **argv); } diff --git a/vm/master.hpp b/vm/master.hpp index dca3d7473c..9879fa607a 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -132,6 +132,7 @@ namespace factor #include "jit.hpp" #include "quotations.hpp" #include "inline_cache.hpp" +#include "mvm.hpp" #include "factor.hpp" #include "utilities.hpp" diff --git a/vm/mvm-none.cpp b/vm/mvm-none.cpp new file mode 100644 index 0000000000..ab1b53a4b5 --- /dev/null +++ b/vm/mvm-none.cpp @@ -0,0 +1,28 @@ +#include "master.hpp" + +/* Multi-VM threading is not supported on NetBSD due to +http://gnats.netbsd.org/25563 */ + +namespace factor +{ + +factor_vm *global_vm; + +void init_mvm() +{ + global_vm = NULL; +} + +void register_vm_with_thread(factor_vm *vm) +{ + assert(!global_vm); + global_vm = vm; +} + +factor_vm *current_vm() +{ + assert(global_vm != NULL); + return global_vm; +} + +} diff --git a/vm/mvm-unix.cpp b/vm/mvm-unix.cpp new file mode 100644 index 0000000000..110e73f8af --- /dev/null +++ b/vm/mvm-unix.cpp @@ -0,0 +1,26 @@ +#include "master.hpp" + +namespace factor +{ + +pthread_key_t current_vm_tls_key = 0; + +void init_mvm() +{ + if(pthread_key_create(¤t_vm_tls_key, NULL) != 0) + fatal_error("pthread_key_create() failed",0); +} + +void register_vm_with_thread(factor_vm *vm) +{ + pthread_setspecific(current_vm_tls_key,vm); +} + +factor_vm *current_vm() +{ + factor_vm *vm = (factor_vm*)pthread_getspecific(current_vm_tls_key); + assert(vm != NULL); + return vm; +} + +} diff --git a/vm/mvm-windows-nt.cpp b/vm/mvm-windows-nt.cpp new file mode 100644 index 0000000000..7cb6b826a8 --- /dev/null +++ b/vm/mvm-windows-nt.cpp @@ -0,0 +1,27 @@ +#include "master.hpp" + +namespace factor +{ + +DWORD current_vm_tls_key; + +void init_mvm() +{ + if ((current_vm_tls_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) + fatal_error("TlsAlloc() failed",0); +} + +void register_vm_with_thread(factor_vm *vm) +{ + if (!TlsSetValue(current_vm_tls_key, vm)) + fatal_error("TlsSetValue() failed",0); +} + +factor_vm *current_vm() +{ + factor_vm *vm = (factor_vm *)TlsGetValue(current_vm_tls_key); + assert(vm != NULL); + return vm; +} + +} diff --git a/vm/mvm.cpp b/vm/mvm.cpp new file mode 100644 index 0000000000..dda2d66255 --- /dev/null +++ b/vm/mvm.cpp @@ -0,0 +1,29 @@ +#include "master.cpp" + +namespace factor +{ + +struct startargs { + int argc; + vm_char **argv; +}; + +// arg must be new'ed because we're going to delete it! +void *start_standalone_factor_thread(void *arg) +{ + factor_vm *newvm = new_factor_vm(); + startargs *args = (startargs*) arg; + int argc = args->argc; vm_char **argv = args->argv; + delete args; + newvm->start_standalone_factor(argc, argv); + return 0; +} + +VM_C_API THREADHANDLE start_standalone_factor_in_new_thread(int argc, vm_char **argv) +{ + startargs *args = new startargs; + args->argc = argc; args->argv = argv; + return start_thread(start_standalone_factor_thread,args); +} + +} diff --git a/vm/mvm.hpp b/vm/mvm.hpp new file mode 100644 index 0000000000..52430b7c01 --- /dev/null +++ b/vm/mvm.hpp @@ -0,0 +1,12 @@ +namespace factor +{ + +void init_mvm(); +void register_vm_with_thread(factor_vm *vm); +factor_vm *current_vm(); + +VM_C_API THREADHANDLE start_standalone_factor_in_new_thread(int argc, vm_char **argv); + +extern std::map thread_vms; + +} diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index a724007b1a..a8898eccab 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -17,26 +17,6 @@ THREADHANDLE start_thread(void *(*start_routine)(void *),void *args) return thread; } -pthread_key_t current_vm_tls_key = 0; - -void init_platform_globals() -{ - if(pthread_key_create(¤t_vm_tls_key, NULL) != 0) - fatal_error("pthread_key_create() failed",0); -} - -void register_vm_with_thread(factor_vm *vm) -{ - pthread_setspecific(current_vm_tls_key,vm); -} - -factor_vm *current_vm() -{ - factor_vm *vm = (factor_vm*)pthread_getspecific(current_vm_tls_key); - assert(vm != NULL); - return vm; -} - static void *null_dll; u64 system_micros() diff --git a/vm/os-unix.hpp b/vm/os-unix.hpp index df6e0b4b3e..3673c4e121 100644 --- a/vm/os-unix.hpp +++ b/vm/os-unix.hpp @@ -45,11 +45,6 @@ void dump_stack_signal(int signal, siginfo_t* siginfo, void* uap); u64 system_micros(); u64 nano_count(); void sleep_nanos(u64 nsec); - -void init_platform_globals(); - -void register_vm_with_thread(factor_vm *vm); -factor_vm *current_vm(); void open_console(); void move_file(const vm_char *path1, const vm_char *path2); diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index 54ee78f977..2d5881252a 100755 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -8,27 +8,6 @@ THREADHANDLE start_thread(void *(*start_routine)(void *), void *args) return (void *)CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, args, 0, 0); } -DWORD dwTlsIndex; - -void init_platform_globals() -{ - if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES) - fatal_error("TlsAlloc failed - out of indexes",0); -} - -void register_vm_with_thread(factor_vm *vm) -{ - if (! TlsSetValue(dwTlsIndex, vm)) - fatal_error("TlsSetValue failed",0); -} - -factor_vm *current_vm() -{ - factor_vm *vm = (factor_vm *)TlsGetValue(dwTlsIndex); - assert(vm != NULL); - return vm; -} - u64 system_micros() { FILETIME t; diff --git a/vm/os-windows-nt.hpp b/vm/os-windows-nt.hpp index d425a2c281..c5e721c56d 100755 --- a/vm/os-windows-nt.hpp +++ b/vm/os-windows-nt.hpp @@ -45,8 +45,4 @@ typedef HANDLE THREADHANDLE; THREADHANDLE start_thread(void *(*start_routine)(void *),void *args); inline static THREADHANDLE thread_id() { return GetCurrentThread(); } -void init_platform_globals(); -void register_vm_with_thread(factor_vm *vm); -factor_vm *current_vm(); - } diff --git a/vm/vm.hpp b/vm/vm.hpp index 4402b64f41..d304543879 100755 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -715,6 +715,4 @@ struct factor_vm ~factor_vm(); }; -extern std::map thread_vms; - } -- 2.34.1