jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JackAtomic_os.h 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright (C) 2004-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #ifndef __JackAtomic_linux__
  16. #define __JackAtomic_linux__
  17. #include "JackTypes.h"
  18. #ifdef __PPC__
  19. static inline int CAS(register UInt32 value, register UInt32 newvalue, register volatile void* addr)
  20. {
  21. register int result;
  22. register UInt32 tmp;
  23. asm volatile (
  24. "# CAS \n"
  25. " lwarx %4, 0, %1 \n" // creates a reservation on addr
  26. " cmpw %4, %2 \n" // test value at addr
  27. " bne- 1f \n"
  28. " sync \n" // synchronize instructions
  29. " stwcx. %3, 0, %1 \n" // if the reservation is not altered
  30. // stores the new value at addr
  31. " bne- 1f \n"
  32. " li %0, 1 \n"
  33. " b 2f \n"
  34. "1: \n"
  35. " li %0, 0 \n"
  36. "2: \n"
  37. : "=r" (result)
  38. : "r" (addr), "r" (value), "r" (newvalue), "r" (tmp)
  39. );
  40. return result;
  41. }
  42. #endif
  43. #if defined(__i386__) || defined(__x86_64__)
  44. #define LOCK "lock ; "
  45. static inline char CAS(volatile UInt32 value, UInt32 newvalue, volatile void* addr)
  46. {
  47. register char ret;
  48. __asm__ __volatile__ (
  49. "# CAS \n\t"
  50. LOCK "cmpxchg %2, (%1) \n\t"
  51. "sete %0 \n\t"
  52. : "=a" (ret)
  53. : "c" (addr), "d" (newvalue), "a" (value)
  54. );
  55. return ret;
  56. }
  57. #endif
  58. #if !defined(__i386__) && !defined(__x86_64__) && !defined(__PPC__)
  59. static inline char CAS(volatile UInt32 value, UInt32 newvalue, volatile void* addr)
  60. {
  61. return __sync_bool_compare_and_swap ((UInt32*)addr, value, newvalue);
  62. }
  63. #endif
  64. #endif