pspinlock.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* POSIX spinlock implementation. x86-64 version.
  2. Copyright (C) 2001 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
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the 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; if not, see
  14. <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. But the kernel is byte instructions for the memory access. This is
  20. faster but unusable here. The problem is that only 128
  21. threads/processes could use the spinlock at the same time. If (by
  22. a design error in the program) a thread/process would hold the
  23. spinlock for a time long enough to accumulate 128 waiting
  24. processes, the next one will find a positive value in the spinlock
  25. and assume it is unlocked. We cannot accept that. */
  26. int
  27. __pthread_spin_lock (pthread_spinlock_t *lock)
  28. {
  29. __asm__ __volatile__
  30. ("\n"
  31. "1:\n\t"
  32. "lock; decl %0\n\t"
  33. "js 2f\n\t"
  34. ".section .text.spinlock,\"ax\"\n"
  35. "2:\n\t"
  36. "cmpl $0,%0\n\t"
  37. "rep; nop\n\t"
  38. "jle 2b\n\t"
  39. "jmp 1b\n\t"
  40. ".previous"
  41. : "=m" (*lock));
  42. return 0;
  43. }
  44. weak_alias (__pthread_spin_lock, pthread_spin_lock)
  45. int
  46. __pthread_spin_trylock (pthread_spinlock_t *lock)
  47. {
  48. int oldval;
  49. __asm__ __volatile__
  50. ("xchgl %0,%1"
  51. : "=r" (oldval), "=m" (*lock)
  52. : "0" (0));
  53. return oldval > 0 ? 0 : EBUSY;
  54. }
  55. weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
  56. int
  57. __pthread_spin_unlock (pthread_spinlock_t *lock)
  58. {
  59. __asm__ __volatile__
  60. ("movl $1,%0"
  61. : "=m" (*lock));
  62. return 0;
  63. }
  64. weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
  65. int
  66. __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
  67. {
  68. /* We can ignore the `pshared' parameter. Since we are busy-waiting
  69. all processes which can access the memory location `lock' points
  70. to can use the spinlock. */
  71. *lock = 1;
  72. return 0;
  73. }
  74. weak_alias (__pthread_spin_init, pthread_spin_init)
  75. int
  76. __pthread_spin_destroy (pthread_spinlock_t *lock)
  77. {
  78. /* Nothing to do. */
  79. return 0;
  80. }
  81. weak_alias (__pthread_spin_destroy, pthread_spin_destroy)