Commit 5eb570e0 authored by Martin Reinecke's avatar Martin Reinecke

make caching thread-safe

parent d3d3204c
......@@ -48,6 +48,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <memory>
#include <vector>
#include <complex>
#include <mutex>
#ifdef POCKETFFT_OPENMP
#include <omp.h>
#endif
......@@ -2205,18 +2206,31 @@ template<typename T> shared_ptr<T> get_plan(size_t length)
constexpr size_t nmax=10;
static vector<shared_ptr<T>> cache(nmax);
static size_t lastpos=0;
// searching phase
size_t pos = lastpos;
do {
if (!cache[pos]) break;
if (cache[pos]->length()==length)
return cache[pos];
pos = (pos==0) ? nmax-1 : pos-1;
} while (pos != lastpos);
// adding phase
static mutex mut;
{
lock_guard<mutex> lock(mut);
for (size_t i=0; i<nmax; ++i)
{
if (cache[i]==nullptr)
break;
if (cache[i]->length()==length)
return cache[i];
}
}
auto plan = make_shared<T>(length);
{
lock_guard<mutex> lock(mut);
for (size_t i=0; i<nmax; ++i)
{
if (cache[i]==nullptr)
break;
if (cache[i]->length()==length)
return cache[i];
}
cache[lastpos] = plan;
if (++lastpos>=nmax) lastpos=0;
cache[lastpos] = make_shared<T>(length);
return cache[lastpos];
}
return plan;
}
class arr_info
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment