1
0

pthread_spin.c 1020 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-10-26 Bernard the first version
  9. */
  10. #include <pthread.h>
  11. int pthread_spin_init (pthread_spinlock_t *lock, int pshared)
  12. {
  13. if (!lock)
  14. return EINVAL;
  15. lock->lock = 0;
  16. return 0;
  17. }
  18. int pthread_spin_destroy (pthread_spinlock_t *lock)
  19. {
  20. if (!lock)
  21. return EINVAL;
  22. return 0;
  23. }
  24. int pthread_spin_lock (pthread_spinlock_t *lock)
  25. {
  26. if (!lock)
  27. return EINVAL;
  28. while (!(lock->lock))
  29. {
  30. lock->lock = 1;
  31. }
  32. return 0;
  33. }
  34. int pthread_spin_trylock (pthread_spinlock_t *lock)
  35. {
  36. if (!lock)
  37. return EINVAL;
  38. if (!(lock->lock))
  39. {
  40. lock->lock = 1;
  41. return 0;
  42. }
  43. return EBUSY;
  44. }
  45. int pthread_spin_unlock (pthread_spinlock_t *lock)
  46. {
  47. if (!lock)
  48. return EINVAL;
  49. if (!(lock->lock))
  50. return EPERM;
  51. lock->lock = 0;
  52. return 0;
  53. }