comp.lang.ada
 help / color / mirror / Atom feed
* Should weak counted reference be able to be strengthened?
@ 2014-11-21 11:07 Natasha Kerensikova
  2014-11-21 13:16 ` Dmitry A. Kazakov
  0 siblings, 1 reply; 21+ messages in thread
From: Natasha Kerensikova @ 2014-11-21 11:07 UTC (permalink / raw)


Hello,

I hope I'm not too off-topic, but I have a question about designing weak
and strong references in a reference-counting system, implemented in
Ada.

In most of the general discussion about weak references, the title
question is a no-brainer: since the object referred by a weak reference
can disappear at any time, a strong reference must be obtained from the
weak reference before actually accessing the object.

However, in my (strong-only) current reference counting system, and in
most of those written in Ada, the referred object is not accessed
directly from the reference, instead they use an access discriminant in
a Mutator object (or an access constant discriminant in an Accessor),
and the object contains privately a strong reference, because in some
weird situations the reference might disappear before the mutator.

So my question is, now that I am considering extending my
reference-counting system to include weak reference, do I really needed
a public interface to build a strong reference from a weak one?
Or is it enough to have weak reference primitives that return Accessor
and Mutator objects, thereby allowing safe access without allowing a
weak reference to be promoted into a strong one?

In all the use cases I can imagine, the weak reference is only used to
access the referred object. I can't make up an example where a strong
reference would be needed from only a weak one. Am I missing something?

The interesting point I see in forbidding such a "reference
strengthening" is to prevent some cases circular strong references,
which is the problem weak references are supposed to solve.
Indeed, when an object has only a weak reference, it means it's
dangerous for it to hold a strong one, so it might also be dangerous for
it to hand one over to anything it can access.
The holder can still do bad things by storing an Accessor or a Mutator
object, but it is much more obviously a bad thing.

What are your thoughts on this?


As a side note, I'm a bit surprised by how easy weak/strong reference
seem to be: I think I would only need to store two counters instead of
one, one for the strong references and one for all references,
deallocating the object when the strong reference counter reaches
zero, and freeing the counters when the total counter reaches zero -- as
if each weak reference was a strong reference to the counters. Then it's
only a matter of adding an atomic increase-if-not-zero to the strong
reference counter to build accessors from weak references.
Is there a subtle trap I'm missing?


Thanks in advance for your help,
Natasha


^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2014-11-26 21:09 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-21 11:07 Should weak counted reference be able to be strengthened? Natasha Kerensikova
2014-11-21 13:16 ` Dmitry A. Kazakov
2014-11-21 15:00   ` Natasha Kerensikova
2014-11-21 16:50     ` Dmitry A. Kazakov
2014-11-21 18:24       ` Natasha Kerensikova
2014-11-21 19:36         ` Dmitry A. Kazakov
2014-11-21 22:28           ` Randy Brukardt
2014-11-22  9:57           ` Natasha Kerensikova
2014-11-22 10:19             ` Dmitry A. Kazakov
2014-11-22 10:36               ` Natasha Kerensikova
2014-11-22 11:15                 ` Dmitry A. Kazakov
2014-11-21 22:39       ` Randy Brukardt
2014-11-21 22:49         ` Shark8
2014-11-22  8:42           ` Dmitry A. Kazakov
2014-11-22  8:42         ` Dmitry A. Kazakov
2014-11-24 22:15           ` Randy Brukardt
2014-11-25  8:36             ` Dmitry A. Kazakov
2014-11-25 21:49               ` Randy Brukardt
2014-11-26  9:28                 ` Dmitry A. Kazakov
2014-11-26 17:51                   ` brbarkstrom
2014-11-26 21:09                   ` Randy Brukardt

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