pthread_spin_lock.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* pthread_spin_lock -- lock a spin lock. Generic version.
  2. Copyright (C) 2012-2019 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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <atomic.h>
  16. #include "pthreadP.h"
  17. int
  18. pthread_spin_lock (pthread_spinlock_t *lock)
  19. {
  20. int val = 0;
  21. /* We assume that the first try mostly will be successful, thus we use
  22. atomic_exchange if it is not implemented by a CAS loop (we also assume
  23. that atomic_exchange can be faster if it succeeds, see
  24. ATOMIC_EXCHANGE_USES_CAS). Otherwise, we use a weak CAS and not an
  25. exchange so we bail out after the first failed attempt to change the
  26. state. For the subsequent attempts we use atomic_compare_and_exchange
  27. after we observe that the lock is not acquired.
  28. See also comment in pthread_spin_trylock.
  29. We use acquire MO to synchronize-with the release MO store in
  30. pthread_spin_unlock, and thus ensure that prior critical sections
  31. happen-before this critical section. */
  32. #if ! ATOMIC_EXCHANGE_USES_CAS
  33. /* Try to acquire the lock with an exchange instruction as this architecture
  34. has such an instruction and we assume it is faster than a CAS.
  35. The acquisition succeeds if the lock is not in an acquired state. */
  36. if (__glibc_likely (atomic_exchange_acquire (lock, 1) == 0))
  37. return 0;
  38. #else
  39. /* Try to acquire the lock with a CAS instruction as this architecture
  40. has no exchange instruction. The acquisition succeeds if the lock is not
  41. acquired. */
  42. if (__glibc_likely (atomic_compare_exchange_weak_acquire (lock, &val, 1)))
  43. return 0;
  44. #endif
  45. do
  46. {
  47. /* The lock is contended and we need to wait. Going straight back
  48. to cmpxchg is not a good idea on many targets as that will force
  49. expensive memory synchronizations among processors and penalize other
  50. running threads.
  51. There is no technical reason for throwing in a CAS every now and then,
  52. and so far we have no evidence that it can improve performance.
  53. If that would be the case, we have to adjust other spin-waiting loops
  54. elsewhere, too!
  55. Thus we use relaxed MO reads until we observe the lock to not be
  56. acquired anymore. */
  57. do
  58. {
  59. /* TODO Back-off. */
  60. atomic_spin_nop ();
  61. val = atomic_load_relaxed (lock);
  62. }
  63. while (val != 0);
  64. /* We need acquire memory order here for the same reason as mentioned
  65. for the first try to lock the spinlock. */
  66. }
  67. while (!atomic_compare_exchange_weak_acquire (lock, &val, 1));
  68. return 0;
  69. }