pspinlock.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* POSIX spinlock implementation. S/390 version.
  2. Copyright (C) 2000, 2004 Free Software Foundation, Inc.
  3. Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License as
  7. published by the Free Software Foundation; either version 2.1 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If
  15. not, see <http://www.gnu.org/licenses/>. */
  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. __asm__ __volatile__(" basr 1,0\n"
  31. "0: slr 0,0\n"
  32. " cs 0,1,%1\n"
  33. " jl 0b\n"
  34. : "=m" (*lock)
  35. : "m" (*lock) : "0", "1", "cc" );
  36. return 0;
  37. }
  38. weak_alias (__pthread_spin_lock, pthread_spin_lock)
  39. int
  40. __pthread_spin_trylock (pthread_spinlock_t *lock)
  41. {
  42. int oldval;
  43. __asm__ __volatile__(" slr %1,%1\n"
  44. " basr 1,0\n"
  45. "0: cs %1,1,%0"
  46. : "=m" (*lock), "=&d" (oldval)
  47. : "m" (*lock) : "1", "cc" );
  48. return oldval == 0 ? 0 : EBUSY;
  49. }
  50. weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
  51. int
  52. __pthread_spin_unlock (pthread_spinlock_t *lock)
  53. {
  54. __asm__ __volatile__(" xc 0(4,%0),0(%0)\n"
  55. " bcr 15,0"
  56. : : "a" (lock) : "memory" );
  57. return 0;
  58. }
  59. weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
  60. int
  61. __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
  62. {
  63. /* We can ignore the `pshared' parameter. Since we are busy-waiting
  64. all processes which can access the memory location `lock' points
  65. to can use the spinlock. */
  66. *lock = 0;
  67. return 0;
  68. }
  69. weak_alias (__pthread_spin_init, pthread_spin_init)
  70. int
  71. __pthread_spin_destroy (pthread_spinlock_t *lock)
  72. {
  73. /* Nothing to do. */
  74. return 0;
  75. }
  76. weak_alias (__pthread_spin_destroy, pthread_spin_destroy)