pspinlock.c 2.9 KB

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