[cpp-threads] Failed compare-and-swap

Alexander Terekhov alexander.terekhov at gmail.com
Tue Jul 31 20:41:07 BST 2007


On 7/31/07, Boehm, Hans <hans.boehm at hp.com> wrote:
[...]
> Trylock is a different situation, since a failed trylock by definition
> reads a value written by an operation (lock acquisition)without release
> semantics.  Thus it currently never establishes a synchronizes with
> relationship, and hence has no impact on visibility.  To meaningfully
> change that, lock acquisition would need release semantics, as it
> technically does in Posix.  (As Alexander points out, a simple trylock
> can't tell the difference in Posix.  On the other hand, a trylock
> followed by an unrelated successful synchronization operation can.)
> We've brought this up repeatedly, and I believe there is little support
> for release semantics for lock acquisition, not even from the Posix
> side.  (I believe a lot of Posix implementations don't follow this
> interpretation of the rules as it stands.)

That interpretation is based on the meaning of POSIX phrase
"synchronize memory" ("memory synchronization") being "both acquire
and release" so to speak. But POSIX doesn't define the term
"synchronize memory" ("memory synchronization") at all. More sound
interpretation is that "synchronize memory" means "release" or
"acquire" or "both acquire and release" depending on the operation
(more relaxed case for read locks on read-write locks aside for a
moment). I've filed a DR urging correction and it is still pending
(since 2004). The sketch was to replace XBD 4.10 with something along
the lines of

-----
Applications shall ensure that access to any memory location by more
than one thread is restricted such that no thread can read or modify
a memory location while another thread of control may be modifying it.
Applications shall use functions listed below that synchronize thread
execution and ensure that modifications to locations in memory are
ordered with respect to accesses to the same memory locations in other
threads. There is a happens-before inter-thread ordering with respect
to preceding (in program and inter-thread order) modifications of
memory locations in

  a thread calling pthreads_create() and accesses to modified memory
  locations in the created thread;

  a thread calling fork() and accesses to modified memory locations
  in the initial thread of the created process;

  a terminated thread and accesses to modified memory locations in
  another thread in the same process after that thread returns from
  pthread_join() joining the terminated thread, or in another thread
  in a parent process after that thread returns from wait() or
  waitpid() observing terminated status of a child process;

  a thread calling sem_post() on a specified semaphore and accesses
  to modified memory locations in another thread after that thread
  calls sem_wait() or sem_trywait() or sem_timedwait() and
  locks the same semaphore unlocked by the calling thread, or
  another thread observes the incremented semaphore value of the
  same semaphore by calling sem_getvalue();

  a thread calling sem_wait() or sem_trywait() or sem_timedwait()
  decrementing semaphore value of a specified semaphore and accesses
  to modified memory locations in another thread after that thread
  observes the decremented semaphore value of the same semaphore
  using sem_getvalue();

  < XSI IPC semas... >

  < realtime signals? sigvalue is a union with pointer... >

  a thread calling pthread_spin_unlock() on a specified spin lock
  and accesses to modified memory locations in another thread
  after that thread calls pthread_spin_lock() or
  pthread_spin_trylock() and locks the same spin lock unlocked by
  the calling thread;

  a thread calling pthread_rwlock_unlock() on a specified read-
  write lock releasing a write lock and read accesses to modified
  memory locations in another thread after that thread acquires a
  read lock using pthread_rwlock_rdlock() or
  pthread_rwlock_timedrdlock() or pthread_rwlock_tryrdlock() on
  the same read-write lock unlocked by the calling thread;

  a thread calling pthread_rwlock_unlock() on a specified read-
  write lock releasing a write lock and read/write accesses to
  modified memory locations in another thread after that thread
  acquires a write lock using pthread_rwlock_timedwrlock() or
  pthread_rwlock_trywrlock() or pthread_rwlock_wrlock() on
  the same read-write lock unlocked by the calling thread;

  a thread calling pthread_mutex_unlock() on a specified mutex
  and accesses to modified memory locations in another thread
  after that thread calls pthread_mutex_lock() or
  pthread_mutex_timedlock() or pthread_mutex_trylock() and locks
  the same mutex unlocked by the calling thread or when another
  thread locks the same mutex unlocked by the calling thread
  and returns from a call to pthread_cond_wait() or
  pthread_cond_timedwait();

  a thread calling pthread_cond_wait() or pthread_cond_timedwait()
  and accesses to modified memory locations in another thread
  after that thread calls pthread_mutex_lock() or
  pthread_mutex_timedlock() or pthread_mutex_trylock() and locks
  the same mutex unlocked by the calling thread or when another
  thread locks the same mutex unlocked by the calling thread
  and returns from a call to pthread_cond_wait() or
  pthread_cond_timedwait();

  a thread calling pthread_barrier_wait() on a specified barrier
  and accesses to modified memory locations in another thread
  after that thread returns from pthread_barrier_wait() call on
  the same specified barrier on the same or later barrier cycle;

  a thread calling pthread_once() and on a specified pthread_once_t
  object returning from a specified init_routine and accesses to
  modified memory locations in another thread after that thread
  returns from pthread_once() on the same pthread_once_t object.

Functions listed above and also pthread_cond_signal() and
pthread_cond_broadcast() are all side effects.

Conforming applications are said to be data race-free.
-----

regards,
alexander.



More information about the cpp-threads mailing list