Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ThreadSafeFFTW.hpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Thread-safe fftw
  3. * Copyright (C) 2018-2019 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef THREAD_SAFE_FFTW_HPP_INCLUDED
  18. #define THREAD_SAFE_FFTW_HPP_INCLUDED
  19. #include "CarlaDefines.h"
  20. #ifdef CARLA_OS_UNIX
  21. // --------------------------------------------------------------------------------------------------------------------
  22. // Thread-safe fftw
  23. #include "CarlaLibUtils.hpp"
  24. class ThreadSafeFFTW
  25. {
  26. public:
  27. typedef void (*void_func)(void);
  28. struct Deinitializer {
  29. Deinitializer(ThreadSafeFFTW& s)
  30. : tsfftw(s) {}
  31. ~Deinitializer()
  32. {
  33. tsfftw.deinit();
  34. }
  35. ThreadSafeFFTW& tsfftw;
  36. };
  37. ThreadSafeFFTW()
  38. : initialized(false),
  39. libfftw3(nullptr),
  40. libfftw3f(nullptr),
  41. libfftw3l(nullptr),
  42. libfftw3q(nullptr) {}
  43. ~ThreadSafeFFTW()
  44. {
  45. CARLA_SAFE_ASSERT(libfftw3 == nullptr);
  46. }
  47. void init()
  48. {
  49. if (initialized)
  50. return;
  51. initialized = true;
  52. if ((libfftw3 = lib_open("libfftw3_threads.so.3")) != nullptr)
  53. if (const void_func func = lib_symbol<void_func>(libfftw3, "fftw_make_planner_thread_safe"))
  54. func();
  55. if ((libfftw3f = lib_open("libfftw3f_threads.so.3")) != nullptr)
  56. if (const void_func func = lib_symbol<void_func>(libfftw3f, "fftwf_make_planner_thread_safe"))
  57. func();
  58. if ((libfftw3l = lib_open("libfftw3l_threads.so.3")) != nullptr)
  59. if (const void_func func = lib_symbol<void_func>(libfftw3l, "fftwl_make_planner_thread_safe"))
  60. func();
  61. if ((libfftw3q = lib_open("libfftw3q_threads.so.3")) != nullptr)
  62. if (const void_func func = lib_symbol<void_func>(libfftw3q, "fftwq_make_planner_thread_safe"))
  63. func();
  64. }
  65. void deinit()
  66. {
  67. if (! initialized)
  68. return;
  69. initialized = false;
  70. if (libfftw3 != nullptr)
  71. {
  72. lib_close(libfftw3);
  73. libfftw3 = nullptr;
  74. }
  75. if (libfftw3f != nullptr)
  76. {
  77. lib_close(libfftw3f);
  78. libfftw3f = nullptr;
  79. }
  80. if (libfftw3l != nullptr)
  81. {
  82. lib_close(libfftw3l);
  83. libfftw3l = nullptr;
  84. }
  85. if (libfftw3q != nullptr)
  86. {
  87. lib_close(libfftw3q);
  88. libfftw3q = nullptr;
  89. }
  90. }
  91. private:
  92. bool initialized;
  93. lib_t libfftw3;
  94. lib_t libfftw3f;
  95. lib_t libfftw3l;
  96. lib_t libfftw3q;
  97. CARLA_DECLARE_NON_COPY_CLASS(ThreadSafeFFTW)
  98. };
  99. #endif
  100. // --------------------------------------------------------------------------------------------------------------------
  101. #endif // THREAD_SAFE_FFTW_HPP_INCLUDED