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.

87 lines
2.3KB

  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;
  23. pthread_mutex_t writeLock;
  24. SharedMutex() noexcept {
  25. pthread_mutexattr_t attr;
  26. pthread_mutexattr_init(&attr);
  27. pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
  28. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  29. pthread_mutex_init(&readLock, &attr);
  30. pthread_mutexattr_destroy(&attr);
  31. pthread_mutexattr_t attr2;
  32. pthread_mutexattr_init(&attr2);
  33. pthread_mutexattr_setprotocol(&attr2, PTHREAD_PRIO_NONE);
  34. pthread_mutexattr_settype(&attr2, PTHREAD_MUTEX_NORMAL);
  35. pthread_mutex_init(&writeLock, &attr2);
  36. pthread_mutexattr_destroy(&attr2);
  37. }
  38. ~SharedMutex() noexcept {
  39. pthread_mutex_destroy(&readLock);
  40. pthread_mutex_destroy(&writeLock);
  41. }
  42. // for std::lock_guard usage, writers lock
  43. void lock() noexcept {
  44. pthread_mutex_lock(&readLock);
  45. pthread_mutex_lock(&writeLock);
  46. }
  47. void unlock() noexcept {
  48. pthread_mutex_unlock(&writeLock);
  49. pthread_mutex_unlock(&readLock);
  50. }
  51. // for SharedLock usage, readers lock
  52. void lock_shared() noexcept {
  53. pthread_mutex_lock(&readLock);
  54. }
  55. void unlock_shared() noexcept {
  56. pthread_mutex_unlock(&readLock);
  57. }
  58. };
  59. template <class Mutex>
  60. struct SharedLock {
  61. Mutex& mutex;
  62. SharedLock(Mutex& m) noexcept : mutex(m) {
  63. mutex.lock_shared();
  64. }
  65. ~SharedLock() noexcept {
  66. mutex.unlock_shared();
  67. }
  68. };
  69. } // namespace rack