Add hb_threadsafe_set_t
authorBehdad Esfahbod <behdad@behdad.org>
Thu, 5 May 2011 19:07:54 +0000 (15:07 -0400)
committerBehdad Esfahbod <behdad@behdad.org>
Thu, 5 May 2011 19:07:54 +0000 (15:07 -0400)
src/hb-mutex-private.hh
src/hb-private.hh

index cf5beb1..65e5892 100644 (file)
@@ -110,4 +110,58 @@ struct hb_static_mutex_t : hb_mutex_t
 
 HB_END_DECLS
 
+
+template <typename item_t>
+struct hb_threadsafe_set_t
+{
+  hb_set_t <item_t> set;
+  hb_static_mutex_t mutex;
+
+  template <typename T>
+  inline item_t *insert (T v)
+  {
+    hb_mutex_lock (&mutex);
+    item_t *item = set.insert (v);
+    hb_mutex_unlock (&mutex);
+    return item;
+  }
+
+  template <typename T>
+  inline void remove (T v)
+  {
+    hb_mutex_lock (&mutex);
+    set.remove (v);
+    hb_mutex_unlock (&mutex);
+  }
+
+  template <typename T>
+  inline item_t *find (T v)
+  {
+    hb_mutex_lock (&mutex);
+    item_t *item = set.find (v);
+    hb_mutex_unlock (&mutex);
+    return item;
+  }
+
+  template <typename T>
+  inline item_t *find_or_insert (T v) {
+    hb_mutex_lock (&mutex);
+    item_t *item = set.find_or_insert (v);
+    hb_mutex_unlock (&mutex);
+    return item;
+  }
+
+  void finish (void) {
+    hb_mutex_lock (&mutex);
+    set.finish ();
+    hb_mutex_unlock (&mutex);
+  }
+
+};
+
+
+HB_BEGIN_DECLS
+
+HB_END_DECLS
+
 #endif /* HB_MUTEX_PRIVATE_HH */
index d31e014..577389d 100644 (file)
@@ -324,8 +324,6 @@ struct hb_set_t
 {
   hb_array_t <item_t> items;
 
-  public:
-
   template <typename T>
   inline item_t *insert (T v)
   {
@@ -370,6 +368,7 @@ struct hb_set_t
   void finish (void) {
     for (unsigned i = 0; i < items.len; i++)
       items[i].finish ();
+    items.shrink (0);
   }
 
 };