pspinlock.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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
  15. not, see <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include "internals.h"
  19. #include <ia64intrin.h>
  20. /* This implementation is inspired by the implementation used in the
  21. Linux kernel. */
  22. int
  23. __pthread_spin_lock (pthread_spinlock_t *lock)
  24. {
  25. int *p = (int *) lock;
  26. while (__builtin_expect (__sync_val_compare_and_swap (p, 0, 1), 0))
  27. {
  28. /* Spin without using the atomic instruction. */
  29. do
  30. __asm__ __volatile__ ("" : : : "memory");
  31. while (*p);
  32. }
  33. return 0;
  34. }
  35. weak_alias (__pthread_spin_lock, pthread_spin_lock)
  36. int
  37. __pthread_spin_trylock (pthread_spinlock_t *lock)
  38. {
  39. return __sync_val_compare_and_swap ((int *) lock, 0, 1) == 0 ? 0 : EBUSY;
  40. }
  41. weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
  42. int
  43. __pthread_spin_unlock (pthread_spinlock_t *lock)
  44. {
  45. return *lock = 0;
  46. }
  47. weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
  48. int
  49. __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
  50. {
  51. /* We can ignore the `pshared' parameter. Since we are busy-waiting
  52. all processes which can access the memory location `lock' points
  53. to can use the spinlock. */
  54. return *lock = 0;
  55. }
  56. weak_alias (__pthread_spin_init, pthread_spin_init)
  57. int
  58. __pthread_spin_destroy (pthread_spinlock_t *lock)
  59. {
  60. /* Nothing to do. */
  61. return 0;
  62. }
  63. weak_alias (__pthread_spin_destroy, pthread_spin_destroy)