1
0

pthread_spin.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * File : pthread_spin.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program 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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2010-10-26 Bernard the first version
  23. */
  24. #include <pthread.h>
  25. int pthread_spin_init (pthread_spinlock_t *lock, int pshared)
  26. {
  27. if (!lock)
  28. return EINVAL;
  29. lock->lock = 0;
  30. return 0;
  31. }
  32. int pthread_spin_destroy (pthread_spinlock_t *lock)
  33. {
  34. if (!lock)
  35. return EINVAL;
  36. return 0;
  37. }
  38. int pthread_spin_lock (pthread_spinlock_t *lock)
  39. {
  40. if (!lock)
  41. return EINVAL;
  42. while (!(lock->lock))
  43. {
  44. lock->lock = 1;
  45. }
  46. return 0;
  47. }
  48. int pthread_spin_trylock (pthread_spinlock_t *lock)
  49. {
  50. if (!lock)
  51. return EINVAL;
  52. if (!(lock->lock))
  53. {
  54. lock->lock = 1;
  55. return 0;
  56. }
  57. return EBUSY;
  58. }
  59. int pthread_spin_unlock (pthread_spinlock_t *lock)
  60. {
  61. if (!lock)
  62. return EINVAL;
  63. if (!(lock->lock))
  64. return EPERM;
  65. lock->lock = 0;
  66. return 0;
  67. }