shared_object.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * shared_object.h - class sharedObject for use among other objects
  3. *
  4. * Copyright (c) 2006-2007 Javier Serrano Polo <jasp00/at/users.sourceforge.net>
  5. * Copyright (c) 2008-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #ifndef SHARED_OBJECT_H
  26. #define SHARED_OBJECT_H
  27. #include <atomic>
  28. class sharedObject
  29. {
  30. public:
  31. sharedObject() :
  32. m_referenceCount(1)
  33. {
  34. }
  35. virtual ~sharedObject()
  36. {
  37. }
  38. template<class T>
  39. static T* ref( T* object )
  40. {
  41. // Incrementing an atomic reference count can be relaxed since no action
  42. // is ever taken as a result of increasing the count.
  43. // Other loads and stores can be reordered around this without consequence.
  44. object->m_referenceCount.fetch_add(1, std::memory_order_relaxed);
  45. return object;
  46. }
  47. template<class T>
  48. static void unref( T* object )
  49. {
  50. // When decrementing an atomic reference count, we need to provide
  51. // two ordering guarantees:
  52. // 1. All reads and writes to the referenced object occur before
  53. // the count reaches zero.
  54. // 2. Deletion occurs after the count reaches zero.
  55. //
  56. // To accomplish this, each decrement must be store-released,
  57. // and the final thread (which is deleting the referenced data)
  58. // must load-acquire those stores.
  59. // The simplest way to do this to give the decrement acquire-release
  60. // semantics.
  61. //
  62. // See https://www.boost.org/doc/libs/1_67_0/doc/html/atomic/usage_examples.html
  63. // for further discussion, along with a slightly more complicated
  64. // (but possibly more performant on weakly-ordered hardware like ARM)
  65. // approach.
  66. const bool deleteObject =
  67. object->m_referenceCount.fetch_sub(1, std::memory_order_acq_rel) == 1;
  68. if ( deleteObject )
  69. {
  70. object->deleteLater();
  71. }
  72. }
  73. private:
  74. std::atomic_int m_referenceCount;
  75. } ;
  76. #endif