[libgee] Move to WeakRef to avoid data races



commit 0bcebb3473fafc0e9c30d7bd3962b336fb11f4da
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date:   Sat Jan 25 21:16:40 2014 +0100

    Move to WeakRef to avoid data races

 gee/abstractbidirlist.vala      |   12 +++++++-----
 gee/abstractbidirsortedmap.vala |   12 +++++++-----
 gee/abstractbidirsortedset.vala |   12 +++++++-----
 gee/abstractcollection.vala     |   12 +++++++-----
 gee/abstractlist.vala           |   12 +++++++-----
 gee/abstractmap.vala            |   12 +++++++-----
 gee/abstractmultimap.vala       |   13 ++++++++-----
 gee/abstractmultiset.vala       |   13 ++++++++-----
 gee/abstractset.vala            |   12 +++++++-----
 gee/abstractsortedmap.vala      |   13 ++++++++-----
 gee/abstractsortedset.vala      |   12 +++++++-----
 11 files changed, 80 insertions(+), 55 deletions(-)
---
diff --git a/gee/abstractbidirlist.vala b/gee/abstractbidirlist.vala
index 1f50a0f..4787d0a 100644
--- a/gee/abstractbidirlist.vala
+++ b/gee/abstractbidirlist.vala
@@ -26,18 +26,20 @@ public abstract class Gee.AbstractBidirList<G> : AbstractList<G>, BidirList<G> {
         */
        public abstract BidirListIterator<G> bidir_list_iterator ();
 
-       private weak BidirList<G> _read_only_view;
+       private WeakRef _read_only_view;
+       construct {
+               _read_only_view = WeakRef(null);
+       }
 
        /**
         * { inheritDoc}
         */
        public virtual new BidirList<G> read_only_view {
                owned get {
-                       BidirList<G> instance = _read_only_view;
-                       if (_read_only_view == null) {
+                       BidirList<G>? instance = (BidirList<G>?)_read_only_view.get();
+                       if (instance == null) {
                                instance = new ReadOnlyBidirList<G> (this);
-                               _read_only_view = instance;
-                               instance.add_weak_pointer ((void**) (&_read_only_view));
+                               _read_only_view.set (instance);
                        }
                        return instance;
                }
diff --git a/gee/abstractbidirsortedmap.vala b/gee/abstractbidirsortedmap.vala
index bab090a..c3f5a70 100644
--- a/gee/abstractbidirsortedmap.vala
+++ b/gee/abstractbidirsortedmap.vala
@@ -33,18 +33,20 @@ public abstract class Gee.AbstractBidirSortedMap<K,V> : Gee.AbstractSortedMap<K,
         */
        public abstract BidirMapIterator<K,V> bidir_map_iterator ();
 
-       private weak BidirSortedMap<K,V> _read_only_view;
+       private WeakRef _read_only_view;
+       construct {
+               _read_only_view = WeakRef(null);
+       }
 
        /**
         * { inheritDoc}
         */
        public virtual new BidirSortedMap<K,V> read_only_view {
                owned get {
-                       BidirSortedMap<K,V> instance = _read_only_view;
-                       if (_read_only_view == null) {
+                       BidirSortedMap<K,V>? instance = (BidirSortedMap<K,V>?)_read_only_view.get ();
+                       if (instance == null) {
                                instance = new ReadOnlyBidirSortedMap<K,V> (this);
-                               _read_only_view = instance;
-                               instance.add_weak_pointer ((void**) (&_read_only_view));
+                               _read_only_view.set (instance);
                        }
                        return instance;
                }
diff --git a/gee/abstractbidirsortedset.vala b/gee/abstractbidirsortedset.vala
index 92ca920..4702c6b 100644
--- a/gee/abstractbidirsortedset.vala
+++ b/gee/abstractbidirsortedset.vala
@@ -33,18 +33,20 @@ public abstract class Gee.AbstractBidirSortedSet<G> : Gee.AbstractSortedSet<G>,
         */
        public abstract BidirIterator<G> bidir_iterator ();
 
-       private weak BidirSortedSet<G> _read_only_view;
+       private WeakRef _read_only_view;
+       construct {
+               _read_only_view = WeakRef(null);
+       }
 
        /**
         * { inheritDoc}
         */
        public virtual new BidirSortedSet<G> read_only_view {
                owned get {
-                       BidirSortedSet<G> instance = _read_only_view;
-                       if (_read_only_view == null) {
+                       BidirSortedSet<G>? instance = (BidirSortedSet<G>?)_read_only_view.get ();
+                       if (instance == null) {
                                instance = new ReadOnlyBidirSortedSet<G> (this);
-                               _read_only_view = instance;
-                               instance.add_weak_pointer ((void**) (&_read_only_view));
+                               _read_only_view.set (instance);
                        }
                        return instance;
                }
diff --git a/gee/abstractcollection.vala b/gee/abstractcollection.vala
index f7aa9b2..85e24f5 100644
--- a/gee/abstractcollection.vala
+++ b/gee/abstractcollection.vala
@@ -70,18 +70,20 @@ public abstract class Gee.AbstractCollection<G> : Object, Traversable<G>, Iterab
                return iterator ().foreach (f);
        }
 
-       private weak Collection<G> _read_only_view;
+       private WeakRef _read_only_view;
+       construct {
+               _read_only_view = WeakRef(null);
+       }
 
        /**
         * { inheritDoc}
         */
        public virtual Collection<G> read_only_view {
                owned get {
-                       Collection<G> instance = _read_only_view;
-                       if (_read_only_view == null) {
+                       Collection<G>? instance = (Collection<G>?)_read_only_view.get ();
+                       if (instance == null) {
                                instance = new ReadOnlyCollection<G> (this);
-                               _read_only_view = instance;
-                               instance.add_weak_pointer ((void**) (&_read_only_view));
+                               _read_only_view.set (instance);
                        }
                        return instance;
                }
diff --git a/gee/abstractlist.vala b/gee/abstractlist.vala
index 03650f6..eeff984 100644
--- a/gee/abstractlist.vala
+++ b/gee/abstractlist.vala
@@ -66,18 +66,20 @@ public abstract class Gee.AbstractList<G> : Gee.AbstractCollection<G>, List<G> {
         */
        public abstract List<G>? slice (int start, int stop);
 
-       private weak List<G> _read_only_view;
+       private WeakRef _read_only_view;
+       construct {
+               _read_only_view = WeakRef(null);
+       }
 
        /**
         * { inheritDoc}
         */
        public virtual new List<G> read_only_view {
                owned get {
-                       List<G> instance = _read_only_view;
-                       if (_read_only_view == null) {
+                       List<G>? instance = (List<G>?)_read_only_view.get ();
+                       if (instance == null) {
                                instance = new ReadOnlyList<G> (this);
-                               _read_only_view = instance;
-                               instance.add_weak_pointer ((void**) (&_read_only_view));
+                               _read_only_view.set (instance);
                        }
                        return instance;
                }
diff --git a/gee/abstractmap.vala b/gee/abstractmap.vala
index cd761b8..37abfe3 100644
--- a/gee/abstractmap.vala
+++ b/gee/abstractmap.vala
@@ -91,18 +91,20 @@ public abstract class Gee.AbstractMap<K,V> : Object, Traversable<Map.Entry<K,V>>
         */
        public abstract void clear ();
 
-       private weak Map<K,V> _read_only_view;
+       private WeakRef _read_only_view;
+       construct {
+               _read_only_view = WeakRef(null);
+       }
 
        /**
         * { inheritDoc}
         */
        public virtual Map<K,V> read_only_view {
                owned get {
-                       Map<K,V> instance = _read_only_view;
-                       if (_read_only_view == null) {
+                       Map<K,V>? instance = (Map<K,V>?)_read_only_view.get ();
+                       if (instance == null) {
                                instance = new ReadOnlyMap<K,V> (this);
-                               _read_only_view = instance;
-                               instance.add_weak_pointer ((void**) (&_read_only_view));
+                               _read_only_view.set (instance);
                        }
                        return instance;
                }
diff --git a/gee/abstractmultimap.vala b/gee/abstractmultimap.vala
index 12e84dd..0f17077 100644
--- a/gee/abstractmultimap.vala
+++ b/gee/abstractmultimap.vala
@@ -309,14 +309,17 @@ public abstract class Gee.AbstractMultiMap<K,V> : Object, MultiMap<K,V> {
                public bool mutable { get { return false; } }
        }
 
-       private weak MultiMap<K, V> _read_only_view;
+       private WeakRef _read_only_view;
+       construct {
+               _read_only_view = WeakRef(null);
+       }
+
        public virtual new MultiMap<K, V> read_only_view {
                owned get {
-                       MultiMap<K, V> instance = _read_only_view;
-                       if (_read_only_view == null) {
+                       MultiMap<K, V>? instance = (MultiMap<K, V>?)_read_only_view.get ();
+                       if (instance == null) {
                                instance = new ReadOnlyMultiMap<K, V> (this);
-                               _read_only_view = instance;
-                               instance.add_weak_pointer ((void**) (&_read_only_view));
+                               _read_only_view.set (instance);
                        }
                        return instance;
                }
diff --git a/gee/abstractmultiset.vala b/gee/abstractmultiset.vala
index 3104c55..b237814 100644
--- a/gee/abstractmultiset.vala
+++ b/gee/abstractmultiset.vala
@@ -92,14 +92,17 @@ public abstract class Gee.AbstractMultiSet<G> : AbstractCollection<G>, MultiSet<
                _nitems = 0;
        }
 
-       private weak MultiSet<G> _read_only_view;
+       private WeakRef _read_only_view;
+       construct {
+               _read_only_view = WeakRef(null);
+       }
+
        public virtual new MultiSet<G> read_only_view {
                owned get {
-                       MultiSet<G> instance = _read_only_view;
-                       if (_read_only_view == null) {
+                       MultiSet<G>? instance = (MultiSet<G>?)_read_only_view.get ();
+                       if (instance == null) {
                                instance = new ReadOnlyMultiSet<G> (this);
-                               _read_only_view = instance;
-                               instance.add_weak_pointer ((void**) (&_read_only_view));
+                               _read_only_view.set (instance);
                        }
                        return instance;
                }
diff --git a/gee/abstractset.vala b/gee/abstractset.vala
index 972b4d1..1d411c7 100644
--- a/gee/abstractset.vala
+++ b/gee/abstractset.vala
@@ -31,18 +31,20 @@
  */
 public abstract class Gee.AbstractSet<G> : Gee.AbstractCollection<G>, Set<G> {
 
-       private weak Set<G> _read_only_view;
+       private WeakRef _read_only_view;
+       construct {
+               _read_only_view = WeakRef(null);
+       }
 
        /**
         * { inheritDoc}
         */
        public virtual new Set<G> read_only_view {
                owned get {
-                       Set<G> instance = _read_only_view;
-                       if (_read_only_view == null) {
+                       Set<G>? instance = (Set<G>?)_read_only_view.get ();
+                       if (instance == null) {
                                instance = new ReadOnlySet<G> (this);
-                               _read_only_view = instance;
-                               instance.add_weak_pointer ((void**) (&_read_only_view));
+                               _read_only_view.set (instance);
                        }
                        return instance;
                }
diff --git a/gee/abstractsortedmap.vala b/gee/abstractsortedmap.vala
index 90ec215..fafe75d 100644
--- a/gee/abstractsortedmap.vala
+++ b/gee/abstractsortedmap.vala
@@ -46,17 +46,20 @@ public abstract class Gee.AbstractSortedMap<K, V> : AbstractMap<K,V>, SortedMap<
         */
        public abstract SortedSet<Map.Entry<K,V>> ascending_entries { owned get; }
 
-       private weak SortedMap<K,V> _read_only_view;
+       private WeakRef _read_only_view;
+       construct {
+               _read_only_view = WeakRef(null);
+       }
+
        /**
         * The read-only view this map.
         */
        public new SortedMap<K,V> read_only_view {
                owned get {
-                       SortedMap<K,V> instance = _read_only_view;
-                       if (_read_only_view == null) {
+                       SortedMap<K,V>? instance = (SortedMap<K,V>?)_read_only_view.get ();
+                       if (instance == null) {
                                instance = new ReadOnlySortedMap<K,V> (this);
-                               _read_only_view = instance;
-                               instance.add_weak_pointer ((void**) (&_read_only_view));
+                               _read_only_view.set (instance);
                        }
                        return instance;
                }
diff --git a/gee/abstractsortedset.vala b/gee/abstractsortedset.vala
index 50daca2..eb83d3d 100644
--- a/gee/abstractsortedset.vala
+++ b/gee/abstractsortedset.vala
@@ -78,18 +78,20 @@ public abstract class Gee.AbstractSortedSet<G> : Gee.AbstractSet<G>, SortedSet<G
         */
        public abstract SortedSet<G> sub_set (G from, G to);
 
-       private weak SortedSet<G> _read_only_view;
+       private WeakRef _read_only_view;
+       construct {
+               _read_only_view = WeakRef(null);
+       }
 
        /**
         * { inheritDoc}
         */
        public virtual new SortedSet<G> read_only_view {
                owned get {
-                       SortedSet<G> instance = _read_only_view;
-                       if (_read_only_view == null) {
+                       SortedSet<G>? instance = (SortedSet<G>?)_read_only_view.get ();
+                       if (instance == null) {
                                instance = new ReadOnlySortedSet<G> (this);
-                               _read_only_view = instance;
-                               instance.add_weak_pointer ((void**) (&_read_only_view));
+                               _read_only_view.set (instance);
                        }
                        return instance;
                }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]