Skip to content

Commit

Permalink
Added mutex to FFT plan cache function.
Browse files Browse the repository at this point in the history
  • Loading branch information
crisluengo committed Dec 5, 2024
1 parent baaf75e commit 0d213d2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions changelogs/diplib_next.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ date: 2020-00-00
This would throw an exception if the image was protected, but was always a bad thing to do.
See [issue #170](https://github.com/DIPlib/diplib/issues/170).

- The Fourier Transform, when using the default PocketFFT, used a plan cache that was not thread safe. When calling
any function using a Fourier Transform from multiple threads, a race condition could occur. We've added a mutex
to the function that maintains the cache to avoid this.

### Updated dependencies

### Build changes
Expand Down
5 changes: 5 additions & 0 deletions src/transform/dft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <cmath>
#include <complex>
#include <limits>
#include <mutex>
#include <vector>

#include "diplib.h"
Expand Down Expand Up @@ -91,6 +92,8 @@ using RPlan = pocketfft::pocketfft_r< T >;

namespace {

std::mutex planCacheMutex;

// This is a container that holds plans, and maintains a use count. Plans in use cannot be
// deleted.
//
Expand All @@ -99,6 +102,8 @@ namespace {
// PlanType is CPlan< T > or RPlan< T >, T is dip::sfloat or dip::dfloat. There are 4 caches.
template< typename PlanType >
PlanType* PlanCache( dip::uint length, bool free = false ) {
std::lock_guard< std::mutex > guard( planCacheMutex );

struct Data {
dip::uint use_count;
dip::uint last_access;
Expand Down

0 comments on commit 0d213d2

Please sign in to comment.