jiface.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "jiface.hpp"
  14. #include "jlib.hpp"
  15. #include <assert.h>
  16. #include "jmutex.hpp"
  17. //===========================================================================
  18. bool CSimpleInterface::Release() const
  19. {
  20. return CSimpleInterfaceOf<CEmptyClass>::Release();
  21. }
  22. bool CInterface::Release() const
  23. {
  24. return CInterfaceOf<CEmptyClass>::Release();
  25. }
  26. //===========================================================================
  27. #if !defined(_WIN32) && !defined(__GNUC__)
  28. static CriticalSection *ICrit;
  29. MODULE_INIT(INIT_PRIORITY_JIFACE)
  30. {
  31. ICrit = new CriticalSection();
  32. return true;
  33. }
  34. MODULE_EXIT()
  35. {
  36. // delete ICrit; - need to make sure this is deleted after anything that uses it
  37. }
  38. int poor_atomic_dec_and_read(atomic_t * v)
  39. {
  40. ICrit->enter();
  41. int ret = --(*v);
  42. ICrit->leave();
  43. return ret;
  44. }
  45. bool poor_atomic_inc_and_test(atomic_t * v)
  46. {
  47. ICrit->enter();
  48. bool ret = (++(*v) == 0);
  49. ICrit->leave();
  50. return ret;
  51. }
  52. int poor_atomic_xchg(int i, atomic_t * v)
  53. {
  54. ICrit->enter();
  55. int prev = (*v);
  56. (*v)=i;
  57. ICrit->leave();
  58. return prev;
  59. }
  60. void poor_atomic_add(atomic_t * v, int i)
  61. {
  62. ICrit->enter();
  63. (*v) += i;
  64. ICrit->leave();
  65. }
  66. int poor_atomic_add_and_read(atomic_t * v, int i)
  67. {
  68. ICrit->enter();
  69. (*v) += i;
  70. int ret = (*v);
  71. ICrit->leave();
  72. return ret;
  73. }
  74. int poor_atomic_add_exchange(atomic_t * v, int i)
  75. {
  76. ICrit->enter();
  77. int prev = (*v);
  78. (*v)=prev+i;
  79. ICrit->leave();
  80. return prev;
  81. }
  82. bool poor_atomic_cas(atomic_t * v, int newvalue, int expectedvalue)
  83. {
  84. ICrit->enter();
  85. bool ret = false;
  86. if ((*v)==expectedvalue)
  87. {
  88. *v=newvalue;
  89. ret = true;
  90. }
  91. ICrit->leave();
  92. return ret;
  93. }
  94. void *poor_atomic_xchg_ptr(void *p, void** v)
  95. {
  96. ICrit->enter();
  97. void * prev = (*v);
  98. (*v)=p;
  99. ICrit->leave();
  100. return prev;
  101. }
  102. bool poor_atomic_cas_ptr(void ** v, void * newvalue, void * expectedvalue)
  103. {
  104. ICrit->enter();
  105. bool ret = false;
  106. if ((*v)==expectedvalue)
  107. {
  108. *v=newvalue;
  109. ret = true;
  110. }
  111. ICrit->leave();
  112. return ret;
  113. }
  114. //Hopefully the function call will be enough to stop the compiler reordering any operations
  115. void poor_compiler_memory_barrier()
  116. {
  117. }
  118. #endif