unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Han-Wen Nienhuys <hanwen@xs4all.nl>
To: guile-devel@gnu.org
Subject: Re: Race condition in threading code?
Date: Sun, 31 Aug 2008 21:18:23 -0300	[thread overview]
Message-ID: <g9fcev$ods$1@ger.gmane.org> (raw)
In-Reply-To: <871w05x7zu.fsf@gnu.org>

Ludovic Courtès escreveu:
> Hello,
> 
> Andy Wingo <wingo@pobox.com> writes:
> 
>> ERROR: srfi-18.test: thread-start!:
>>   thread activates only after start
>>    - arguments: ((syntax-error "memoization"
>>                   "In file ~S, line ~S: ~A ~S in expression ~S."
>>                   ("/home/lilydev/vc/guile/srfi/srfi-18.scm" 135
>>                    "Bad binding" ct
>>                     (let (ct (current-thread))
>>                     #@let (or (hashq-ref thread-exception-handlers ct)
>>                           (hashq-set! thread-exception-handlers ct
>>                     (list initial-handler))))) #f))
> 
> I'm seeing this as well, but it's a `#@let*' here (single-binding `let's
> are memoized as `#@let*'):
> 
>   ((syntax-error "memoization"
>                  "In file ~S, line ~S: ~A ~S in expression ~S."
>                  ("/home/ludo/src/guile/srfi/srfi-18.scm" 138
>                   "Bad binding"
>                   ct
>                   (#@let* (ct (#<variable b7d28110 value: #<primitive-procedure current-thread>>))
>                     (#@or (#<variable b7d2ad88 value: #<primitive-procedure hashq-ref>>
>                                       #<variable 839df08 value: #<weak-key-hash-table 1/31>> #@0-0)
>                           (#<variable b7d2adc0 value: #<primitive-procedure hashq-set!>> #<variable 839df08 value: #<weak-key-hash-table 1/31>> #@0-0 (#<variable b7d2c498 value: #<primitive-procedure list>> #<variable 839d130 value: #<procedure initial-handler (obj)>>))
>                           )))
>                  #f))
> 
> It can be reproduced, but very infrequently, with this program:
> 
>   (use-modules (ice-9 threads))
> 
>   (define (foo x y)
>     (let ((z (+ x y)))
>       (let ((a (+ z 1)))
>         (let ((b (- a 2)))
>           (let ((c (* b 3)))
>             c)))))
> 
>   (define (entry)
>     (foo 1 2))
> 
>   (for-each (lambda (i) (make-thread entry))
>             (iota 123))
> 
> My explanation is that the `let*' memoizer, aka. `scm_m_letstar ()', is
> not thread-safe; it's clearly not atomic, and it's of course not
> protected by a mutex or so.

Is that the only one?

  SCM
  scm_m_let (SCM expr, SCM env)
  ...
      /* plain let */
      SCM rvariables;
      SCM inits;
      transform_bindings (bindings, expr, &rvariables, &inits);

      {
        const SCM new_body = m_body (SCM_IM_LET, SCM_CDR (cdr_expr));
        const SCM new_tail = scm_cons2 (rvariables, inits, new_body);
        SCM_SETCAR (expr, SCM_IM_LET); 
        // ****!!!
        SCM_SETCDR (expr, new_tail);

What happens if another thread tries to evaluate expr at the place marked ****!!! ?

At the very least, we should have an atomic SCM_SETCELL() which overwrites car and
cdr atomically.

-- 
 Han-Wen Nienhuys - hanwen@xs4all.nl - http://www.xs4all.nl/~hanwen





  parent reply	other threads:[~2008-09-01  0:18 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-16 18:21 Race condition in threading code? Han-Wen Nienhuys
2008-08-16 18:42 ` Julian Graham
2008-08-16 18:45   ` Han-Wen Nienhuys
2008-08-26 20:23     ` Andy Wingo
2008-08-27  0:41       ` Han-Wen Nienhuys
2008-08-27  2:36       ` Han-Wen Nienhuys
2008-08-27  7:46       ` Ludovic Courtès
2008-08-27 13:14         ` Julian Graham
2008-08-30 23:05           ` Julian Graham
2008-08-31  1:49             ` Han-Wen Nienhuys
2008-08-31  2:54               ` Julian Graham
2008-08-31 13:52                 ` Han-Wen Nienhuys
2008-08-31 10:34               ` Ludovic Courtès
2008-08-31 12:58             ` Ludovic Courtès
2008-08-31 15:05               ` Julian Graham
2008-08-31 19:39                 ` Ludovic Courtès
2008-09-07  0:12                   ` Julian Graham
2008-09-08 12:37                     ` Ludovic Courtès
2008-08-31 20:08       ` Ludovic Courtès
2008-08-31 23:59         ` Han-Wen Nienhuys
2008-09-01  8:11           ` Ludovic Courtès
2008-09-01  0:18         ` Han-Wen Nienhuys [this message]
2008-09-03  4:56           ` Han-Wen Nienhuys
2008-09-04 18:12             ` Andy Wingo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='g9fcev$ods$1@ger.gmane.org' \
    --to=hanwen@xs4all.nl \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).