unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Thread and guile environment
@ 2010-07-05  8:23 rixed
  2010-07-05 19:52 ` Neil Jerram
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: rixed @ 2010-07-05  8:23 UTC (permalink / raw)
  To: guile-user

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

Suppose I have a multithreaded C program. Isn't the guile environment supposed
to be shared amongst all threads ? That's what I understood from reading the
docs anyway.

Yet this simple exemple shows the opposite (see the 3 attached files).
So am I supposed to source my global scheme definitions in all threads ?



[-- Attachment #2: bug.c --]
[-- Type: text/plain, Size: 604 bytes --]

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

static void *load_file_with_guile(void *filename)
{
	scm_c_primitive_load((char *)filename);
	return NULL;
}

static void *hug_me(void *str)
{
	scm_c_eval_string(str);
}

static void *thread_hugme(void *dummy)
{
	//scm_with_guile(load_file_with_guile, "bug.scm");
	scm_with_guile(hug_me, "(hug 2)");
	return NULL;
}

int main(void) {
	scm_with_guile(load_file_with_guile, "bug.scm");

	scm_with_guile(hug_me, "(hug 1)");

	pthread_t thread;
    pthread_create(&thread, NULL, thread_hugme, NULL);

	pthread_join(thread, NULL);

	return 0;
}

[-- Attachment #3: bug.scm --]
[-- Type: text/plain, Size: 73 bytes --]

(use-modules (ice-9 format))
(define (hug x) (format #t "HUG ~a !\n" x))

[-- Attachment #4: Makefile --]
[-- Type: text/plain, Size: 97 bytes --]

CFLAGS += $(shell guile-config compile)
LDLIBS += $(shell guile-config link) -lpthread

all: bug

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

* Re: Thread and guile environment
  2010-07-05  8:23 Thread and guile environment rixed
@ 2010-07-05 19:52 ` Neil Jerram
  2010-07-06 11:22   ` Cedric Cellier
  2010-07-08 19:48 ` Andy Wingo
  2010-07-12  8:09 ` Cedric Cellier
  2 siblings, 1 reply; 12+ messages in thread
From: Neil Jerram @ 2010-07-05 19:52 UTC (permalink / raw)
  To: rixed; +Cc: guile-user

rixed@happyleptic.org writes:

> Suppose I have a multithreaded C program. Isn't the guile environment supposed
> to be shared amongst all threads ? That's what I understood from reading the
> docs anyway.
>
> Yet this simple exemple shows the opposite (see the 3 attached files).
> So am I supposed to source my global scheme definitions in all threads ?

Hm.  I think I recall a bug to do with different threads starting in
different modules.  Does it work if you add

(define-module (guile-user))

at the start of your bug.scm ?

     Neil



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

* Re: Thread and guile environment
  2010-07-05 19:52 ` Neil Jerram
@ 2010-07-06 11:22   ` Cedric Cellier
  2010-07-07 11:02     ` Cedric Cellier
  0 siblings, 1 reply; 12+ messages in thread
From: Cedric Cellier @ 2010-07-06 11:22 UTC (permalink / raw)
  To: guile-user

-[ Mon, Jul 05, 2010 at 08:52:44PM +0100, Neil Jerram ]----
> rixed@happyleptic.org writes:
> Hm.  I think I recall a bug to do with different threads starting in
> different modules.  Does it work if you add
>
> (define-module (guile-user))
>
> at the start of your bug.scm ?

Same behavior :

ERROR: Unbound variable: hug

Nobody's doing something similar ?





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

* Re: Thread and guile environment
  2010-07-06 11:22   ` Cedric Cellier
@ 2010-07-07 11:02     ` Cedric Cellier
  0 siblings, 0 replies; 12+ messages in thread
From: Cedric Cellier @ 2010-07-07 11:02 UTC (permalink / raw)
  To: guile-user

By the way, I'm using guile v1.8.7(+1-3ubuntu1)




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

* Re: Thread and guile environment
  2010-07-05  8:23 Thread and guile environment rixed
  2010-07-05 19:52 ` Neil Jerram
@ 2010-07-08 19:48 ` Andy Wingo
  2010-07-09 15:54   ` Cedric Cellier
  2010-07-09 20:23   ` Neil Jerram
  2010-07-12  8:09 ` Cedric Cellier
  2 siblings, 2 replies; 12+ messages in thread
From: Andy Wingo @ 2010-07-08 19:48 UTC (permalink / raw)
  To: rixed; +Cc: guile-user

On Mon 05 Jul 2010 09:23, rixed@happyleptic.org writes:

> Suppose I have a multithreaded C program. Isn't the guile environment supposed
> to be shared amongst all threads ? That's what I understood from reading the
> docs anyway.
>
> Yet this simple exemple shows the opposite (see the 3 attached files).
> So am I supposed to source my global scheme definitions in all threads
> ?

Interestingly, the first thread has you in (guile-user), but the second
has you in (guile). So you don't see the full definition of format, nor
do you see hug.

A workaround is to do:

	scm_with_guile(hug_me, "(begin (set-current-module (resolve-module '(guile-user))) (hug 2))");

instead of

	scm_with_guile(hug_me, "(hug 2)");

But it's quite ugly. Does anyone have any input as to what module should
be current when a thread previously unknown to Guile enters Guile?

Andy
-- 
http://wingolog.org/



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

* Re: Thread and guile environment
  2010-07-08 19:48 ` Andy Wingo
@ 2010-07-09 15:54   ` Cedric Cellier
  2010-07-09 19:10     ` Andy Wingo
  2010-07-09 20:23   ` Neil Jerram
  1 sibling, 1 reply; 12+ messages in thread
From: Cedric Cellier @ 2010-07-09 15:54 UTC (permalink / raw)
  To: guile-user

-[ Thu, Jul 08, 2010 at 08:48:33PM +0100, Andy Wingo ]----
> Interestingly, the first thread has you in (guile-user), but the second
> has you in (guile). So you don't see the full definition of format, nor
> do you see hug.

Interresting indeed.
Is there a definition of these modules and their purpose somewhere ?

> 	scm_with_guile(hug_me, "(begin (set-current-module (resolve-module '(guile-user))) (hug 2))");

Ok for me.
I think I will have to play with modules anyway (see my other thread).

Thank you for your help !




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

* Re: Thread and guile environment
  2010-07-09 15:54   ` Cedric Cellier
@ 2010-07-09 19:10     ` Andy Wingo
  0 siblings, 0 replies; 12+ messages in thread
From: Andy Wingo @ 2010-07-09 19:10 UTC (permalink / raw)
  To: guile-user

On Fri 09 Jul 2010 17:54, Cedric Cellier <rixed@happyleptic.org> writes:

> -[ Thu, Jul 08, 2010 at 08:48:33PM +0100, Andy Wingo ]----
>> Interestingly, the first thread has you in (guile-user), but the second
>> has you in (guile). So you don't see the full definition of format, nor
>> do you see hug.
>
> Interresting indeed.
> Is there a definition of these modules and their purpose somewhere ?

Mmm, some of this is not as documented as it should be. There is
"Modules" in the manual, and there is the source of boot-9.scm (fairly
well commented). If you have more specific questions, please ask the list.

Andy
-- 
http://wingolog.org/



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

* Re: Thread and guile environment
  2010-07-08 19:48 ` Andy Wingo
  2010-07-09 15:54   ` Cedric Cellier
@ 2010-07-09 20:23   ` Neil Jerram
  2010-07-10  8:22     ` Andy Wingo
  1 sibling, 1 reply; 12+ messages in thread
From: Neil Jerram @ 2010-07-09 20:23 UTC (permalink / raw)
  To: Andy Wingo; +Cc: guile-user

Andy Wingo <wingo@pobox.com> writes:

> On Mon 05 Jul 2010 09:23, rixed@happyleptic.org writes:
>
>> Suppose I have a multithreaded C program. Isn't the guile environment supposed
>> to be shared amongst all threads ? That's what I understood from reading the
>> docs anyway.
>>
>> Yet this simple exemple shows the opposite (see the 3 attached files).
>> So am I supposed to source my global scheme definitions in all threads
>> ?
>
> Interestingly, the first thread has you in (guile-user), but the second
> has you in (guile). So you don't see the full definition of format, nor
> do you see hug.

That's what I thought too.  But in that case I have no idea why my
'(define-module (guile-user))' suggestion didn't work.

> But it's quite ugly. Does anyone have any input as to what module should
> be current when a thread previously unknown to Guile enters Guile?

Surely it has to be (guile-user), since that what the end of boot-9.scm
moves into - and hence is well-established for the single thread case.
I suspect that a program that calls scm_with_guile() on multiple threads
can't necessarily predict which of the threads will perform the Guile
startup.

     Neil



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

* Re: Thread and guile environment
  2010-07-09 20:23   ` Neil Jerram
@ 2010-07-10  8:22     ` Andy Wingo
  2010-07-12 20:24       ` Linas Vepstas
  0 siblings, 1 reply; 12+ messages in thread
From: Andy Wingo @ 2010-07-10  8:22 UTC (permalink / raw)
  To: Neil Jerram; +Cc: guile-user

Hi,

On Fri 09 Jul 2010 22:23, Neil Jerram <neil@ossau.uklinux.net> writes:

> Andy Wingo <wingo@pobox.com> writes:
>
>> Interestingly, the first thread has you in (guile-user), but the second
>> has you in (guile). So you don't see the full definition of format, nor
>> do you see hug.
>
> That's what I thought too.  But in that case I have no idea why my
> '(define-module (guile-user))' suggestion didn't work.

I think bug.scm was loaded in the main module -- the threads just tried
to call a function defined by bug.scm, and as they were in (guile), not
(guile-user), there was the error.

>> But it's quite ugly. Does anyone have any input as to what module should
>> be current when a thread previously unknown to Guile enters Guile?
>
> Surely it has to be (guile-user), since that what the end of boot-9.scm
> moves into - and hence is well-established for the single thread case.

Agreed. I just pushed the following patch, which fixes Cedric's case.


commit a85f90f5ac5c3c5f830e295c0ca7b006141b1a83
Author: Andy Wingo <wingo@pobox.com>
Date:   Sat Jul 10 10:21:22 2010 +0200

    capture default dynamic state in (guile-user)
    
    * libguile/init.c (scm_i_init_guile): Move the call to
      scm_init_threads_default_dynamic_state after the call to
      scm_load_startup_files, so that the default dynamic state is in the
      (guile-user) module, not (guile).

diff --git a/libguile/init.c b/libguile/init.c
index 6313b65..4843910 100644
--- a/libguile/init.c
+++ b/libguile/init.c
@@ -570,8 +570,6 @@ scm_i_init_guile (SCM_STACKITEM *base)
   scm_i_init_deprecated ();
 #endif
 
-  scm_init_threads_default_dynamic_state ();
-
   scm_initialized_p = 1;
 
 #ifdef STACK_CHECKING
@@ -585,6 +583,10 @@ scm_i_init_guile (SCM_STACKITEM *base)
   atexit (cleanup_for_exit);
   scm_load_startup_files ();
   scm_init_load_should_autocompile ();
+
+  /* Capture the dynamic state after loading boot-9, so that new threads end up
+     in the guile-user module. */
+  scm_init_threads_default_dynamic_state ();
 }
 
 /*


Andy
-- 
http://wingolog.org/



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

* Re: Thread and guile environment
  2010-07-05  8:23 Thread and guile environment rixed
  2010-07-05 19:52 ` Neil Jerram
  2010-07-08 19:48 ` Andy Wingo
@ 2010-07-12  8:09 ` Cedric Cellier
  2 siblings, 0 replies; 12+ messages in thread
From: Cedric Cellier @ 2010-07-12  8:09 UTC (permalink / raw)
  To: guile-user

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

This works if I explicitely define a module _and_ export the hug symbol.


[-- Attachment #2: bug.scm --]
[-- Type: text/plain, Size: 109 bytes --]

(define-module (bug))
(export hug)

(use-modules (ice-9 format))
(define (hug x) (format #t "HUG ~a !\n" x))

[-- Attachment #3: bug.c --]
[-- Type: text/x-csrc, Size: 624 bytes --]

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

static void *load_file_with_guile(void *filename)
{
	scm_c_primitive_load((char *)filename);
	return NULL;
}

static void *hug_me(void *str)
{
	scm_c_eval_string(str);
}

static void *thread_hugme(void *dummy)
{
	//scm_with_guile(load_file_with_guile, "bug.scm");
	scm_with_guile(hug_me, "((@ (bug) hug) 2)");
	return NULL;
}

int main(void) {
	scm_with_guile(load_file_with_guile, "bug.scm");

	scm_with_guile(hug_me, "((@ (bug) hug) 1)");

	pthread_t thread;
    pthread_create(&thread, NULL, thread_hugme, NULL);

	pthread_join(thread, NULL);

	return 0;
}

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

* Re: Thread and guile environment
  2010-07-10  8:22     ` Andy Wingo
@ 2010-07-12 20:24       ` Linas Vepstas
  2010-07-17 12:24         ` Andy Wingo
  0 siblings, 1 reply; 12+ messages in thread
From: Linas Vepstas @ 2010-07-12 20:24 UTC (permalink / raw)
  To: Andy Wingo; +Cc: guile-user, Neil Jerram

Hi,

On 10 July 2010 03:22, Andy Wingo <wingo@pobox.com> wrote:
> Hi,
>
> On Fri 09 Jul 2010 22:23, Neil Jerram <neil@ossau.uklinux.net> writes:
>
>> Andy Wingo <wingo@pobox.com> writes:
>>
>>> Interestingly, the first thread has you in (guile-user), but the second
>>> has you in (guile). So you don't see the full definition of format, nor
>>> do you see hug.
>>
>> That's what I thought too.  But in that case I have no idea why my
>> '(define-module (guile-user))' suggestion didn't work.
>
> I think bug.scm was loaded in the main module -- the threads just tried
> to call a function defined by bug.scm, and as they were in (guile), not
> (guile-user), there was the error

Yes, this sounds a lot like the bug I saw few years ago ...
.
>>> But it's quite ugly. Does anyone have any input as to what module should
>>> be current when a thread previously unknown to Guile enters Guile?
>>
>> Surely it has to be (guile-user), since that what the end of boot-9.scm
>> moves into - and hence is well-established for the single thread case.
>
> Agreed. I just pushed the following patch, which fixes Cedric's case.

Thanks.

Is this for guile1.8.8 or for 2.0?

--linas



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

* Re: Thread and guile environment
  2010-07-12 20:24       ` Linas Vepstas
@ 2010-07-17 12:24         ` Andy Wingo
  0 siblings, 0 replies; 12+ messages in thread
From: Andy Wingo @ 2010-07-17 12:24 UTC (permalink / raw)
  To: linasvepstas; +Cc: guile-user, Neil Jerram

Hi,

On Mon 12 Jul 2010 22:24, Linas Vepstas <linasvepstas@gmail.com> writes:
> On 10 July 2010 03:22, Andy Wingo <wingo@pobox.com> wrote:
>> On Fri 09 Jul 2010 22:23, Neil Jerram <neil@ossau.uklinux.net> writes:
>>> Andy Wingo <wingo@pobox.com> writes:
>>>
>>>> Does anyone have any input as to what module should be current when
>>>> a thread previously unknown to Guile enters Guile?
>>>
>>> Surely it has to be (guile-user), since that what the end of boot-9.scm
>>> moves into - and hence is well-established for the single thread case.
>>
>> Agreed. I just pushed the following patch, which fixes Cedric's case.
>
> Thanks.
>
> Is this for guile1.8.8 or for 2.0?

2.0.

Andy
-- 
http://wingolog.org/



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

end of thread, other threads:[~2010-07-17 12:24 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-07-05  8:23 Thread and guile environment rixed
2010-07-05 19:52 ` Neil Jerram
2010-07-06 11:22   ` Cedric Cellier
2010-07-07 11:02     ` Cedric Cellier
2010-07-08 19:48 ` Andy Wingo
2010-07-09 15:54   ` Cedric Cellier
2010-07-09 19:10     ` Andy Wingo
2010-07-09 20:23   ` Neil Jerram
2010-07-10  8:22     ` Andy Wingo
2010-07-12 20:24       ` Linas Vepstas
2010-07-17 12:24         ` Andy Wingo
2010-07-12  8:09 ` Cedric Cellier

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