Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Clean up thread registration code and DllMain thread registration. #305

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions libs/mod_neko/mod_neko.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,6 @@ static int neko_handler_rec( request_rec *r ) {
const char *ctype;
value exc = NULL;

/*
Seems to crash on Windows. And on Linux, we rarely have libGC 7.x installed anyway

# if defined(APACHE_2_X) || defined(NEKO_WINDOWS)
// we are using threads, so let's make sure that the current thread is registered
neko_thread_register(true);
# endif
*/

config.hits++;

ctx.r = r;
Expand Down Expand Up @@ -310,11 +301,14 @@ static int neko_handler( request_rec *r ) {
int ret;
if( strcmp(r->handler,"neko-handler") != 0)
return DECLINED;

neko_thread_register(true);
if( config.use_stats ) neko_stats_measure(NULL,r->hostname,1);
ret = neko_handler_rec(r);
neko_vm_select(NULL);
if( config.use_stats ) neko_stats_measure(NULL,r->hostname,0);
gc_major();
neko_thread_register(false);
return ret;
}

Expand Down
16 changes: 3 additions & 13 deletions vm/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,11 @@ static void null_warn_proc( char *msg, int arg ) {
}

void neko_gc_init() {
# ifndef NEKO_WINDOWS
// we can't set this on windows with old GC since
// it's already initialized through its own DllMain
GC_all_interior_pointers = 0;
# endif
#if (GC_VERSION_MAJOR >= 7) && defined(NEKO_WINDOWS)
GC_all_interior_pointers = 0;
# ifndef NEKO_STANDALONE
GC_use_DllMain(); // needed to auto-detect threads created by Apache
# endif
#endif
GC_java_finalization = 1;
GC_set_all_interior_pointers(0);
GC_set_java_finalization(1);
GC_init();
GC_set_warn_proc((GC_warn_proc)(void*)null_warn_proc);
GC_no_dls = 1;
GC_set_no_dls(1);
#ifdef LOW_MEM
GC_dont_expand = 1;
#endif
Expand Down
68 changes: 2 additions & 66 deletions vm/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifdef __APPLE__
// prevent later redefinition of bool
# include <dlfcn.h>
#endif
#include "vm.h"
#include <string.h>

Expand Down Expand Up @@ -69,10 +65,6 @@ struct _mt_lock {
pthread_mutex_t lock;
};

// should be enough to store any GC_stack_base
// implementation
typedef char __stack_base[64];

#endif

#endif // !NEKO_THREADS
Expand Down Expand Up @@ -166,52 +158,20 @@ EXTERN int neko_thread_create( thread_main_func init, thread_main_func main, voi
# endif
}

#if defined(NEKO_POSIX) && defined(NEKO_THREADS)
# include <dlfcn.h>
typedef void (*callb_func)( thread_main_func, void * );
typedef int (*std_func)();
typedef int (*gc_stack_ptr)( __stack_base * );

static int do_nothing( __stack_base *sb ) {
return -1;
}

#endif

EXTERN void neko_thread_blocking( thread_main_func f, void *p ) {
# if !defined(NEKO_THREADS)
f(p); // nothing
# elif defined(NEKO_WINDOWS)
f(p); // we don't have pthreads issues
# else
// we have different APIs depending on the GC version, make sure we load
// the good one at runtime
static callb_func do_blocking = NULL;
static std_func start = NULL, end = NULL;
if( do_blocking )
do_blocking(f,p);
else if( start ) {
start();
f(p);
end();
} else {
void *self = dlopen(NULL,0);
do_blocking = (callb_func)dlsym(self,"GC_do_blocking");
if( !do_blocking ) {
start = (std_func)dlsym(self,"GC_start_blocking");
end = (std_func)dlsym(self,"GC_end_blocking");
if( !start || !end )
val_throw(alloc_string("Could not init GC blocking API"));
}
neko_thread_blocking(f,p);
}
GC_do_blocking((GC_fn_type)f, p);
# endif
}

EXTERN bool neko_thread_register( bool t ) {
# if !defined(NEKO_THREADS)
return 0;
# elif defined(NEKO_WINDOWS)
# else
struct GC_stack_base sb;
int r;
if( !t )
Expand All @@ -220,30 +180,6 @@ EXTERN bool neko_thread_register( bool t ) {
return 0;
r = GC_register_my_thread(&sb);
return( r == GC_SUCCESS || r == GC_DUPLICATE );
# else
// since the API is only available on GC 7.0,
// we will do our best to locate it dynamically
static gc_stack_ptr get_sb = NULL, my_thread = NULL;
static std_func unreg_my_thread = NULL;
if( !t && unreg_my_thread != NULL ) {
return unreg_my_thread() == GC_SUCCESS;
} else if( my_thread != NULL ) {
__stack_base sb;
int r;
if( get_sb(&sb) != GC_SUCCESS )
return 0;
r = my_thread(&sb);
return( r == GC_SUCCESS || r == GC_DUPLICATE );
} else {
void *self = dlopen(NULL,0);
my_thread = (gc_stack_ptr)dlsym(self,"GC_register_my_thread");
get_sb = (gc_stack_ptr)dlsym(self,"GC_get_stack_base");
unreg_my_thread = (std_func)dlsym(self,"GC_unregister_my_thread");
if( my_thread == NULL ) my_thread = do_nothing;
if( get_sb == NULL ) get_sb = do_nothing;
if( unreg_my_thread == NULL ) unreg_my_thread = (std_func)do_nothing;
return neko_thread_register(t);
}
# endif
}

Expand Down
Loading