- uhash_iput(cache, (int32_t) source, t, &ec);
+ Transliterator *rt = NULL;
+ {
+ Mutex m(NULL);
+ rt = static_cast<Transliterator *> (uhash_iget(cache, (int32_t) source));
+ if (rt == NULL) {
+ // Common case, no race to cache this new transliterator.
+ uhash_iput(cache, (int32_t) source, t, &ec);
+ } else {
+ // Race case, some other thread beat us to caching this transliterator.
+ Transliterator *temp = rt;
+ rt = t; // Our newly created transliterator that lost the race & now needs deleting.
+ t = temp; // The transliterator from the cache that we will return.
+ }
+ }
+ delete rt; // will be non-null only in case of races.