Re: atomic and ref pointer

2018-04-04 Thread andrea
Great, this should go into the official docs!


Re: atomic and ref pointer

2018-04-04 Thread boia01
The way I understand `protect` and `dispose` is they are the inter-thread 
equivalent to `GC_ref` and `GC_unref` respectively.

Nim's per-thread GC only looks for roots in the current thread. If some objects 
can't be tracked using traced `ref`'s, i.e. using typed `ptr`'s or raw 
`pointer`'s, then you can use `GC_ref` and `GC_unref` to manually manage these 
untraced objects in the same thread. The typical use-case for this is when 
passing objects through FFI to C code.

When GC objects are passed across threads through `ptr` or `pointer`, then 
`protect` and `dispose` do the same thing, except they are thread-safe 
(GC_ref/unref can only be used in same thread). You obtain a `ForeignCell` 
handle from the thread that owns the object -- this ensures the object won't be 
inadvertently collected -- and then when you're done using it in the other 
thread(s), you call `dispose` on the `ForeignCell` to convey to the original GC 
thread that it can now collect the object if it's no longer in use. 


Re: atomic and ref pointer

2018-04-04 Thread andrea
Can we have any detail on how protect and dispose work? I see them mentioned 
from time to time, but their documentation is currently empty


Re: atomic and ref pointer

2018-04-04 Thread 2vg
> Well if you protect and dispose the ref properly,

"dispose"

Does it mean that the tracking of the GC disappears when casting ref to ptr ?

Should I use boehm gc ?


type
  Foo = object
head, tail: ptr Foo

var
  f = cast[ptr Foo](Foo.new())
  pf = cast[ptr Foo](Foo.new())

f.head = pf
f.tail = pf

# proc doWork() =
#   discard cas(addr(f.head), pf, cast[ptr Foo(Foo.new))

# dealloc(f.head) Is this need ?



Re: atomic and ref pointer

2018-04-03 Thread Araq
Well if you `protect` and `dispose` the `ref` properly, you can exchange it 
between threads. For this to work out it needs to be casted to `pointer` or 
`ptr`.


atomic and ref pointer

2018-04-01 Thread 2vg
Is there a correct way to handle ref object with atomic? Is it a bad way to 
cast to ptr?