comp.lang.ada
 help / color / mirror / Atom feed
From: Hadrien Grasland <hadrien.grasland@gmail.com>
Subject: Re: Lock-Free stack in Ada SPARK
Date: Sun, 12 Feb 2017 04:26:11 -0800 (PST)
Date: 2017-02-12T04:26:11-08:00	[thread overview]
Message-ID: <077a224f-4836-4c05-89f1-3f6fbee61427@googlegroups.com> (raw)
In-Reply-To: <o7np0i$iu$1@dont-email.me>

Le samedi 11 février 2017 20:36:31 UTC+1, Jeffrey R. Carter a écrit :
> Perhaps someone will enlighten me.
> 
> All of the lock-free structures that I've looked at involved busy waiting on a 
> memory location via an atomic test-and-set/compare-and-swap operation.
> 
> To my mind, that's an implementation of a lock, and I don't understand why these 
> are considered lock free.

A lock-free object is an object that can be concurrently manipulated by multiple threads, in such a fashion that any thread can fall asleep permanently at any point in time without preventing other threads from doing useful work.

A spinlock is not a lock-free object because if the thread holding the lock falls asleep, any other thread that needs to manipulate the spinlock is unable to make progress.

If you want an example of a lock-free object, consider Linux's Read-Copy-Update (RCU) mechanism, which is used to implement concurrent data structures that are read frequently but mutated rarely:

- The RCU data structure is accessed indirectly through a pointer.
- To access the data, a thread performs an atomic read on the pointer, then uses that pointer to read the data in a non-atomic fashion.
- To mutate the data, a thread does the following:
    * Access the data structure as above
    * Make a copy of it, and adjust the copy as needed
    * Try to replace the pointer to the old structure with the pointer to the new structure using an atomic compare-and-swap operation
    * If that fails, someone replaced the data structure under our feet: load the new data structure and try to perform our modifications again.

This algorithm is lock-free because threads that read data don't step on each other, and any thread which attempts to mutate the data can fall asleep at any point in time before the final compare-and-swap without preventing other threads from making progress.


  parent reply	other threads:[~2017-02-12 12:26 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-11  3:07 Lock-Free stack in Ada SPARK stevenselectronicmail
2017-02-11  9:57 ` Hadrien Grasland
2017-02-11 10:07   ` Hadrien Grasland
2017-02-11 10:08     ` Hadrien Grasland
2017-02-11 10:14       ` Hadrien Grasland
2017-02-11 17:34         ` stevenselectronicmail
2017-02-11 19:36           ` Jeffrey R. Carter
2017-02-11 20:42             ` stevenselectronicmail
2017-02-11 22:40             ` Dmitry A. Kazakov
2017-02-11 23:02               ` stevenselectronicmail
2017-02-12  8:30                 ` Dmitry A. Kazakov
2017-02-12 12:26             ` Hadrien Grasland [this message]
2017-02-12 12:49               ` Dmitry A. Kazakov
2017-02-12 13:19                 ` Hadrien Grasland
2017-02-12 14:57                   ` Dmitry A. Kazakov
2017-02-12 13:04               ` Hadrien Grasland
2017-02-12 22:25 ` Robert Eachus
2017-02-13  8:22   ` Dmitry A. Kazakov
2017-02-13 20:04   ` stevenselectronicmail
2017-02-21  1:51     ` Robert Eachus
2017-02-21  8:44       ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox