From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Neil Jerram Newsgroups: gmane.lisp.guile.user Subject: Re: threading issue in 1.8.3 Date: Wed, 30 Jan 2008 23:41:48 +0000 Message-ID: <87bq72ubib.fsf@ossau.uklinux.net> References: <497155.28911.qm@web27613.mail.ukl.yahoo.com> <87d4ry1xgl.fsf@ossau.uklinux.net> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1201736543 6624 80.91.229.12 (30 Jan 2008 23:42:23 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 30 Jan 2008 23:42:23 +0000 (UTC) Cc: guile-user@gnu.org To: Antoine Mathys Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Thu Jan 31 00:42:43 2008 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1JKMZZ-0005tW-0w for guile-user@m.gmane.org; Thu, 31 Jan 2008 00:42:29 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JKMZ7-0000Qc-N5 for guile-user@m.gmane.org; Wed, 30 Jan 2008 18:42:01 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1JKMZ2-0000Mt-CX for guile-user@gnu.org; Wed, 30 Jan 2008 18:41:56 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1JKMZ0-0000Iu-Oe for guile-user@gnu.org; Wed, 30 Jan 2008 18:41:55 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JKMZ0-0000IT-Bh for guile-user@gnu.org; Wed, 30 Jan 2008 18:41:54 -0500 Original-Received: from mail3.uklinux.net ([80.84.72.33]) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1JKMYz-0001aY-QY for guile-user@gnu.org; Wed, 30 Jan 2008 18:41:54 -0500 Original-Received: from arudy (host86-145-183-175.range86-145.btcentralplus.com [86.145.183.175]) by mail3.uklinux.net (Postfix) with ESMTP id 354ED1F6829; Wed, 30 Jan 2008 23:41:50 +0000 (GMT) Original-Received: from laruns (laruns [192.168.0.10]) by arudy (Postfix) with ESMTP id AACD13800A; Wed, 30 Jan 2008 23:41:48 +0000 (GMT) In-Reply-To: <87d4ry1xgl.fsf@ossau.uklinux.net> (Neil Jerram's message of "Sat, 19 Jan 2008 00:16:26 +0000") User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) X-detected-kernel: by monty-python.gnu.org: Linux 2.4-2.6 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:6394 Archived-At: --=-=-= Neil Jerram writes: > Antoine Mathys writes: > >> Hello, >> >> I am having a problem with threads in 1.8.3 . > >> It seems that for some reason you cannot load modules from a different thread >> than the one which first initialized guile. >> >> Any idea how to get this to work ? > > Apologies for the delay in responding... > > I think this is caused by the second thread not knowing what its > current module is. Can you try adding, before the scm_shell() call: > > scm_set_current_module (scm_lookup_closure_module (SCM_BOOL_F)); > > and report if that helps? This problem was a bit more complex than it looked. scm_shell () with no command line args expands to (begin (turn-on-debugging) (load-user-init) (top-repl)) And (top-repl) calls (set-current-module guile-user-module) very early on, which means that any `use-modules' calls after that will be fine. Therefore, the reported call stack (which indicates a problem in a `use-modules' call, because of (current-module) being #f) can only be caused by a `use-modules' call during the (load-user-init) - i.e. when loading ~/.guile. (To confirm this: my ~/.guile had a `use-syntax' call in it (which is mostly equivalent to `use-modules'), and I got the same stack as you; when I commented that out, I didn't see the reported stack any more.) Anyway, code in ~/.guile should expect (current-module) to be sane, so the necessary fix is still to make sure that (current-module) always returns a module (so long as the module system has booted). My proposed patch for this is attached. Two other notes for the record. 1. In my tests, the test program hung after the second thread had begun (top-repl), before trying to read any input from stdin. This was because the second thread decided to GC, and so tried to put the first thread to sleep. Put the first thread was blocked inside pthread_join (). The fix for this is for the test program to do the pthread_join () inside a scm_without_guile () call, as in the attached patch. 2. If the second thread had been started in Scheme, it would have inherited the first thread's value of (current-module), which was already non-#f (as a result of the (define-module ...) call at the end of boot-9.sc,). So this problem only occurs with multiple threads started in C using scm_with_guile (). Regards, Neil --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=test-with-guile.patch Index: libguile/modules.c =================================================================== RCS file: /cvsroot/guile/guile/guile-core/libguile/modules.c,v retrieving revision 1.62.2.1 diff -u -r1.62.2.1 modules.c --- libguile/modules.c 12 Feb 2006 13:42:51 -0000 1.62.2.1 +++ libguile/modules.c 30 Jan 2008 23:39:10 -0000 @@ -40,12 +40,25 @@ static SCM the_module; +static SCM the_root_module_var; + +static SCM +the_root_module () +{ + if (scm_module_system_booted_p) + return SCM_VARIABLE_REF (the_root_module_var); + else + return SCM_BOOL_F; +} + SCM_DEFINE (scm_current_module, "current-module", 0, 0, 0, (), "Return the current module.") #define FUNC_NAME s_scm_current_module { - return scm_fluid_ref (the_module); + SCM curr = scm_fluid_ref (the_module); + + return scm_is_true (curr) ? curr : the_root_module (); } #undef FUNC_NAME @@ -234,17 +247,6 @@ SCM_SYMBOL (sym_module, "module"); -static SCM the_root_module_var; - -static SCM -the_root_module () -{ - if (scm_module_system_booted_p) - return SCM_VARIABLE_REF (the_root_module_var); - else - return SCM_BOOL_F; -} - SCM scm_lookup_closure_module (SCM proc) { Index: test-suite/standalone/Makefile.am =================================================================== RCS file: /cvsroot/guile/guile/guile-core/test-suite/standalone/Makefile.am,v retrieving revision 1.13.2.6 diff -u -r1.13.2.6 Makefile.am --- test-suite/standalone/Makefile.am 29 Dec 2007 01:34:18 -0000 1.13.2.6 +++ test-suite/standalone/Makefile.am 30 Jan 2008 23:39:10 -0000 @@ -110,6 +110,12 @@ check_SCRIPTS += test-use-srfi TESTS += test-use-srfi +# test-with-guile-module +test_with_guile_module_CFLAGS = ${test_cflags} +test_with_guile_module_LDADD = ${top_builddir}/libguile/libguile.la +check_PROGRAMS += test-with-guile-module +TESTS += test-with-guile-module + all-local: cd ${srcdir} && chmod u+x ${check_SCRIPTS} Index: test-suite/standalone/test-with-guile-module.c =================================================================== RCS file: test-suite/standalone/test-with-guile-module.c diff -N test-suite/standalone/test-with-guile-module.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test-suite/standalone/test-with-guile-module.c 30 Jan 2008 23:39:10 -0000 @@ -0,0 +1,52 @@ +#include +#include + +void * thread_inner_main (void * unused); +void * thread_main (void * unused); +void * do_join (void * data); +void * inner_main (void * unused); + +void * thread_inner_main (void * unused) +{ + int argc = 3; + char* argv[] = { "guile", + "-c", + "(or (current-module) (exit -1))", + 0 }; + scm_shell (argc, argv); + + return NULL; /* dummy */ +} + +void * thread_main (void * unused) +{ + scm_with_guile (&thread_inner_main, NULL); + + return NULL; /* dummy */ +} + +void * do_join (void * data) +{ + pthread_t *thread = (pthread_t *)data; + + pthread_join (*thread, NULL); + + return NULL; /* dummy */ +} + +void * inner_main (void * unused) +{ + pthread_t thread; + + pthread_create (&thread, NULL, &thread_main, NULL); + scm_without_guile (do_join, &thread); + + return NULL; /* dummy */ +} + +int main (int argc, char **argv) +{ + scm_with_guile (&inner_main, NULL); + + return 0; +} --=-=-=--