pspinlock.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* POSIX spinlock implementation. SPARC32 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. int
  20. __pthread_spin_lock (pthread_spinlock_t *lock)
  21. {
  22. __asm__ __volatile__
  23. ("1: ldstub [%0], %%g2\n"
  24. " orcc %%g2, 0x0, %%g0\n"
  25. " bne,a 2f\n"
  26. " ldub [%0], %%g2\n"
  27. ".subsection 2\n"
  28. "2: orcc %%g2, 0x0, %%g0\n"
  29. " bne,a 2b\n"
  30. " ldub [%0], %%g2\n"
  31. " b,a 1b\n"
  32. ".previous"
  33. : /* no outputs */
  34. : "r" (lock)
  35. : "g2", "memory", "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 result;
  43. __asm__ __volatile__
  44. ("ldstub [%1], %0"
  45. : "=r" (result)
  46. : "r" (lock)
  47. : "memory");
  48. return result == 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. *lock = 0;
  55. return 0;
  56. }
  57. weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
  58. int
  59. __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
  60. {
  61. /* We can ignore the `pshared' parameter. Since we are busy-waiting
  62. all processes which can access the memory location `lock' points
  63. to can use the spinlock. */
  64. *lock = 0;
  65. return 0;
  66. }
  67. weak_alias (__pthread_spin_init, pthread_spin_init)
  68. int
  69. __pthread_spin_destroy (pthread_spinlock_t *lock)
  70. {
  71. /* Nothing to do. */
  72. return 0;
  73. }
  74. weak_alias (__pthread_spin_destroy, pthread_spin_destroy)