atomic.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2013, The Regents of the University of California (Regents).
  3. * All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. Neither the name of the Regents nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
  17. * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
  18. * OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
  19. * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. *
  21. * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
  24. * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
  25. * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  26. */
  27. #define mb() __sync_synchronize()
  28. #define atomic_set(ptr, val) (*(volatile typeof(*(ptr)) *)(ptr) = val)
  29. #define atomic_read(ptr) (*(volatile __typeof__(*(ptr)) *)(ptr))
  30. #if 0
  31. # define atomic_add(ptr, inc) ({ \
  32. long flags = disable_irqsave(); \
  33. typeof(ptr) res = *(volatile typeof(ptr))(ptr); \
  34. *(volatile typeof(ptr))(ptr) = res + (inc); \
  35. enable_irqrestore(flags); \
  36. res; })
  37. # define atomic_swap(ptr, swp) ({ \
  38. long flags = disable_irqsave(); \
  39. typeof(*ptr) res = *(volatile typeof(ptr))(ptr); \
  40. *(volatile typeof(ptr))(ptr) = (swp); \
  41. enable_irqrestore(flags); \
  42. res; })
  43. # define atomic_cas(ptr, cmp, swp) ({ \
  44. long flags = disable_irqsave(); \
  45. typeof(ptr) res = *(volatile typeof(ptr))(ptr); \
  46. if (res == (cmp)) *(volatile typeof(ptr))(ptr) = (swp); \
  47. enable_irqrestore(flags); \
  48. res; })
  49. #endif