pspinlock.c 2.5 KB

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