Atomics.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. // Modified by Lasse Oorni for Urho3D
  13. #ifdef WIN32
  14. // Urho3D: windows.h in lowercase to fix MinGW cross-compiling on a case-sensitive system
  15. #include <windows.h>
  16. #endif
  17. /** @file Atomics.h
  18. @brief Abstracts platform-specific atomics operations. */
  19. // bool CmpXChgPointer(word *dst, word newVal, word cmp);
  20. // Assigns *dst = newVal, but only if *dst == cmp before the assignment. This evaluation is done atomically.
  21. // Returns true if the assignment succeeded.
  22. // Do NOT call this macro with any side expressions on dst, newVal or cmp.
  23. #ifdef WIN32
  24. // See http://msdn.microsoft.com/en-us/library/ms683568(VS.85).aspx
  25. #define CmpXChgPointer(dst, newVal, cmp) (InterlockedCompareExchangePointer((dst), (newVal), (cmp)) == (cmp))
  26. #else
  27. // See http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html
  28. #define CmpXChgPointer(dst, newVal, cmp) __sync_bool_compare_and_swap((dst), (cmp), (newVal))
  29. #endif