pspinlock.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2016-2017 Andes Technology, Inc.
  3. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  4. */
  5. /* POSIX spinlock implementation.
  6. Copyright (C) 2000 Free Software Foundation, Inc.
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public License as
  9. published by the Free Software Foundation; either version 2.1 of the
  10. License, or (at your option) any later version.
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the GNU C Library; see the file COPYING.LIB. If not,
  17. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. Boston, MA 02111-1307, USA. */
  19. #include <errno.h>
  20. #include <pthread.h>
  21. #include "internals.h"
  22. int
  23. __pthread_spin_lock (pthread_spinlock_t *lock)
  24. {
  25. unsigned int val ;
  26. unsigned int temp ;
  27. unsigned int offset = 0 ;
  28. __asm__ __volatile__ (
  29. "1:\n\t"
  30. "llw %0, [%1 + %2 << 0]\n\t"
  31. "bnez %0, 1b\n\t"
  32. "movi %3, #0x1\n\t"
  33. "scw %3, [%1 + %2 << 0]\n\t"
  34. "beqz %3, 1b\n\t"
  35. : "=&r" (val)
  36. : "r" (lock), "r" (offset), "r" (temp)
  37. : "memory" ) ;
  38. return 0 ;
  39. }
  40. weak_alias (__pthread_spin_lock, pthread_spin_lock)
  41. int
  42. __pthread_spin_trylock (pthread_spinlock_t *lock)
  43. {
  44. unsigned int val ;
  45. unsigned int temp ;
  46. unsigned int offset = 0 ;
  47. __asm__ __volatile__ (
  48. "llw %0, [%1 + %2 << 0]\n\t"
  49. "bnez %0, 1f\n\t"
  50. "movi %3, #0x1\n\t"
  51. "scw %3, [%1 + %2 << 0]\n\t"
  52. "beqz %3, 1f\n\t"
  53. "movi %0, #0x0\n\t"
  54. "b 2f\n\t"
  55. "1:\n\t"
  56. "movi %0, #16\n\t"
  57. "2:\n\t"
  58. : "=&r" (val)
  59. : "r" (lock), "r" (offset), "r" (temp)
  60. : "memory" ) ;
  61. return val;
  62. }
  63. weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
  64. int
  65. __pthread_spin_unlock (pthread_spinlock_t *lock)
  66. {
  67. return *lock = 0;
  68. }
  69. weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
  70. int
  71. __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
  72. {
  73. /* We can ignore the `pshared' parameter. Since we are busy-waiting
  74. all processes which can access the memory location `lock' points
  75. to can use the spinlock. */
  76. return *lock = 0;
  77. }
  78. weak_alias (__pthread_spin_init, pthread_spin_init)
  79. int
  80. __pthread_spin_destroy (pthread_spinlock_t *lock)
  81. {
  82. /* Nothing to do. */
  83. return 0;
  84. }
  85. weak_alias (__pthread_spin_destroy, pthread_spin_destroy)