atomic.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. Copyright (c) 2013 Martin Sustrik All rights reserved.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"),
  5. to deal in the Software without restriction, including without limitation
  6. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. and/or sell copies of the Software, and to permit persons to whom
  8. the Software is furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included
  10. in all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  14. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  16. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  17. IN THE SOFTWARE.
  18. */
  19. #ifndef NN_ATOMIC_INCLUDED
  20. #define NN_ATOMIC_INCLUDED
  21. #if defined NN_HAVE_WINDOWS
  22. #include "win.h"
  23. #define NN_ATOMIC_WINAPI
  24. #elif NN_HAVE_ATOMIC_SOLARIS
  25. #include <atomic.h>
  26. #define NN_ATOMIC_SOLARIS
  27. #elif defined NN_HAVE_GCC_ATOMIC_BUILTINS
  28. #define NN_ATOMIC_GCC_BUILTINS
  29. #else
  30. #include "mutex.h"
  31. #define NN_ATOMIC_MUTEX
  32. #endif
  33. #include <stdint.h>
  34. struct nn_atomic {
  35. #if defined NN_ATOMIC_MUTEX
  36. struct nn_mutex sync;
  37. #endif
  38. volatile uint32_t n;
  39. };
  40. /* Initialise the object. Set it to value 'n'. */
  41. void nn_atomic_init (struct nn_atomic *self, uint32_t n);
  42. /* Destroy the object. */
  43. void nn_atomic_term (struct nn_atomic *self);
  44. /* Atomically add n to the object, return old value of the object. */
  45. uint32_t nn_atomic_inc (struct nn_atomic *self, uint32_t n);
  46. /* Atomically subtract n from the object, return old value of the object. */
  47. uint32_t nn_atomic_dec (struct nn_atomic *self, uint32_t n);
  48. #endif