pspinlock.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. #include <errno.h>
  18. #include <pthread.h>
  19. #include "internals.h"
  20. /* This implementation is similar to the one used in the Linux kernel.
  21. But the kernel is byte instructions for the memory access. This is
  22. faster but unusable here. The problem is that only 128
  23. threads/processes could use the spinlock at the same time. If (by
  24. a design error in the program) a thread/process would hold the
  25. spinlock for a time long enough to accumulate 128 waiting
  26. processes, the next one will find a positive value in the spinlock
  27. and assume it is unlocked. We cannot accept that. */
  28. int
  29. __pthread_spin_lock (pthread_spinlock_t *lock)
  30. {
  31. __asm__ __volatile__(" basr 1,0\n"
  32. "0: slr 0,0\n"
  33. " cs 0,1,%1\n"
  34. " jl 0b\n"
  35. : "=m" (*lock)
  36. : "m" (*lock) : "0", "1", "cc" );
  37. return 0;
  38. }
  39. weak_alias (__pthread_spin_lock, pthread_spin_lock)
  40. int
  41. __pthread_spin_trylock (pthread_spinlock_t *lock)
  42. {
  43. int oldval;
  44. __asm__ __volatile__(" slr %1,%1\n"
  45. " basr 1,0\n"
  46. "0: cs %1,1,%0"
  47. : "=m" (*lock), "=&d" (oldval)
  48. : "m" (*lock) : "1", "cc" );
  49. return oldval == 0 ? 0 : EBUSY;
  50. }
  51. weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
  52. int
  53. __pthread_spin_unlock (pthread_spinlock_t *lock)
  54. {
  55. __asm__ __volatile__(" xc 0(4,%0),0(%0)\n"
  56. " bcr 15,0"
  57. : : "a" (lock) : "memory" );
  58. return 0;
  59. }
  60. weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
  61. int
  62. __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
  63. {
  64. /* We can ignore the `pshared' parameter. Since we are busy-waiting
  65. all processes which can access the memory location `lock' points
  66. to can use the spinlock. */
  67. *lock = 0;
  68. return 0;
  69. }
  70. weak_alias (__pthread_spin_init, pthread_spin_init)
  71. int
  72. __pthread_spin_destroy (pthread_spinlock_t *lock)
  73. {
  74. /* Nothing to do. */
  75. return 0;
  76. }
  77. weak_alias (__pthread_spin_destroy, pthread_spin_destroy)