/src/rt/sync/sync.h

http://github.com/jruderman/rust · C Header · 44 lines · 35 code · 8 blank · 1 comment · 0 complexity · 6da6e4ba3b2d83ea1bf3218743b76b8e MD5 · raw file

  1. // -*- c++ -*-
  2. #ifndef SYNC_H
  3. #define SYNC_H
  4. class sync {
  5. public:
  6. template <class T>
  7. static bool compare_and_swap(T *address,
  8. T oldValue, T newValue) {
  9. return __sync_bool_compare_and_swap(address, oldValue, newValue);
  10. }
  11. template <class T>
  12. static T increment(T *address) {
  13. return __sync_add_and_fetch(address, 1);
  14. }
  15. template <class T>
  16. static T decrement(T *address) {
  17. return __sync_sub_and_fetch(address, 1);
  18. }
  19. template <class T>
  20. static T increment(T &address) {
  21. return __sync_add_and_fetch(&address, 1);
  22. }
  23. template <class T>
  24. static T decrement(T &address) {
  25. return __sync_sub_and_fetch(&address, 1);
  26. }
  27. template <class T>
  28. static T read(T *address) {
  29. return __sync_add_and_fetch(address, 0);
  30. }
  31. template <class T>
  32. static T read(T &address) {
  33. return __sync_add_and_fetch(&address, 0);
  34. }
  35. };
  36. #endif /* SYNC_H */