aboutsummaryrefslogtreecommitdiffstats
path: root/src/algo_factory
diff options
context:
space:
mode:
authorlloyd <[email protected]>2011-07-05 20:45:23 +0000
committerlloyd <[email protected]>2011-07-05 20:45:23 +0000
commit1dbbec7519d75f7bc5b8fa23eb175c8a8f96a9b5 (patch)
tree4b2096bb10cd4de87fcd8aef7549e39cc1a0c95d /src/algo_factory
parentb16bd4ad156f954dbcd4299e298765eececac19d (diff)
The Algorithm_Factory has this logic on looking for an object:
- Check the cache; if found, return value - Populate cache, if the value is already there, delete the old object and save the new one. - Recheck the cache value Raja <[email protected]> pointed out on the list that this could race if multiple threads called a lookup function in close succession while the cache was cold. All of them would fail the lookup, then each of them would add it, but the values returned would be deleted by other threads. Instead, declare that first write wins. Then, the cache stays consistent even if there is a race, the only issue is an extra search and delete. Modify GOST and Skein, as their name() function did not roundtrip properly which caused failures otherwise.
Diffstat (limited to 'src/algo_factory')
-rw-r--r--src/algo_factory/algo_cache.h10
1 files changed, 6 insertions, 4 deletions
diff --git a/src/algo_factory/algo_cache.h b/src/algo_factory/algo_cache.h
index 0d891ad3f..25f2db023 100644
--- a/src/algo_factory/algo_cache.h
+++ b/src/algo_factory/algo_cache.h
@@ -1,6 +1,6 @@
/*
* An algorithm cache (used by Algorithm_Factory)
-* (C) 2008-2009 Jack Lloyd
+* (C) 2008-2009,2011 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
@@ -171,14 +171,16 @@ void Algorithm_Cache<T>::add(T* algo,
Mutex_Holder lock(mutex);
- delete algorithms[algo->name()][provider];
- algorithms[algo->name()][provider] = algo;
-
if(algo->name() != requested_name &&
aliases.find(requested_name) == aliases.end())
{
aliases[requested_name] = algo->name();
}
+
+ if(!algorithms[algo->name()][provider])
+ algorithms[algo->name()][provider] = algo;
+ else
+ delete algo;
}
/*