unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* threading issue in 1.8.3
@ 2008-01-11 14:11 Antoine Mathys
  2008-01-19  0:16 ` Neil Jerram
  0 siblings, 1 reply; 7+ messages in thread
From: Antoine Mathys @ 2008-01-11 14:11 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 2514 bytes --]

Hello,

I am having a problem with threads in 1.8.3 .

The offending program:

------------------
#include <pthread.h>
#include <libguile.h>

void * thread_inner_main (void * unused)
{
  int argc = 0;
  char* argv[] = { 0 };
  scm_shell (argc, argv);

  return NULL; /* dummy */
}

void * thread_main (void * unused)
{
  scm_with_guile (&thread_inner_main, NULL);

  return NULL; /* dummy */
}

void * inner_main (void * unused)
{
  pthread_t thread;

  pthread_create (&thread, NULL, &thread_main, NULL);
  pthread_join (thread, NULL);

  return NULL; /* dummy */
}

int main (int argc, char **argv)
{
  scm_with_guile (&inner_main, NULL);

  return 0;
}
------------------

This produces the following output:

------------------
Backtrace:
In unknown file:
   ?: 14* (if (or # #) (try-load-module name))
   ?: 15  [try-load-module (ice-9 readline)]
   ?: 16  (or (begin (try-module-linked name)) (try-module-autoload
   name) ...)
   ?: 17* [try-module-autoload (ice-9 readline)]
   ?: 18  (let* (# # # #) (resolve-module dir-hint-module-name {#f})
   (and # #))
   ?: 19* [resolve-module (ice-9) {#f}]
   ?: 20  (let* ((full-name #)) (let* (#) (if already # #)))
    ...
   ?: 21  (cond ((module-ref module # {#f}) => (lambda # #)) (else
   (let* # # # ...)))
   ?: 22* [module-ref {#f} %app {#f}]
   ?: 23  (let* ((variable #)) (if (and variable #) (variable-ref
   variable) ...))
   ?: 24* [module-variable {#f} %app]
   ?: 25  [module-search #<procedure module-local-variable (m v)> {#f}
   %app]
    ...
   ?: 26  (or (fn m v) (loop (module-uses m)))
   ?: 27* [module-local-variable {#f} %app]
   ?: 28  (let* ((b #)) (or (and # b) (and # #)))
   ?: 29* [module-obarray-ref ...
   ?: 30*  [module-obarray {#f}]
   ?: 31   (if (eq? # #) (struct-ref obj 0) (%record-type-error #
   obj))
   ?: 32*  [eq? ...
   ?: 33*   [struct-vtable {#f}]

<unnamed port>: In procedure struct-vtable in expression
(struct-vtable obj):
<unnamed port>: Wrong type argument in position 1 (expecting struct):
#f
------------------

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 ?

Thanks in advance.


P.S.
I used 'pthread_create' instead of 'scm_spawn_thread' for
simplification because for some reason 'join-thread' has no C
equivalent. But maybe I am missing something here as well ?


             
---------------------------------
 Ne gardez plus qu'une seule adresse mail ! Copiez vos mails vers Yahoo! Mail 

[-- Attachment #2: Type: text/html, Size: 3377 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: threading issue in 1.8.3
  2008-01-11 14:11 threading issue in 1.8.3 Antoine Mathys
@ 2008-01-19  0:16 ` Neil Jerram
  2008-01-30 23:41   ` Neil Jerram
  0 siblings, 1 reply; 7+ messages in thread
From: Neil Jerram @ 2008-01-19  0:16 UTC (permalink / raw)
  To: Antoine Mathys; +Cc: guile-user

Antoine Mathys <antoine_mathys@yahoo.fr> 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?

> Thanks in advance.
>
>
> P.S.
> I used 'pthread_create' instead of 'scm_spawn_thread' for
> simplification because for some reason 'join-thread' has no C
> equivalent. But maybe I am missing something here as well ?

scm_join_thread ?

Regards,
        Neil





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: threading issue in 1.8.3
  2008-01-19  0:16 ` Neil Jerram
@ 2008-01-30 23:41   ` Neil Jerram
  2008-02-01 22:54     ` Neil Jerram
  0 siblings, 1 reply; 7+ messages in thread
From: Neil Jerram @ 2008-01-30 23:41 UTC (permalink / raw)
  To: Antoine Mathys; +Cc: guile-user

[-- Attachment #1: Type: text/plain, Size: 2414 bytes --]

Neil Jerram <neil@ossau.uklinux.net> writes:

> Antoine Mathys <antoine_mathys@yahoo.fr> 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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: test-with-guile.patch --]
[-- Type: text/x-diff, Size: 3256 bytes --]

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 <pthread.h>
+#include <libguile.h>
+
+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;
+}

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: threading issue in 1.8.3
@ 2008-01-31  1:11 Antoine Mathys
  2008-02-01 21:01 ` Neil Jerram
  0 siblings, 1 reply; 7+ messages in thread
From: Antoine Mathys @ 2008-01-31  1:11 UTC (permalink / raw)
  To: neil; +Cc: guile-user


> scm_join_thread ?
>   
Yes. I missed it because it is not in the manual.





      _____________________________________________________________________________ 
Ne gardez plus qu'une seule adresse mail ! Copiez vos mails vers Yahoo! Mail http://mail.yahoo.fr




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: threading issue in 1.8.3
@ 2008-01-31  1:44 Antoine Mathys
  0 siblings, 0 replies; 7+ messages in thread
From: Antoine Mathys @ 2008-01-31  1:44 UTC (permalink / raw)
  To: neil; +Cc: guile-user


> My proposed patch for this is attached.
>   
Thanks, works great.





      _____________________________________________________________________________ 
Ne gardez plus qu'une seule adresse mail ! Copiez vos mails vers Yahoo! Mail http://mail.yahoo.fr




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: threading issue in 1.8.3
  2008-01-31  1:11 Antoine Mathys
@ 2008-02-01 21:01 ` Neil Jerram
  0 siblings, 0 replies; 7+ messages in thread
From: Neil Jerram @ 2008-02-01 21:01 UTC (permalink / raw)
  To: Antoine Mathys; +Cc: guile-user

Antoine Mathys <antoine_mathys@yahoo.fr> writes:

>> scm_join_thread ?
>>   
> Yes. I missed it because it is not in the manual.

That's fixed in CVS now, so will be in 1.8.4.

       Neil





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: threading issue in 1.8.3
  2008-01-30 23:41   ` Neil Jerram
@ 2008-02-01 22:54     ` Neil Jerram
  0 siblings, 0 replies; 7+ messages in thread
From: Neil Jerram @ 2008-02-01 22:54 UTC (permalink / raw)
  To: Antoine Mathys; +Cc: guile-user

Neil Jerram <neil@ossau.uklinux.net> writes:

> 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.

Thanks for testing; this patch is in CVS now.

     Neil





^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-02-01 22:54 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-01-11 14:11 threading issue in 1.8.3 Antoine Mathys
2008-01-19  0:16 ` Neil Jerram
2008-01-30 23:41   ` Neil Jerram
2008-02-01 22:54     ` Neil Jerram
  -- strict thread matches above, loose matches on Subject: below --
2008-01-31  1:11 Antoine Mathys
2008-02-01 21:01 ` Neil Jerram
2008-01-31  1:44 Antoine Mathys

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).