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.
 
 
 
 
 
 

168 lines
4.1 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Lesser General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #define CPPCMS_SOURCE
  20. #include "atomic_counter.h"
  21. #include "config.h"
  22. #include <string.h>
  23. #if defined(HAVE_WIN32_INTERLOCKED)
  24. # include <windows.h>
  25. # define cppcms_atomic_set(p,v) ((p)->l=v)
  26. long static cppcms_atomic_add_and_fetch_impl(volatile long *v,long d)
  27. {
  28. long old,prev;
  29. do{
  30. old = *v;
  31. prev = InterlockedCompareExchange(v,old+d,old);
  32. }
  33. while(prev != old);
  34. return old+d;
  35. }
  36. # define cppcms_atomic_add_and_fetch(p,d) cppcms_atomic_add_and_fetch_impl(&(p)->l,d)
  37. #elif defined(HAVE_FREEBSD_ATOMIC)
  38. # include <sys/types.h>
  39. # include <machine/atomic.h>
  40. # define cppcms_atomic_set(p,v) ((p)->ui=v)
  41. # define cppcms_atomic_add_and_fetch(p,d) (atomic_fetchadd_int(&(p)->ui,d) + d)
  42. #elif defined(HAVE_SOLARIS_ATOMIC)
  43. # include <atomic.h>
  44. # define cppcms_atomic_set(p,v) ((p)->ui=v)
  45. # define cppcms_atomic_add_and_fetch(p,d) (atomic_add_int_nv(&(p)->ui,d))
  46. #elif defined(HAVE_MAC_OS_X_ATOMIC)
  47. # include <libkern/OSAtomic.h>
  48. # define cppcms_atomic_set(p,v) ((p)->i=v)
  49. # define cppcms_atomic_add_and_fetch(p,d) (OSAtomicAdd32(d,&(p)->i))
  50. #elif defined HAVE_SYNC_FETCH_AND_ADD
  51. # define cppcms_atomic_set(p,v) ((p)->l=v)
  52. # define cppcms_atomic_add_and_fetch(p,d) ( __sync_add_and_fetch(&(p)->i,d) )
  53. # elif defined(HAVE_GCC_BITS_EXCHANGE_AND_ADD)
  54. # include <bits/atomicity.h>
  55. using __gnu_cxx::__exchange_and_add;
  56. # define cppcms_atomic_set(p,v) ((p)->i=v)
  57. # define cppcms_atomic_add_and_fetch(p,d) ( __exchange_and_add(&(p)->i,d)+d )
  58. #elif defined(HAVE_GCC_EXT_EXCHANGE_AND_ADD)
  59. # include <ext/atomicity.h>
  60. using __gnu_cxx::__exchange_and_add;
  61. # define cppcms_atomic_set(p,v) ((p)->i=v)
  62. # define cppcms_atomic_add_and_fetch(p,d) ( __exchange_and_add(&(p)->i,d)+d )
  63. #else // Failing back to pthreads
  64. # include <pthread.h>
  65. # define CPPCMS_PTHREAD_ATOMIC
  66. #endif
  67. namespace cppcms {
  68. #if !defined(CPPCMS_PTHREAD_ATOMIC)
  69. atomic_counter::atomic_counter(long value)
  70. {
  71. memset(&value_,0,sizeof(value_));
  72. mutex_ = 0;
  73. cppcms_atomic_set(&value_,value);
  74. }
  75. atomic_counter::~atomic_counter()
  76. {
  77. }
  78. long atomic_counter::inc()
  79. {
  80. return cppcms_atomic_add_and_fetch( &value_, 1 );
  81. }
  82. long atomic_counter::dec()
  83. {
  84. return cppcms_atomic_add_and_fetch( &value_, -1 );
  85. }
  86. long atomic_counter::get() const
  87. {
  88. return cppcms_atomic_add_and_fetch( &value_, 0 );
  89. }
  90. #else
  91. #define MUTEX (reinterpret_cast<pthread_mutex_t *>(mutex_))
  92. atomic_counter::atomic_counter(long value)
  93. {
  94. mutex_ = new pthread_mutex_t;
  95. pthread_mutex_init(MUTEX,0);
  96. value_.l=value;
  97. }
  98. atomic_counter::~atomic_counter()
  99. {
  100. pthread_mutex_destroy(MUTEX);
  101. delete MUTEX;
  102. mutex_ = 0;
  103. }
  104. long atomic_counter::inc()
  105. {
  106. pthread_mutex_lock(MUTEX);
  107. long result= ++value_.l;
  108. pthread_mutex_unlock(MUTEX);
  109. return result;
  110. }
  111. long atomic_counter::dec()
  112. {
  113. pthread_mutex_lock(MUTEX);
  114. long result= --value_.l;
  115. pthread_mutex_unlock(MUTEX);
  116. return result;
  117. }
  118. long atomic_counter::get() const
  119. {
  120. pthread_mutex_lock(MUTEX);
  121. long result= value_.l;
  122. pthread_mutex_unlock(MUTEX);
  123. return result;
  124. }
  125. #endif
  126. } // cppcms