pspinlock.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* POSIX spinlock implementation. Alpha version.
  2. Copyright (C) 2000 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If
  14. not, see <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <pthread.h>
  17. #include "internals.h"
  18. /* This implementation is similar to the one used in the Linux kernel.
  19. But the kernel is byte instructions for the memory access. This is
  20. faster but unusable here. The problem is that only 128
  21. threads/processes could use the spinlock at the same time. If (by
  22. a design error in the program) a thread/process would hold the
  23. spinlock for a time long enough to accumulate 128 waiting
  24. processes, the next one will find a positive value in the spinlock
  25. and assume it is unlocked. We cannot accept that. */
  26. int
  27. __pthread_spin_lock (pthread_spinlock_t *lock)
  28. {
  29. unsigned int tmp;
  30. __asm__ __volatile__
  31. ("1: ldl_l %0,%1\n"
  32. " blbs %0,2f\n"
  33. " or %0,1,%0\n"
  34. " stl_c %0,%1\n"
  35. " beq %0,2f\n"
  36. " mb\n"
  37. ".subsection 2\n"
  38. "2: ldl %0,%1\n"
  39. " blbs %0,2b\n"
  40. " br 1b\n"
  41. ".previous"
  42. : "=r" (tmp), "=m" (lock)
  43. : "m" (lock));
  44. return 0;
  45. }
  46. weak_alias (__pthread_spin_lock, pthread_spin_lock)
  47. int
  48. __pthread_spin_trylock (pthread_spinlock_t *lock)
  49. {
  50. unsigned long int oldval;
  51. unsigned long int temp;
  52. __asm__ __volatile__
  53. ("1: ldl_l %0,%1\n"
  54. " and %0,%3,%2\n"
  55. " bne %2,2f\n"
  56. " xor %0,%3,%0\n"
  57. " stl_c %0,%1\n"
  58. " beq %0,3f\n"
  59. " mb\n"
  60. "2:\n"
  61. ".subsection 2\n"
  62. "3: br 1b\n"
  63. ".previous"
  64. : "=&r" (temp), "=m" (*lock), "=&r" (oldval)
  65. : "Ir" (1UL), "m" (*lock));
  66. return oldval == 0 ? 0 : EBUSY;
  67. }
  68. weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
  69. int
  70. __pthread_spin_unlock (pthread_spinlock_t *lock)
  71. {
  72. __asm__ __volatile__ ("mb");
  73. return *lock = 0;
  74. }
  75. weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
  76. int
  77. __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
  78. {
  79. /* We can ignore the `pshared' parameter. Since we are busy-waiting
  80. all processes which can access the memory location `lock' points
  81. to can use the spinlock. */
  82. *lock = 0;
  83. return 0;
  84. }
  85. weak_alias (__pthread_spin_init, pthread_spin_init)
  86. int
  87. __pthread_spin_destroy (pthread_spinlock_t *lock)
  88. {
  89. /* Nothing to do. */
  90. return 0;
  91. }
  92. weak_alias (__pthread_spin_destroy, pthread_spin_destroy)