pspinlock.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* POSIX spinlock implementation. SPARC64 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], %%g5\n"
  24. " brnz,pn %%g5, 2f\n"
  25. " membar #StoreLoad | #StoreStore\n"
  26. ".subsection 2\n"
  27. "2: ldub [%0], %%g5\n"
  28. " brnz,pt %%g5, 2b\n"
  29. " membar #LoadLoad\n"
  30. " b,a,pt %%xcc, 1b\n"
  31. ".previous"
  32. : /* no outputs */
  33. : "r" (lock)
  34. : "g5", "memory");
  35. return 0;
  36. }
  37. weak_alias (__pthread_spin_lock, pthread_spin_lock)
  38. int
  39. __pthread_spin_trylock (pthread_spinlock_t *lock)
  40. {
  41. int result;
  42. __asm__ __volatile__
  43. ("ldstub [%1], %0\n"
  44. "membar #StoreLoad | #StoreStore"
  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. __asm__ __volatile__
  55. ("membar #StoreStore | #LoadStore\n"
  56. "stb %%g0, [%0]"
  57. :
  58. : "r" (lock)
  59. : "memory");
  60. return 0;
  61. }
  62. weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
  63. int
  64. __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
  65. {
  66. /* We can ignore the `pshared' parameter. Since we are busy-waiting
  67. all processes which can access the memory location `lock' points
  68. to can use the spinlock. */
  69. *lock = 0;
  70. return 0;
  71. }
  72. weak_alias (__pthread_spin_init, pthread_spin_init)
  73. int
  74. __pthread_spin_destroy (pthread_spinlock_t *lock)
  75. {
  76. /* Nothing to do. */
  77. return 0;
  78. }
  79. weak_alias (__pthread_spin_destroy, pthread_spin_destroy)