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. ...@@ -48,6 +48,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <complex> #include <complex>
#include <mutex>
#ifdef POCKETFFT_OPENMP #ifdef POCKETFFT_OPENMP
#include <omp.h> #include <omp.h>
#endif #endif
...@@ -2205,18 +2206,31 @@ template<typename T> shared_ptr<T> get_plan(size_t length) ...@@ -2205,18 +2206,31 @@ template<typename T> shared_ptr<T> get_plan(size_t length)
constexpr size_t nmax=10; constexpr size_t nmax=10;
static vector<shared_ptr<T>> cache(nmax); static vector<shared_ptr<T>> cache(nmax);
static size_t lastpos=0; static size_t lastpos=0;
// searching phase static mutex mut;
size_t pos = lastpos; {
do { lock_guard<mutex> lock(mut);
if (!cache[pos]) break; for (size_t i=0; i<nmax; ++i)
if (cache[pos]->length()==length) {
return cache[pos]; if (cache[i]==nullptr)
pos = (pos==0) ? nmax-1 : pos-1; break;
} while (pos != lastpos); if (cache[i]->length()==length)
// adding phase 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; if (++lastpos>=nmax) lastpos=0;
cache[lastpos] = make_shared<T>(length); }
return cache[lastpos]; return plan;
} }
class arr_info 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