pspinlock.c 2.7 KB

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