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.
 
 
 
 
 
 

76 lines
1.6 KiB

  1. #ifndef CPPCMS_ATOMIC_COUNT_H
  2. #define CPPCMS_ATOMIC_COUNT_H
  3. #include "defs.h"
  4. #if !defined(CPPCMS_WIN32)
  5. # include <pthread.h>
  6. #endif
  7. namespace cppcms {
  8. ///
  9. /// \brief Atomic counter is a class that allows perform counting in thread safe way.
  10. ///
  11. /// It is mainly used for reference counting. Under Windows it uses Interlocked API, under
  12. /// other platforms it used built-in atomic operations or fails back to pthreads locking implementation.
  13. ///
  14. /// Notes:
  15. ///
  16. /// - This counter is not safe for use in process shared memory, when pthreads fall-back is used
  17. /// - Under POSIX platform pthread_mutex_t is always present in order to make sure that we can implement
  18. /// or remove pthread fall-back at any point not affecting ABI
  19. ///
  20. class CPPCMS_API atomic_counter {
  21. public:
  22. ///
  23. /// Create a counter with initial value v
  24. ///
  25. explicit atomic_counter( long v );
  26. ~atomic_counter();
  27. ///
  28. /// Increment and return the result after increment atomically
  29. ///
  30. long operator++()
  31. {
  32. return inc();
  33. }
  34. ///
  35. /// Decrement and return the result after decrement atomically
  36. ///
  37. long operator--()
  38. {
  39. return dec();
  40. }
  41. ///
  42. /// Return current value - atomically
  43. ///
  44. operator long() const
  45. {
  46. return get();
  47. }
  48. private:
  49. long inc();
  50. long dec();
  51. long get() const;
  52. atomic_counter(atomic_counter const &);
  53. atomic_counter & operator=(atomic_counter const &);
  54. mutable long value_;
  55. #if !defined(CPPCMS_WIN32)
  56. // Is actually used for platforms without lock
  57. // it would not be used when atomic operations
  58. // available
  59. mutable pthread_mutex_t mutex_;
  60. #endif
  61. };
  62. } // cppcms
  63. #endif