atomic_count.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // boost/detail/atomic_count.hpp - thread/SMP safe reference counter
  9. //
  10. // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
  11. //
  12. // Distributed under the Boost Software License, Version 1.0. (See
  13. // accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. //
  16. // typedef <implementation-defined> boost::detail::atomic_count;
  17. //
  18. // atomic_count a(n);
  19. //
  20. // (n is convertible to long)
  21. //
  22. // Effects: Constructs an atomic_count with an initial value of n
  23. //
  24. // a;
  25. //
  26. // Returns: (long) the current value of a
  27. //
  28. // ++a;
  29. //
  30. // Effects: Atomically increments the value of a
  31. // Returns: (long) the new value of a
  32. //
  33. // --a;
  34. //
  35. // Effects: Atomically decrements the value of a
  36. // Returns: (long) the new value of a
  37. //
  38. // Important note: when --a returns zero, it must act as a
  39. // read memory barrier (RMB); i.e. the calling thread must
  40. // have a synchronized view of the memory
  41. //
  42. // On Intel IA-32 (x86) memory is always synchronized, so this
  43. // is not a problem.
  44. //
  45. // On many architectures the atomic instructions already act as
  46. // a memory barrier.
  47. //
  48. // This property is necessary for proper reference counting, since
  49. // a thread can update the contents of a shared object, then
  50. // release its reference, and another thread may immediately
  51. // release the last reference causing object destruction.
  52. //
  53. // The destructor needs to have a synchronized view of the
  54. // object to perform proper cleanup.
  55. //
  56. // Original example by Alexander Terekhov:
  57. //
  58. // Given:
  59. //
  60. // - a mutable shared object OBJ;
  61. // - two threads THREAD1 and THREAD2 each holding
  62. // a private smart_ptr object pointing to that OBJ.
  63. //
  64. // t1: THREAD1 updates OBJ (thread-safe via some synchronization)
  65. // and a few cycles later (after "unlock") destroys smart_ptr;
  66. //
  67. // t2: THREAD2 destroys smart_ptr WITHOUT doing any synchronization
  68. // with respect to shared mutable object OBJ; OBJ destructors
  69. // are called driven by smart_ptr interface...
  70. //
  71. #include <boost/config.hpp>
  72. #include <boost/smart_ptr/detail/sp_has_sync.hpp>
  73. #ifndef BOOST_HAS_THREADS
  74. namespace boost
  75. {
  76. namespace detail
  77. {
  78. typedef long atomic_count;
  79. }
  80. }
  81. #elif defined(BOOST_AC_USE_PTHREADS)
  82. # include <boost/smart_ptr/detail/atomic_count_pthreads.hpp>
  83. #elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
  84. # include <boost/smart_ptr/detail/atomic_count_gcc_x86.hpp>
  85. #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
  86. # include <boost/smart_ptr/detail/atomic_count_win32.hpp>
  87. #elif defined( BOOST_SP_HAS_SYNC )
  88. # include <boost/smart_ptr/detail/atomic_count_sync.hpp>
  89. #elif defined(__GLIBCPP__) || defined(__GLIBCXX__)
  90. # include <boost/smart_ptr/detail/atomic_count_gcc.hpp>
  91. #elif defined(BOOST_HAS_PTHREADS)
  92. # define BOOST_AC_USE_PTHREADS
  93. # include <boost/smart_ptr/detail/atomic_count_pthreads.hpp>
  94. #else
  95. // Use #define BOOST_DISABLE_THREADS to avoid the error
  96. #error Unrecognized threading platform
  97. #endif
  98. #endif // #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_HPP_INCLUDED