From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Chris Vine Newsgroups: gmane.lisp.guile.user Subject: Re: libguile thread safety Date: Sat, 4 Jan 2014 09:39:31 +0000 Message-ID: <20140104093931.00cd420d@bother.homenet> References: <20140103233407.36382e5f@bother.homenet> <20140104002252.465aef65@bother.homenet> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1388828391 25079 80.91.229.3 (4 Jan 2014 09:39:51 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 4 Jan 2014 09:39:51 +0000 (UTC) Cc: "guile-user@gnu.org" To: Panicz Maciej Godek Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sat Jan 04 10:39:57 2014 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1VzNhw-0002aI-Uc for guile-user@m.gmane.org; Sat, 04 Jan 2014 10:39:53 +0100 Original-Received: from localhost ([::1]:53678 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VzNhw-0005e1-Jp for guile-user@m.gmane.org; Sat, 04 Jan 2014 04:39:52 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:56767) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VzNhh-0005an-UH for guile-user@gnu.org; Sat, 04 Jan 2014 04:39:45 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VzNha-0001wA-Io for guile-user@gnu.org; Sat, 04 Jan 2014 04:39:37 -0500 Original-Received: from smtpout2.wanadoo.co.uk ([80.12.242.42]:45128 helo=smtpout.wanadoo.co.uk) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VzNha-0001w4-7r for guile-user@gnu.org; Sat, 04 Jan 2014 04:39:30 -0500 Original-Received: from bother.homenet ([95.146.112.60]) by mwinf5d26 with ME id 9lfU1n0021JEVaP03lfURR; Sat, 04 Jan 2014 10:39:28 +0100 Original-Received: from bother.homenet (localhost [127.0.0.1]) by bother.homenet (Postfix) with ESMTP id 151AC88504; Sat, 4 Jan 2014 09:39:32 +0000 (GMT) In-Reply-To: X-Mailer: Claws Mail 3.9.2 (GTK+ 2.24.22; i686-pc-linux-gnu) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 80.12.242.42 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:10969 Archived-At: On Sat, 4 Jan 2014 02:22:11 +0100 Panicz Maciej Godek wrote: > I concluded that this might be a solution. I tested it with 2.0.5 from > ubuntu repository. I had to add GC_allow_register_threads (and link > against gc, accordingly), because otherwise the program aborted with > the message "Threads explicit registering is not previously enabled" > whenever it attempted to create a thread. > > For some reason the initialization routine needs to call > scm_c_eval_string; otherwise it doesn't help. > > The solution doesn't fill me with joy, but I hope it works for you Cool. Your revised test case appears to work on guile-2.0.9 without calling GC_allow_register_threads(). Subject to further testing the key seems to be to allow one thread to complete a call to scm_with_guile() and scm_c_eval_string() before any new one is allowed to do anything. On further testing (involving inserting my own synchronization and using scm_init_guile()) it appears that none of scm_with_guile(), scm_init_guile() and scm_c_eval_string() are thread safe on the first call. They seem to do some initial set up on first call which is not done in a thread safe way. By way of example, for me this also works (does it work for guile-2.0.5?): #include #include void *guile_wrapper (void *data) { scm_c_eval_string ("(display \"Hello\n\")"); return NULL; } void *thread_func (void *data) { scm_with_guile (&guile_wrapper, NULL); return NULL; } int main () { pthread_t thread1; pthread_t thread2; pthread_t thread3; /* initialize */ pthread_create (&thread1, NULL, thread_func, NULL); pthread_join (thread1, NULL); /* proceed with other threads */ pthread_create (&thread2, NULL, thread_func, NULL); pthread_create (&thread3, NULL, thread_func, NULL); pthread_join (thread2, NULL); pthread_join (thread3, NULL); return 0; } Unfortunately, from what Mark Weaver says, the module system doesn't initialize itself in a thread safe way either :(. That is more problematic because you might not know in advance what modules are to be loaded by any particular code. The task model isn't suppose to import cross-dependencies of that kind. Chris