fastlock.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * fast arhitecture specific locking
  3. *
  4. * $Id$
  5. *
  6. *
  7. *
  8. * Copyright (C) 2001-2003 Fhg Fokus
  9. *
  10. * This file is part of ser, a free SIP server.
  11. *
  12. * ser is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version
  16. *
  17. * For a license to use the ser software under conditions
  18. * other than those described here, or to purchase support for this
  19. * software, please contact iptel.org by e-mail at the following addresses:
  20. * [email protected]
  21. *
  22. * ser is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. */
  31. /*
  32. *
  33. *
  34. * 2003-01-16 added PPC locking code contributed by Dinos Dorkofikis
  35. * <[email protected]>
  36. *
  37. */
  38. #ifndef fastlock_h
  39. #define fastlock_h
  40. #ifdef HAVE_SCHED_YIELD
  41. #include <sched.h>
  42. #else
  43. #include <unistd.h>
  44. /* fake sched_yield */
  45. #define sched_yield() sleep(0)
  46. #endif
  47. typedef volatile int fl_lock_t;
  48. #define init_lock( l ) (l)=0
  49. /*test and set lock, ret 1 if lock held by someone else, 0 otherwise*/
  50. inline static int tsl(fl_lock_t* lock)
  51. {
  52. int val;
  53. #ifdef __CPU_i386
  54. #ifdef NOSMP
  55. val=0;
  56. asm volatile(
  57. " btsl $0, %1 \n\t"
  58. " adcl $0, %0 \n\t"
  59. : "=q" (val), "=m" (*lock) : "0"(val) : "memory", "cc" /* "cc" */
  60. );
  61. #else
  62. val=1;
  63. asm volatile(
  64. " xchg %b1, %0" : "=q" (val), "=m" (*lock) : "0" (val) : "memory"
  65. );
  66. #endif /*NOSMP*/
  67. #elif defined __CPU_sparc64
  68. asm volatile(
  69. "ldstub [%1], %0 \n\t"
  70. #ifndef NOSMP
  71. "membar #StoreStore | #StoreLoad \n\t"
  72. #endif
  73. : "=r"(val) : "r"(lock):"memory"
  74. );
  75. #elif defined __CPU_arm
  76. asm volatile(
  77. "# here \n\t"
  78. "swpb %0, %1, [%2] \n\t"
  79. : "=r" (val)
  80. : "r"(1), "r" (lock) : "memory"
  81. );
  82. #elif defined __CPU_ppc
  83. asm volatile(
  84. "1: lwarx %0, 0, %2\n\t"
  85. " cmpwi %0, 0\n\t"
  86. " bne 0f\n\t"
  87. " stwcx. %1, 0, %2\n\t"
  88. " bne- 1b\n\t"
  89. "0:\n\t"
  90. : "=r" (val)
  91. : "r"(1), "b" (lock) :
  92. "memory", "cc"
  93. );
  94. #else
  95. #error "unknown arhitecture"
  96. #endif
  97. return val;
  98. }
  99. inline static void get_lock(fl_lock_t* lock)
  100. {
  101. #ifdef ADAPTIVE_WAIT
  102. int i=ADAPTIVE_WAIT_LOOPS;
  103. #endif
  104. while(tsl(lock)){
  105. #ifdef BUSY_WAIT
  106. #elif defined ADAPTIVE_WAIT
  107. if (i>0) i--;
  108. else sched_yield();
  109. #else
  110. sched_yield();
  111. #endif
  112. }
  113. }
  114. inline static void release_lock(fl_lock_t* lock)
  115. {
  116. #ifdef __CPU_i386
  117. char val;
  118. val=0;
  119. asm volatile(
  120. " movb $0, (%0)" : /*no output*/ : "r"(lock): "memory"
  121. /*" xchg %b0, %1" : "=q" (val), "=m" (*lock) : "0" (val) : "memory"*/
  122. );
  123. #elif defined __CPU_sparc64
  124. asm volatile(
  125. #ifndef NOSMP
  126. "membar #LoadStore | #StoreStore \n\t" /*is this really needed?*/
  127. #endif
  128. "stb %%g0, [%0] \n\t"
  129. : /*no output*/
  130. : "r" (lock)
  131. : "memory"
  132. );
  133. #elif defined __CPU_arm
  134. asm volatile(
  135. " str %0, [%1] \n\r"
  136. : /*no outputs*/
  137. : "r"(0), "r"(lock)
  138. : "memory"
  139. );
  140. #elif defined __CPU_ppc
  141. asm volatile(
  142. "sync\n\t"
  143. "stw %0, 0(%1)\n\t"
  144. : /* no output */
  145. : "r"(0), "b" (lock)
  146. : "memory"
  147. );
  148. *lock = 0;
  149. #else
  150. #error "unknown arhitecture"
  151. #endif
  152. }
  153. #endif