Locking data

Malte no at valid.mail
Wed May 23 18:27:10 UTC 2018


On Wednesday, 23 May 2018 at 13:36:20 UTC, rikki cattermole wrote:
> On 24/05/2018 1:29 AM, Malte wrote:
>> On Wednesday, 23 May 2018 at 13:24:35 UTC, rikki cattermole 
>> wrote:
>>> On 24/05/2018 1:20 AM, Malte wrote:
>>>> On Tuesday, 22 May 2018 at 21:45:07 UTC, 
>>>> IntegratedDimensions wrote:
>>>>> an idea to lock data by removing the reference:
>>>>>
>>>>> class A
>>>>> {
>>>>>    Lockable!Data data;
>>>>> }
>>>>>
>>>>> [...]
>>>>
>>>> This sounds like you are looking for is an atomic swap. 
>>>> Afaik it doesn't exist in the standard library. You could 
>>>> use asm for the XCHG, but that would make your code x86 
>>>> dependent.
>>>> I think the easiest way would be to just use a mutex and 
>>>> tryLock.
>>>
>>> What are you talking about? :p
>>>
>>> http://dpldocs.info/experimental-docs/core.atomic.cas.1.html
>> 
>> That is Compare-and-set.
>> To make an exchange using cas I first have to read the value, 
>> then write to it expecting to be still the value I read 
>> before. That are more instructions than just a swap. If a cas 
>> fails, I have to redo everything. An exchange never fails, I 
>> just might not get the result I would like to have (null 
>> instead of pointer).
>
> So you want a load + store as swap in a single function (that 
> is optimized).
> In that case, please create an issue on bugzilla 
> (issues.dlang.org).

No, as I said, that is already one instruction on X86: 
https://www.felixcloutier.com/x86/XCHG.html
Just being able to use that instruction with the standard library 
would be good.

You could also use it with compiler intrinsics. Something like
>import ldc.intrinsics;
>T* tryGetPtr(T)(T** a) {
>    return 
> cast(T*)llvm_atomic_rmw_xchg!size_t(cast(shared(size_t)*)a, 0);
>}
>void restorePtr(T)(T** a, T* b) {
>    
> llvm_atomic_rmw_xchg!size_t(cast(shared(size_t)*)a,cast(size_t)b);
>}

I would just go with mutexes unless your really need to go that 
low level though, much saner.


More information about the Digitalmars-d-learn mailing list