E&S CVS Commit: Contextual - Implemented EigenValue-style write-until-read immutability for Config

pje at eby-sarna.com pje at eby-sarna.com
Sun Feb 25 21:16:06 EST 2007


Author: pje
Date: Sun Feb 25 21:16:05 2007
New Revision: 2293

URL: http://svn.eby-sarna.com?rev=2293&view=rev
Log:
Implemented EigenValue-style write-until-read immutability for Config
objects.  This new algorithm is actually thread-safe without needing
any locks, by using the atomic properties of dictionaries, and by
detecting conflicts *after* a write attempt is made.  In the event of
a race between two or more threads, it's nondeterministic who will
"win", but there will be exactly one winner and all other writers
will fail.

Proof (informal):

  writes are copied into buffer[key]

  snapshot[key] is never modified except via setdefault -- therefore
  it can only ever have *one* value

  when snapshot[key] is set, its value comes from buffer[key]; thus,
  snapshot[key] is always *some* (indeterminately chosen) setting
  that was set by writing to the mapping, or by computing a default
  value.

  once a write is completed, snapshot.get(key, value_written) is
  used to verify that snapshot[key] is either *empty* OR a value
  that is the same as or equal to the value written.

  If the value is different, some other writer's value was read
  first, and our write is in conflict.  If the value is the same,
  then our write was "successful", in that there is a *chance* it
  will be the end value of the setting.

In other words, a succesful write does *not* necessarily mean that
the written value is or will be "the" value.  It is still possible
to have non-deterministic behavior if multiple threads are
simultaneously writing to the same Config, but it will not corrupt
the Config or violate the "only one value is ever seen by readers"
invariant.  If precedence of an individual setting value matters, 
you must serialze the writes, but unserialized writes will not
cause inconsistent behavior.  This is a sufficient guarantee to
allow e.g. configuration file loaders to be written.

Modified:
    Contextual/Contextual.txt
    Contextual/context_tests.txt
    Contextual/peak/context.py




More information about the source-changes mailing list