ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
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.
 
 
 
 
 
 

102 lines
1.5 KiB

  1. #define CPPCMS_SOURCE
  2. #include "atomic_counter.h"
  3. #include "config.h"
  4. #ifdef CPPCMS_WIN32
  5. # include <windows.h>
  6. #endif
  7. namespace cppcms {
  8. #if defined(CPPCMS_WIN32) || defined(HAVE_SYNC_FETCH_AND_ADD)
  9. atomic_counter::atomic_counter(long value) : value_(value)
  10. {
  11. }
  12. atomic_counter::~atomic_counter()
  13. {
  14. }
  15. #else
  16. atomic_counter::atomic_counter(long value) :
  17. value_(value)
  18. {
  19. pthread_mutex_init(&mutex_,0);
  20. }
  21. atomic_counter::~atomic_counter()
  22. {
  23. pthread_mutex_destroy(&mutex_);
  24. }
  25. #endif
  26. #ifdef CPPCMS_WIN32
  27. long atomic_counter::inc()
  28. {
  29. return InterlockedIncrement(&value_);
  30. }
  31. long atomic_counter::dec()
  32. {
  33. return InterlockedDecrement(&value_);
  34. }
  35. long atomic_counter::get() const
  36. {
  37. return *reinterpret_cast<volatile long *>(&value_);
  38. }
  39. #elif defined(HAVE_SYNC_FETCH_AND_ADD)
  40. long atomic_counter::inc()
  41. {
  42. return __sync_add_and_fetch( &value_, 1 );
  43. }
  44. long atomic_counter::dec()
  45. {
  46. return __sync_add_and_fetch( &value_, -1 );
  47. }
  48. long atomic_counter::get() const
  49. {
  50. return __sync_fetch_and_add( &value_, 0 );
  51. }
  52. #else // pthreads
  53. long atomic_counter::inc()
  54. {
  55. pthread_mutex_lock(&mutex_);
  56. long result= ++value_;
  57. pthread_mutex_unlock(&mutex_);
  58. return result;
  59. }
  60. long atomic_counter::dec()
  61. {
  62. pthread_mutex_lock(&mutex_);
  63. long result= --value_;
  64. pthread_mutex_unlock(&mutex_);
  65. return result;
  66. }
  67. long atomic_counter::get() const
  68. {
  69. pthread_mutex_lock(&mutex_);
  70. long result= value_;
  71. pthread_mutex_unlock(&mutex_);
  72. return result;
  73. }
  74. #endif
  75. } // cppcms