From d37486d87b65c5abaaa2998fa5c9e48eedde0933 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 5 May 2011 15:07:54 -0400 Subject: [PATCH] Add hb_threadsafe_set_t --- src/hb-mutex-private.hh | 54 +++++++++++++++++++++++++++++++++++++++++ src/hb-private.hh | 3 +-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/hb-mutex-private.hh b/src/hb-mutex-private.hh index cf5beb13c..65e589223 100644 --- a/src/hb-mutex-private.hh +++ b/src/hb-mutex-private.hh @@ -108,6 +108,60 @@ struct hb_static_mutex_t : hb_mutex_t }; +HB_END_DECLS + + +template +struct hb_threadsafe_set_t +{ + hb_set_t set; + hb_static_mutex_t mutex; + + template + inline item_t *insert (T v) + { + hb_mutex_lock (&mutex); + item_t *item = set.insert (v); + hb_mutex_unlock (&mutex); + return item; + } + + template + inline void remove (T v) + { + hb_mutex_lock (&mutex); + set.remove (v); + hb_mutex_unlock (&mutex); + } + + template + inline item_t *find (T v) + { + hb_mutex_lock (&mutex); + item_t *item = set.find (v); + hb_mutex_unlock (&mutex); + return item; + } + + template + 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 */ diff --git a/src/hb-private.hh b/src/hb-private.hh index d31e01435..577389dd1 100644 --- a/src/hb-private.hh +++ b/src/hb-private.hh @@ -324,8 +324,6 @@ struct hb_set_t { hb_array_t items; - public: - template 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); } };