pthread_spin_lock.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* pthread_spin_lock -- lock a spin lock. Generic version.
  2. Copyright (C) 2012-2016 Free Software Foundation, Inc.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <atomic.h>
  15. #include "pthreadP.h"
  16. /* A machine-specific version can define SPIN_LOCK_READS_BETWEEN_CMPXCHG
  17. to the number of plain reads that it's optimal to spin on between uses
  18. of atomic_compare_and_exchange_val_acq. If spinning forever is optimal
  19. then use -1. If no plain reads here would ever be optimal, use 0. */
  20. #define SPIN_LOCK_READS_BETWEEN_CMPXCHG 1000
  21. int
  22. pthread_spin_lock (pthread_spinlock_t *lock)
  23. {
  24. /* atomic_exchange usually takes less instructions than
  25. atomic_compare_and_exchange. On the other hand,
  26. atomic_compare_and_exchange potentially generates less bus traffic
  27. when the lock is locked.
  28. We assume that the first try mostly will be successful, and we use
  29. atomic_exchange. For the subsequent tries we use
  30. atomic_compare_and_exchange. */
  31. if (atomic_exchange_acq (lock, 1) == 0)
  32. return 0;
  33. do
  34. {
  35. /* The lock is contended and we need to wait. Going straight back
  36. to cmpxchg is not a good idea on many targets as that will force
  37. expensive memory synchronizations among processors and penalize other
  38. running threads.
  39. On the other hand, we do want to update memory state on the local core
  40. once in a while to avoid spinning indefinitely until some event that
  41. will happen to update local memory as a side-effect. */
  42. if (SPIN_LOCK_READS_BETWEEN_CMPXCHG >= 0)
  43. {
  44. int wait = SPIN_LOCK_READS_BETWEEN_CMPXCHG;
  45. while (*lock != 0 && wait > 0)
  46. --wait;
  47. }
  48. else
  49. {
  50. while (*lock != 0)
  51. ;
  52. }
  53. }
  54. while (atomic_compare_and_exchange_val_acq (lock, 1, 0) != 0);
  55. return 0;
  56. }