pspinlock.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include "internals.h"
  19. /* This implementation is similar to the one used in the Linux kernel.
  20. But the kernel is byte instructions for the memory access. This is
  21. faster but unusable here. The problem is that only 128
  22. threads/processes could use the spinlock at the same time. If (by
  23. a design error in the program) a thread/process would hold the
  24. spinlock for a time long enough to accumulate 128 waiting
  25. processes, the next one will find a positive value in the spinlock
  26. and assume it is unlocked. We cannot accept that. */
  27. int
  28. __pthread_spin_lock (pthread_spinlock_t *lock)
  29. {
  30. unsigned int tmp;
  31. asm volatile
  32. ("1: ldl_l %0,%1\n"
  33. " blbs %0,2f\n"
  34. " or %0,1,%0\n"
  35. " stl_c %0,%1\n"
  36. " beq %0,2f\n"
  37. " mb\n"
  38. ".subsection 2\n"
  39. "2: ldl %0,%1\n"
  40. " blbs %0,2b\n"
  41. " br 1b\n"
  42. ".previous"
  43. : "=r" (tmp), "=m" (lock)
  44. : "m" (lock));
  45. return 0;
  46. }
  47. weak_alias (__pthread_spin_lock, pthread_spin_lock)
  48. int
  49. __pthread_spin_trylock (pthread_spinlock_t *lock)
  50. {
  51. unsigned long int oldval;
  52. unsigned long int temp;
  53. asm volatile
  54. ("1: ldl_l %0,%1\n"
  55. " and %0,%3,%2\n"
  56. " bne %2,2f\n"
  57. " xor %0,%3,%0\n"
  58. " stl_c %0,%1\n"
  59. " beq %0,3f\n"
  60. " mb\n"
  61. "2:\n"
  62. ".subsection 2\n"
  63. "3: br 1b\n"
  64. ".previous"
  65. : "=&r" (temp), "=m" (*lock), "=&r" (oldval)
  66. : "Ir" (1UL), "m" (*lock));
  67. return oldval == 0 ? 0 : EBUSY;
  68. }
  69. weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
  70. int
  71. __pthread_spin_unlock (pthread_spinlock_t *lock)
  72. {
  73. asm volatile ("mb");
  74. return *lock = 0;
  75. }
  76. weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
  77. int
  78. __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
  79. {
  80. /* We can ignore the `pshared' parameter. Since we are busy-waiting
  81. all processes which can access the memory location `lock' points
  82. to can use the spinlock. */
  83. *lock = 0;
  84. return 0;
  85. }
  86. weak_alias (__pthread_spin_init, pthread_spin_init)
  87. int
  88. __pthread_spin_destroy (pthread_spinlock_t *lock)
  89. {
  90. /* Nothing to do. */
  91. return 0;
  92. }
  93. weak_alias (__pthread_spin_destroy, pthread_spin_destroy)