www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use core.atomic.cas with (function) pointers?

reply Johan Engelen <j j.nl> writes:
The following code compiles:
```
alias T = shared(int)*;

shared T a;
shared T b;
shared T c;

void foo() {
     import core.atomic: cas;
     cas(&a, b, c);
}
```

The type of T has to be a pointer to a shared int (you get a 
template match error for `cas` if `T = int*`), which is annoying 
but I kind-of understand.
However, change b to null (`cas(&a, null, c);`) and things no 
longer work: "Error: template `core.atomic.cas` cannot deduce 
function from argument types"

I have not succeeded to make things work with function pointers 
(neither with nor without `null` as second argument). What am I 
doing wrong if `alias T = void function();` ?

Thanks,
   Johan
Jan 22 2019
next sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Tuesday, 22 January 2019 at 14:13:23 UTC, Johan Engelen wrote:
 The following code compiles:
 ```
 alias T = shared(int)*;

 shared T a;
 shared T b;
 shared T c;

 void foo() {
     import core.atomic: cas;
     cas(&a, b, c);
 }
 ```

 The type of T has to be a pointer to a shared int (you get a 
 template match error for `cas` if `T = int*`), which is 
 annoying but I kind-of understand.
 However, change b to null (`cas(&a, null, c);`) and things no 
 longer work: "Error: template `core.atomic.cas` cannot deduce 
 function from argument types"

 I have not succeeded to make things work with function pointers 
 (neither with nor without `null` as second argument). What am I 
 doing wrong if `alias T = void function();` ?

 Thanks,
   Johan
You need to cast stuff (as seems to be the case with everything to do with shared, at least until/if Manu's proposal goes through): alias T = void function(); alias S = shared(size_t*); shared T a; shared T b; shared T c; void foo() { import core.atomic: cas; cas(cast(S*)&a, cast(S)b, cast(S)c); }
Jan 22 2019
prev sibling parent Kagamin <spam here.lot> writes:
I believe this is historical. It will fail because atomics are 
combinatorially parameterized because the compiler couldn't 
properly infer template arguments until recently (this also 
resulted in memory corruption in ldc fork of druntime). Now that 
the compiler was fixed, atomics can be fixed too and have single 
template parameter.
Jan 22 2019