changeset 1132:0cb263a2f52c 3.0-integration

Fix lock contention in FFTapi
author Chris Cannam
date Mon, 12 Oct 2015 15:06:15 +0100
parents 2c43f9904068
children f8604a7c4660
files data/fft/FFTapi.cpp data/fft/FFTapi.h
diffstat 2 files changed, 13 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/data/fft/FFTapi.cpp	Thu Aug 20 13:33:46 2015 +0100
+++ b/data/fft/FFTapi.cpp	Mon Oct 12 15:06:15 2015 +0100
@@ -16,6 +16,8 @@
 
 #include "FFTapi.h"
 
+std::mutex FFTForward::m_mutex;
+
 #ifndef HAVE_FFTW3F
 
 #include <cmath>
--- a/data/fft/FFTapi.h	Thu Aug 20 13:33:46 2015 +0100
+++ b/data/fft/FFTapi.h	Mon Oct 12 15:06:15 2015 +0100
@@ -52,18 +52,24 @@
 
 #include <vector>
 #include <complex>
+#include <mutex>
 
 class FFTForward // with fft shift but not window
 {
+    static std::mutex m_mutex;
+    
 public:
     FFTForward(int size) :
-        m_size(size),
-        m_input((float *)fftf_malloc(size * sizeof(float))),
-        m_output((fftf_complex *)fftf_malloc((size/2 + 1) * sizeof(fftf_complex))),
-        m_plan(fftf_plan_dft_r2c_1d(size, m_input, m_output, FFTW_MEASURE))
-    { }
+        m_size(size)
+    {
+        std::lock_guard<std::mutex> lock(m_mutex);
+        m_input = (float *)fftf_malloc(size * sizeof(float));
+        m_output = (fftf_complex *)fftf_malloc((size/2 + 1) * sizeof(fftf_complex));
+        m_plan = fftf_plan_dft_r2c_1d(size, m_input, m_output, FFTW_ESTIMATE);
+    }
 
     ~FFTForward() {
+        std::lock_guard<std::mutex> lock(m_mutex);
         fftf_destroy_plan(m_plan);
         fftf_free(m_input);
         fftf_free(m_output);