Lock free structures in Pharo

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Lock free structures in Pharo

Stephan Eggermont-3
Reading this code, made me wonder what operations are actually atomic.
Anyone having a good explanation?

Stephan

AtomicQueueItem>makeCircular
        "Make a receiver circular, i.e. point to itself,
        answer the old value of next variable.
        Note, this operation should be atomic"
       
        | temp |

        " atomic swap here"
        temp := next.
        next := self.

        ^ temp

Reply | Threaded
Open this post in threaded view
|

Re: Lock free structures in Pharo

Marcus Denker-4

On 14 Nov 2013, at 10:15, Stephan Eggermont <[hidden email]> wrote:

> Reading this code, made me wonder what operations are actually atomic.
> Anyone having a good explanation?
>
> Stephan
>
> AtomicQueueItem>makeCircular
> "Make a receiver circular, i.e. point to itself,
> answer the old value of next variable.
> Note, this operation should be atomic"
>
> | temp |
>
> " atomic swap here"
> temp := next.
> next := self.
>
> ^ temp
>
-> no message send
-> no back jump bytecode

therefore it can not be interrupted and process switches can not happen between the statements.

        Marcus
Reply | Threaded
Open this post in threaded view
|

Re: Lock free structures in Pharo

NorbertHartl

Am 14.11.2013 um 10:25 schrieb Marcus Denker <[hidden email]>:

>
> On 14 Nov 2013, at 10:15, Stephan Eggermont <[hidden email]> wrote:
>
>> Reading this code, made me wonder what operations are actually atomic.
>> Anyone having a good explanation?
>>
>> Stephan
>>
>> AtomicQueueItem>makeCircular
>> "Make a receiver circular, i.e. point to itself,
>> answer the old value of next variable.
>> Note, this operation should be atomic"
>>
>> | temp |
>>
>> " atomic swap here"
>> temp := next.
>> next := self.
>>
>> ^ temp
>>
> -> no message send
> -> no back jump bytecode
>
> therefore it can not be interrupted and process switches can not happen between the statements.

Thanks, I learn something new every day. I’ve never thought about the condition when a process switch can happen. Can you say in short when a switch of processes is checked? Because I think I won’t finding it in a reasonable time looking myself.

Norbert


Reply | Threaded
Open this post in threaded view
|

Re: Lock free structures in Pharo

jtuchel
Hi Norbert,

I couldn't have said it any better. For me, this also was interesting,
because I first thought: "Heck, how would I make anything atomic anyways?".
And that question still stands: is there a way to make something atomic
that includes message sends? I guess #critical is not the solution, is it?

Joachim

Am 14.11.13 11:12, schrieb Norbert Hartl:

> Am 14.11.2013 um 10:25 schrieb Marcus Denker <[hidden email]>:
>
>> On 14 Nov 2013, at 10:15, Stephan Eggermont <[hidden email]> wrote:
>>
>>> Reading this code, made me wonder what operations are actually atomic.
>>> Anyone having a good explanation?
>>>
>>> Stephan
>>>
>>> AtomicQueueItem>makeCircular
>>> "Make a receiver circular, i.e. point to itself,
>>> answer the old value of next variable.
>>> Note, this operation should be atomic"
>>>
>>> | temp |
>>>
>>> " atomic swap here"
>>> temp := next.
>>> next := self.
>>>
>>> ^ temp
>>>
>> -> no message send
>> -> no back jump bytecode
>>
>> therefore it can not be interrupted and process switches can not happen between the statements.
> Thanks, I learn something new every day. I’ve never thought about the condition when a process switch can happen. Can you say in short when a switch of processes is checked? Because I think I won’t finding it in a reasonable time looking myself.
>
> Norbert
>
>
>


--
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          mailto:[hidden email]
Fliederweg 1                         http://www.objektfabrik.de
D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0         Fax: +49 7141 56 10 86 1


Reply | Threaded
Open this post in threaded view
|

Re: Lock free structures in Pharo

NorbertHartl

Am 14.11.2013 um 11:16 schrieb [hidden email]:

> Hi Norbert,
>
> I couldn't have said it any better. For me, this also was interesting, because I first thought: "Heck, how would I make anything atomic anyways?".
> And that question still stands: is there a way to make something atomic that includes message sends? I guess #critical is not the solution, is it?
>
No, because then it isn’t lock free anymore ;)

Norbert
>

> Am 14.11.13 11:12, schrieb Norbert Hartl:
>> Am 14.11.2013 um 10:25 schrieb Marcus Denker <[hidden email]>:
>>
>>> On 14 Nov 2013, at 10:15, Stephan Eggermont <[hidden email]> wrote:
>>>
>>>> Reading this code, made me wonder what operations are actually atomic.
>>>> Anyone having a good explanation?
>>>>
>>>> Stephan
>>>>
>>>> AtomicQueueItem>makeCircular
>>>> "Make a receiver circular, i.e. point to itself,
>>>> answer the old value of next variable.
>>>> Note, this operation should be atomic"
>>>>
>>>> | temp |
>>>>
>>>> " atomic swap here"
>>>> temp := next.
>>>> next := self.
>>>>
>>>> ^ temp
>>>>
>>> -> no message send
>>> -> no back jump bytecode
>>>
>>> therefore it can not be interrupted and process switches can not happen between the statements.
>> Thanks, I learn something new every day. I’ve never thought about the condition when a process switch can happen. Can you say in short when a switch of processes is checked? Because I think I won’t finding it in a reasonable time looking myself.
>>
>> Norbert
>>
>>
>>
>
>
> --
> -----------------------------------------------------------------------
> Objektfabrik Joachim Tuchel          mailto:[hidden email]
> Fliederweg 1                         http://www.objektfabrik.de
> D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
> Telefon: +49 7141 56 10 86 0         Fax: +49 7141 56 10 86 1
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Lock free structures in Pharo

jtuchel
Am 14.11.13 11:19, schrieb Norbert Hartl:

> Am 14.11.2013 um 11:16 schrieb [hidden email]:
>
>> Hi Norbert,
>>
>> I couldn't have said it any better. For me, this also was interesting, because I first thought: "Heck, how would I make anything atomic anyways?".
>> And that question still stands: is there a way to make something atomic that includes message sends? I guess #critical is not the solution, is it?
>>
> No, because then it isn’t lock free anymore ;)
>
> Norbert
I knew I was typing something stupid ;-)
But you are of course right. #critical's whole concept is locking, of
course ;-)
>>
>>


--
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          mailto:[hidden email]
Fliederweg 1                         http://www.objektfabrik.de
D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0         Fax: +49 7141 56 10 86 1


Reply | Threaded
Open this post in threaded view
|

Re: Lock free structures in Pharo

Stéphane Ducasse
In reply to this post by NorbertHartl
Note that it would be good to have a special syntactic construct for that because now
we rely on the way the compiler works to ensure such properties and it means that
an accessor and a direct access are not semantically equals.


Stef


>> On 14 Nov 2013, at 10:15, Stephan Eggermont <[hidden email]> wrote:
>>
>>> Reading this code, made me wonder what operations are actually atomic.
>>> Anyone having a good explanation?
>>>
>>> Stephan
>>>
>>> AtomicQueueItem>makeCircular
>>> "Make a receiver circular, i.e. point to itself,
>>> answer the old value of next variable.
>>> Note, this operation should be atomic"
>>>
>>> | temp |
>>>
>>> " atomic swap here"
>>> temp := next.
>>> next := self.
>>>
>>> ^ temp
>>>
>> -> no message send
>> -> no back jump bytecode
>>
>> therefore it can not be interrupted and process switches can not happen between the statements.
>
> Thanks, I learn something new every day. I’ve never thought about the condition when a process switch can happen. Can you say in short when a switch of processes is checked? Because I think I won’t finding it in a reasonable time looking myself.
>
> Norbert
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Lock free structures in Pharo

NorbertHartl
Stef,

Am 14.11.2013 um 12:10 schrieb Stéphane Ducasse <[hidden email]>:

> Note that it would be good to have a special syntactic construct for that because now
> we rely on the way the compiler works to ensure such properties and it means that
> an accessor and a direct access are not semantically equals.
>
I was asking for pointers/locations where to look at. I like to wrap my head around it in order to understand how it works and thus figuring out where and when there is a problem.

Norbert

>
> Stef
>
>
>>> On 14 Nov 2013, at 10:15, Stephan Eggermont <[hidden email]> wrote:
>>>
>>>> Reading this code, made me wonder what operations are actually atomic.
>>>> Anyone having a good explanation?
>>>>
>>>> Stephan
>>>>
>>>> AtomicQueueItem>makeCircular
>>>> "Make a receiver circular, i.e. point to itself,
>>>> answer the old value of next variable.
>>>> Note, this operation should be atomic"
>>>>
>>>> | temp |
>>>>
>>>> " atomic swap here"
>>>> temp := next.
>>>> next := self.
>>>>
>>>> ^ temp
>>>>
>>> -> no message send
>>> -> no back jump bytecode
>>>
>>> therefore it can not be interrupted and process switches can not happen between the statements.
>>
>> Thanks, I learn something new every day. I’ve never thought about the condition when a process switch can happen. Can you say in short when a switch of processes is checked? Because I think I won’t finding it in a reasonable time looking myself.
>>
>> Norbert
>>
>>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Lock free structures in Pharo

Stéphane Ducasse
You should look in VM code clement / eliot can reply more precisely
but the idea is to see when do you accept to suspend a computation.
In Smalltlak this is after each message.

Stef

> Stef,
>
> Am 14.11.2013 um 12:10 schrieb Stéphane Ducasse <[hidden email]>:
>
>> Note that it would be good to have a special syntactic construct for that because now
>> we rely on the way the compiler works to ensure such properties and it means that
>> an accessor and a direct access are not semantically equals.
>>
> I was asking for pointers/locations where to look at. I like to wrap my head around it in order to understand how it works and thus figuring out where and when there is a problem.
>
> Norbert
>>
>> Stef
>>
>>
>>>> On 14 Nov 2013, at 10:15, Stephan Eggermont <[hidden email]> wrote:
>>>>
>>>>> Reading this code, made me wonder what operations are actually atomic.
>>>>> Anyone having a good explanation?
>>>>>
>>>>> Stephan
>>>>>
>>>>> AtomicQueueItem>makeCircular
>>>>> "Make a receiver circular, i.e. point to itself,
>>>>> answer the old value of next variable.
>>>>> Note, this operation should be atomic"
>>>>>
>>>>> | temp |
>>>>>
>>>>> " atomic swap here"
>>>>> temp := next.
>>>>> next := self.
>>>>>
>>>>> ^ temp
>>>>>
>>>> -> no message send
>>>> -> no back jump bytecode
>>>>
>>>> therefore it can not be interrupted and process switches can not happen between the statements.
>>>
>>> Thanks, I learn something new every day. I’ve never thought about the condition when a process switch can happen. Can you say in short when a switch of processes is checked? Because I think I won’t finding it in a reasonable time looking myself.
>>>
>>> Norbert
>>>
>>>
>>
>>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Lock free structures in Pharo

philippeback
In reply to this post by jtuchel

Clement also told me that the VM only checked for Cmd-. breaks on back jumps.

Phil


On Thu, Nov 14, 2013 at 11:16 AM, [hidden email] <[hidden email]> wrote:
Hi Norbert,

I couldn't have said it any better. For me, this also was interesting, because I first thought: "Heck, how would I make anything atomic anyways?".
And that question still stands: is there a way to make something atomic that includes message sends? I guess #critical is not the solution, is it?

Joachim

Am 14.11.13 11:12, schrieb Norbert Hartl:
Am 14.11.2013 um 10:25 schrieb Marcus Denker <[hidden email]>:

On 14 Nov 2013, at 10:15, Stephan Eggermont <[hidden email]> wrote:

Reading this code, made me wonder what operations are actually atomic.
Anyone having a good explanation?

Stephan

AtomicQueueItem>makeCircular
        "Make a receiver circular, i.e. point to itself,
        answer the old value of next variable.
        Note, this operation should be atomic"
       
        | temp |

        " atomic swap here"
        temp := next.
        next := self.

        ^ temp

-> no message send
-> no back jump bytecode

therefore it can not be interrupted and process switches can not happen between the statements.
Thanks, I learn something new every day. I’ve never thought about the condition when a process switch can happen. Can you say in short when a switch of processes is checked? Because I think I won’t finding it in a reasonable time looking myself.

Norbert





--
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          mailto:[hidden email]
Fliederweg 1                         http://www.objektfabrik.de
D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
Telefon: <a href="tel:%2B49%207141%2056%2010%2086%200" value="+4971415610860" target="_blank">+49 7141 56 10 86 0         Fax: <a href="tel:%2B49%207141%2056%2010%2086%201" value="+4971415610861" target="_blank">+49 7141 56 10 86 1




Reply | Threaded
Open this post in threaded view
|

Re: Lock free structures in Pharo

camille teruel
In reply to this post by Stéphane Ducasse

On 14 nov. 2013, at 12:38, Stéphane Ducasse <[hidden email]> wrote:

> You should look in VM code clement / eliot can reply more precisely
> but the idea is to see when do you accept to suspend a computation.
> In Smalltlak this is after each message.

Except for #== and inlined message like #ifTrue:ifFalse: , #to:do: , etc...
See http://forum.world.st/About-and-td3898409.html for more details

> Stef
>
>> Stef,
>>
>> Am 14.11.2013 um 12:10 schrieb Stéphane Ducasse <[hidden email]>:
>>
>>> Note that it would be good to have a special syntactic construct for that because now
>>> we rely on the way the compiler works to ensure such properties and it means that
>>> an accessor and a direct access are not semantically equals.
>>>
>> I was asking for pointers/locations where to look at. I like to wrap my head around it in order to understand how it works and thus figuring out where and when there is a problem.
>>
>> Norbert
>>>
>>> Stef
>>>
>>>
>>>>> On 14 Nov 2013, at 10:15, Stephan Eggermont <[hidden email]> wrote:
>>>>>
>>>>>> Reading this code, made me wonder what operations are actually atomic.
>>>>>> Anyone having a good explanation?
>>>>>>
>>>>>> Stephan
>>>>>>
>>>>>> AtomicQueueItem>makeCircular
>>>>>> "Make a receiver circular, i.e. point to itself,
>>>>>> answer the old value of next variable.
>>>>>> Note, this operation should be atomic"
>>>>>>
>>>>>> | temp |
>>>>>>
>>>>>> " atomic swap here"
>>>>>> temp := next.
>>>>>> next := self.
>>>>>>
>>>>>> ^ temp
>>>>>>
>>>>> -> no message send
>>>>> -> no back jump bytecode
>>>>>
>>>>> therefore it can not be interrupted and process switches can not happen between the statements.
>>>>
>>>> Thanks, I learn something new every day. I’ve never thought about the condition when a process switch can happen. Can you say in short when a switch of processes is checked? Because I think I won’t finding it in a reasonable time looking myself.
>>>>
>>>> Norbert
>>>>
>>>>
>>>
>>>
>>
>>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Lock free structures in Pharo

Igor Stasenko
In reply to this post by Stephan Eggermont-3



On 14 November 2013 10:15, Stephan Eggermont <[hidden email]> wrote:
Reading this code, made me wonder what operations are actually atomic.
Anyone having a good explanation?

Stephan

AtomicQueueItem>makeCircular
        "Make a receiver circular, i.e. point to itself,
        answer the old value of next variable.
        Note, this operation should be atomic"

        | temp |

        " atomic swap here"
        temp := next.
        next := self.

        ^ temp

imagine that i rewrite it with following:

temp := self.
temp <=> next.
^ temp

here the <=> is a special 'atomic swap' operation which atomically swaps values
of two variables.
The best way to do that is to introduce a special bytecode in VM,
which either swaps values of two temps or swaps values of temp and instance variable.
If we could have such bytecode, then we would have a strong guarantee from VM
about atomicity of certain operations,
but since we don't have it yet, right now it is just (ab)uses the intrinsic behavior of VM,
knowing that it never interrupts between two simple assignments.

--
Best regards,
Igor Stasenko.