atomic_count.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef DAHUA_DETAIL_ATOMIC_COUNT_HPP_INCLUDED
  2. #define DAHUA_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: nothing
  32. //
  33. // --a;
  34. //
  35. // Effects: Atomically decrements the value of a
  36. // Returns: (long) zero if the new value of a is zero,
  37. // unspecified non-zero value otherwise (usually the new value)
  38. //
  39. // Important note: when --a returns zero, it must act as a
  40. // read memory barrier (RMB); i.e. the calling thread must
  41. // have a synchronized view of the memory
  42. //
  43. // On Intel IA-32 (x86) memory is always synchronized, so this
  44. // is not a problem.
  45. //
  46. // On many architectures the atomic instructions already act as
  47. // a memory barrier.
  48. //
  49. // This property is necessary for proper reference counting, since
  50. // a thread can update the contents of a shared object, then
  51. // release its reference, and another thread may immediately
  52. // release the last reference causing object destruction.
  53. //
  54. // The destructor needs to have a synchronized view of the
  55. // object to perform proper cleanup.
  56. //
  57. // Original example by Alexander Terekhov:
  58. //
  59. // Given:
  60. //
  61. // - a mutable shared object OBJ;
  62. // - two threads THREAD1 and THREAD2 each holding
  63. // a private smart_ptr object pointing to that OBJ.
  64. //
  65. // t1: THREAD1 updates OBJ (thread-safe via some synchronization)
  66. // and a few cycles later (after "unlock") destroys smart_ptr;
  67. //
  68. // t2: THREAD2 destroys smart_ptr WITHOUT doing any synchronization
  69. // with respect to shared mutable object OBJ; OBJ destructors
  70. // are called driven by smart_ptr interface...
  71. //
  72. #if defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
  73. # include "atomic_count_gcc_x86.hpp"
  74. #elif defined WIN64 || defined _WIN64 || defined(__WIN64__)
  75. # include "atomic_count_win64.hpp"
  76. #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
  77. # include "atomic_count_win32.hpp"
  78. #elif defined( __GNUC__ ) && ( __GNUC__ * 100 + __GNUC_MINOR__ >= 401 )
  79. # include "atomic_count_sync.hpp"
  80. #elif defined(__GLIBCPP__) || defined(__GLIBCXX__)
  81. # include "atomic_count_gcc.hpp"
  82. #elif defined(__linux__)
  83. # include "atomic_count_pthreads.hpp"
  84. #else
  85. // Use #define DAHUA_DISABLE_THREADS to avoid the error
  86. #error Unrecognized threading platform
  87. #endif
  88. #endif // #ifndef DAHUA_DETAIL_ATOMIC_COUNT_HPP_INCLUDED