unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Ken Raeburn <raeburn@raeburn.org>
Subject: Re: Some introductory docs about C level threading
Date: Tue, 1 Feb 2005 12:37:36 -0500	[thread overview]
Message-ID: <6b496d191d7216e82df4e9a7afbb3168@raeburn.org> (raw)
In-Reply-To: <lj651qy6sw.fsf@troy.dt.e-technik.uni-dortmund.de>

On Jan 21, 2005, at 13:29, Marius Vollmer wrote:
> 4.3.5 Multi-Threading
> ---------------------
>      SCM
>      my_list_to_vector (SCM list)
>      {
>        SCM vector = scm_make_vector (scm_length (list), SCM_UNDEFINED);
>        size_t len, i;
>
>        len = SCM_SIMPLE_VECTOR_LENGTH (vector);
>        i = 0;
>        while (i < len && scm_is_pair (list))
>          {
>            SCM_SIMPLE_VECTOR_SET (vector, i, SCM_CAR (list));
>            list = SCM_CDR (list);
>            i++;
>          }
>
>        return vector;
>      }
>
>    It is safe to use `SCM_CAR' and `SCM_CDR' on the local variable LIST
> once it is known that the variable contains a pair.  The contents of
> the pair might change spontaneously, but it will always stay a valid
> pair (and a local variable will of course not spontaneously point to a
> different Scheme object).

But is it guaranteed that, without locking, SCM_CAR and SCM_CDR will 
always return the "before" or "after" values while another thread is 
modifying them, and not some corrupted intermediate form?  Given 
typical data alignment and access, and hardware memory management 
techniques, and the small size of a SCM value, I'm not aware of a 
situation where it would be at all likely to happen, but I'm also not 
aware of any guarantee that it won't.

Though, even without weird memory management issues, consider:
   SCM_SETCAR(x, SCM_I_MAKINUM(i >> 2))    i some external int var
  -> (....x)[0] = ((... i >> 2) << 2 + scm_tc2_int)
  -> copy i, masking off bottom two bits, into target location; then add 
in scm_tc2_int

If SCM_CAR(x) is read at just the wrong time, the tag bits might not be 
correct.

It may also be the case that if both the car and cdr of the pair (or 
indeed, any two separate locations) are modified in one thread and read 
in another, the memory accesses could be reordered (by the compiler, or 
even by the CPU these days) such that the read-back contents of the 
pair don't seem to match the "before" or "after" values or even the 
expected intermediate state where the first-listed modification has 
been done but the second has not.

If someone's got lots more experience with multithreaded systems and 
wants to say I'm wrong, great.  I've been thinking about this a bit 
lately, trying to make a pile of code thread-safe at work, but I'm 
mostly coming at it from a theoretical perspective.

Ken



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


  parent reply	other threads:[~2005-02-01 17:37 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-21 18:29 Some introductory docs about C level threading Marius Vollmer
2005-01-21 21:20 ` Neil Jerram
2005-01-24 19:23   ` Marius Vollmer
2005-02-15  4:11     ` Robert Uhl
2005-01-22 12:37 ` tomas
2005-01-24 19:28   ` Marius Vollmer
2005-01-25  9:06     ` tomas
2005-01-25 15:58       ` Rob Browning
2005-01-25 16:01         ` Rob Browning
2005-01-26  9:05         ` tomas
2005-02-01 16:01           ` Marius Vollmer
2005-02-02  9:31             ` tomas
2005-02-02 13:18               ` Marius Vollmer
2005-02-02 13:47                 ` tomas
2005-01-23 23:34 ` Kevin Ryde
2005-01-24 19:31 ` Some new reference docs about initialization Marius Vollmer
2005-01-24 20:27   ` Andreas Rottmann
2005-01-24 22:00     ` Marius Vollmer
2005-02-01 17:37 ` Ken Raeburn [this message]
2005-02-01 23:40   ` Some introductory docs about C level threading Kevin Ryde
2005-02-02  0:38     ` Ken Raeburn
2005-02-07  0:48       ` Kevin Ryde
2005-02-08 21:45         ` Ken Raeburn
2005-02-09 12:17         ` Marius Vollmer
2005-02-09 20:26           ` Kevin Ryde
2005-02-10 11:36             ` Marius Vollmer
2005-02-09 12:13       ` Marius Vollmer
2005-02-09 18:10         ` Ken Raeburn
2005-02-10 12:05           ` Marius Vollmer
2005-02-10 19:59             ` Ken Raeburn
2005-02-09 12:08     ` Marius Vollmer
2005-02-09 16:33       ` Doug Evans
2005-02-10 11:15         ` Marius Vollmer
2005-02-09 20:19       ` Kevin Ryde
2005-02-09 12:04   ` Marius Vollmer

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=6b496d191d7216e82df4e9a7afbb3168@raeburn.org \
    --to=raeburn@raeburn.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).