pspinlock.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* POSIX spinlock implementation. ia64 version.
  2. Copyright (C) 2000, 2003 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jes Sorensen <jes@linuxcare.com>
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License as
  7. published by the Free Software Foundation; either version 2.1 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. #include <errno.h>
  18. #include <pthread.h>
  19. #include "internals.h"
  20. #include <ia64intrin.h>
  21. /* This implementation is inspired by the implementation used in the
  22. Linux kernel. */
  23. int
  24. __pthread_spin_lock (pthread_spinlock_t *lock)
  25. {
  26. int *p = (int *) lock;
  27. while (__builtin_expect (__sync_val_compare_and_swap (p, 0, 1), 0))
  28. {
  29. /* Spin without using the atomic instruction. */
  30. do
  31. __asm __volatile ("" : : : "memory");
  32. while (*p);
  33. }
  34. return 0;
  35. }
  36. weak_alias (__pthread_spin_lock, pthread_spin_lock)
  37. int
  38. __pthread_spin_trylock (pthread_spinlock_t *lock)
  39. {
  40. return __sync_val_compare_and_swap ((int *) lock, 0, 1) == 0 ? 0 : EBUSY;
  41. }
  42. weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
  43. int
  44. __pthread_spin_unlock (pthread_spinlock_t *lock)
  45. {
  46. return *lock = 0;
  47. }
  48. weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
  49. int
  50. __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
  51. {
  52. /* We can ignore the `pshared' parameter. Since we are busy-waiting
  53. all processes which can access the memory location `lock' points
  54. to can use the spinlock. */
  55. return *lock = 0;
  56. }
  57. weak_alias (__pthread_spin_init, pthread_spin_init)
  58. int
  59. __pthread_spin_destroy (pthread_spinlock_t *lock)
  60. {
  61. /* Nothing to do. */
  62. return 0;
  63. }
  64. weak_alias (__pthread_spin_destroy, pthread_spin_destroy)