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.

85 lines
2.2KB

  1. /*
  2. * DISTRHO Cardinal Plugin
  3. * Copyright (C) 2021-2022 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 3 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 LICENSE file.
  16. */
  17. #pragma once
  18. #include <pthread.h>
  19. /* replace Rack's mutex with our own custom one, which can do priority inversion. */
  20. namespace rack {
  21. struct SharedMutex {
  22. pthread_mutex_t readLock, writeLock;
  23. SharedMutex() noexcept {
  24. pthread_mutexattr_t attr;
  25. pthread_mutexattr_init(&attr);
  26. pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
  27. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  28. pthread_mutex_init(&readLock, &attr);
  29. pthread_mutexattr_destroy(&attr);
  30. pthread_mutexattr_init(&attr);
  31. pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
  32. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
  33. pthread_mutex_init(&writeLock, &attr);
  34. pthread_mutexattr_destroy(&attr);
  35. }
  36. ~SharedMutex() noexcept {
  37. pthread_mutex_destroy(&readLock);
  38. pthread_mutex_destroy(&writeLock);
  39. }
  40. // for std::lock_guard usage, writers lock
  41. void lock() noexcept {
  42. pthread_mutex_lock(&writeLock);
  43. pthread_mutex_lock(&readLock);
  44. }
  45. void unlock() noexcept {
  46. pthread_mutex_unlock(&readLock);
  47. pthread_mutex_unlock(&writeLock);
  48. }
  49. // for SharedLock usage, readers lock
  50. void lock_shared() noexcept {
  51. pthread_mutex_lock(&readLock);
  52. }
  53. void unlock_shared() noexcept {
  54. pthread_mutex_unlock(&readLock);
  55. }
  56. };
  57. template <class Mutex>
  58. struct SharedLock {
  59. Mutex& mutex;
  60. SharedLock(Mutex& m) noexcept : mutex(m) {
  61. mutex.lock_shared();
  62. }
  63. ~SharedLock() noexcept {
  64. mutex.unlock_shared();
  65. }
  66. };
  67. } // namespace rack