cpu_up.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-04-19 Shell Fixup UP irq spinlock
  9. * 2024-05-22 Shell Add UP cpu object and
  10. * maintain the rt_current_thread inside it
  11. */
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. static struct rt_cpu _cpu;
  15. /**
  16. * @brief Initialize a static spinlock object.
  17. *
  18. * @param lock is a pointer to the spinlock to initialize.
  19. */
  20. void rt_spin_lock_init(struct rt_spinlock *lock)
  21. {
  22. RT_UNUSED(lock);
  23. }
  24. /**
  25. * @brief This function will lock the spinlock, will lock the thread scheduler.
  26. *
  27. * @note If the spinlock is locked, the current CPU will keep polling the spinlock state
  28. * until the spinlock is unlocked.
  29. *
  30. * @param lock is a pointer to the spinlock.
  31. */
  32. void rt_spin_lock(struct rt_spinlock *lock)
  33. {
  34. rt_enter_critical();
  35. RT_SPIN_LOCK_DEBUG(lock);
  36. }
  37. /**
  38. * @brief This function will unlock the spinlock, will unlock the thread scheduler.
  39. * If the scheduling function is called before unlocking, it will be scheduled in this function.
  40. *
  41. * @param lock is a pointer to the spinlock.
  42. */
  43. void rt_spin_unlock(struct rt_spinlock *lock)
  44. {
  45. rt_base_t critical_level;
  46. RT_SPIN_UNLOCK_DEBUG(lock, critical_level);
  47. rt_exit_critical_safe(critical_level);
  48. }
  49. /**
  50. * @brief This function will disable the local interrupt and then lock the spinlock, will lock the thread scheduler.
  51. *
  52. * @note If the spinlock is locked, the current CPU will keep polling the spinlock state
  53. * until the spinlock is unlocked.
  54. *
  55. * @param lock is a pointer to the spinlock.
  56. *
  57. * @return Return current cpu interrupt status.
  58. */
  59. rt_base_t rt_spin_lock_irqsave(struct rt_spinlock *lock)
  60. {
  61. rt_base_t level;
  62. RT_UNUSED(lock);
  63. level = rt_hw_interrupt_disable();
  64. rt_enter_critical();
  65. RT_SPIN_LOCK_DEBUG(lock);
  66. return level;
  67. }
  68. /**
  69. * @brief This function will unlock the spinlock and then restore current cpu interrupt status, will unlock the thread scheduler.
  70. * If the scheduling function is called before unlocking, it will be scheduled in this function.
  71. *
  72. * @param lock is a pointer to the spinlock.
  73. *
  74. * @param level is interrupt status returned by rt_spin_lock_irqsave().
  75. */
  76. void rt_spin_unlock_irqrestore(struct rt_spinlock *lock, rt_base_t level)
  77. {
  78. rt_base_t critical_level;
  79. RT_SPIN_UNLOCK_DEBUG(lock, critical_level);
  80. rt_exit_critical_safe(critical_level);
  81. rt_hw_interrupt_enable(level);
  82. }
  83. /**
  84. * @brief This fucntion will return current cpu object.
  85. *
  86. * @return Return a pointer to the current cpu object.
  87. */
  88. struct rt_cpu *rt_cpu_self(void)
  89. {
  90. return &_cpu;
  91. }
  92. /**
  93. * @brief This fucntion will return the cpu object corresponding to index.
  94. *
  95. * @param index is the index of target cpu object.
  96. *
  97. * @return Return a pointer to the cpu object corresponding to index.
  98. */
  99. struct rt_cpu *rt_cpu_index(int index)
  100. {
  101. return index == 0 ? &_cpu : RT_NULL;
  102. }