LocklessTypes.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "LocklessTypes.h"
  11. using namespace RakNet;
  12. LocklessUint32_t::LocklessUint32_t()
  13. {
  14. value=0;
  15. }
  16. LocklessUint32_t::LocklessUint32_t(uint32_t initial)
  17. {
  18. value=initial;
  19. }
  20. uint32_t LocklessUint32_t::Increment(void)
  21. {
  22. #ifdef _WIN32
  23. return (uint32_t) InterlockedIncrement(&value);
  24. #elif defined(ANDROID) || defined(__S3E__) || defined(__APPLE__)
  25. uint32_t v;
  26. mutex.Lock();
  27. ++value;
  28. v=value;
  29. mutex.Unlock();
  30. return v;
  31. #else
  32. return __sync_fetch_and_add (&value, (uint32_t) 1);
  33. #endif
  34. }
  35. uint32_t LocklessUint32_t::Decrement(void)
  36. {
  37. #ifdef _WIN32
  38. return (uint32_t) InterlockedDecrement(&value);
  39. #elif defined(ANDROID) || defined(__S3E__) || defined(__APPLE__)
  40. uint32_t v;
  41. mutex.Lock();
  42. --value;
  43. v=value;
  44. mutex.Unlock();
  45. return v;
  46. #else
  47. return __sync_fetch_and_add (&value, (uint32_t) -1);
  48. #endif
  49. }
粤ICP备19079148号