spinlock.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2016 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of SCSI2SD.
  4. //
  5. // SCSI2SD is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // SCSI2SD is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  17. #include "spinlock.h"
  18. int s2s_spin_trylock(s2s_lock_t* lock)
  19. {
  20. if (__LDREXW(lock) == 0)
  21. {
  22. // Try to set lock
  23. int status = __STREXW(1, lock);
  24. if (status == 0)
  25. {
  26. // got lock
  27. // Do not start any other memory access
  28. // until memory barrier is completed
  29. __DMB();
  30. return 1;
  31. }
  32. }
  33. return 0;
  34. }
  35. void s2s_spin_lock(s2s_lock_t* lock)
  36. {
  37. int status = 0;
  38. do
  39. {
  40. // Wait until lock is free
  41. while (__LDREXW(lock) != 0);
  42. // Try to set lock
  43. status = __STREXW(1, lock);
  44. } while (status!=0); //retry until lock successfully
  45. // Do not start any other memory access
  46. // until memory barrier is completed
  47. __DMB();
  48. }
  49. void s2s_spin_unlock(s2s_lock_t* lock)
  50. {
  51. // Ensure memory operations completed before releasing
  52. __DMB();
  53. *lock = 0;
  54. }