[Kernel] cleanup code of spinlock in SMP Kernel

This commit is contained in:
Bernard Xiong 2019-09-28 11:56:03 +08:00
parent cb07e5fb24
commit 5cd6509296
4 changed files with 42 additions and 47 deletions

View File

@ -143,10 +143,10 @@ typedef union {
} tickets; } tickets;
} rt_hw_spinlock_t; } rt_hw_spinlock_t;
typedef struct struct rt_spinlock
{ {
rt_hw_spinlock_t lock; rt_hw_spinlock_t lock;
} rt_spinlock_t; };
void rt_hw_spin_lock_init(rt_hw_spinlock_t *lock); void rt_hw_spin_lock_init(rt_hw_spinlock_t *lock);
void rt_hw_spin_lock(rt_hw_spinlock_t *lock); void rt_hw_spin_lock(rt_hw_spinlock_t *lock);
@ -187,16 +187,8 @@ void rt_hw_secondary_cpu_idle_exec(void);
#define rt_hw_spin_lock(lock) *(lock) = rt_hw_interrupt_disable() #define rt_hw_spin_lock(lock) *(lock) = rt_hw_interrupt_disable()
#define rt_hw_spin_unlock(lock) rt_hw_interrupt_enable(*(lock)) #define rt_hw_spin_unlock(lock) rt_hw_interrupt_enable(*(lock))
typedef int rt_spinlock_t;
#endif #endif
void rt_spin_lock_init(rt_spinlock_t *lock);
void rt_spin_lock(rt_spinlock_t *lock);
void rt_spin_unlock(rt_spinlock_t *lock);
rt_base_t rt_spin_lock_irqsave(rt_spinlock_t *lock);
void rt_spin_unlock_irqrestore(rt_spinlock_t *lock, rt_base_t level);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -391,6 +391,27 @@ rt_err_t rt_mq_recv(rt_mq_t mq,
rt_err_t rt_mq_control(rt_mq_t mq, int cmd, void *arg); rt_err_t rt_mq_control(rt_mq_t mq, int cmd, void *arg);
#endif #endif
/*
* spinlock
*/
#ifdef RT_USING_SMP
struct rt_spinlock;
void rt_spin_lock_init(struct rt_spinlock *lock);
void rt_spin_lock(struct rt_spinlock *lock);
void rt_spin_unlock(struct rt_spinlock *lock);
rt_base_t rt_spin_lock_irqsave(struct rt_spinlock *lock);
void rt_spin_unlock_irqrestore(struct rt_spinlock *lock, rt_base_t level);
#else
#define rt_spin_lock_init(lock) /* nothing */
#define rt_spin_lock(lock) rt_enter_critical()
#define rt_spin_unlock(lock) rt_exit_critical()
#define rt_spin_lock_irqsave(lock) rt_hw_interrupt_disable()
#define rt_spin_unlock_irqrestore(lock, level) rt_hw_interrupt_enable(level)
#endif
/**@}*/ /**@}*/
#ifdef RT_USING_DEVICE #ifdef RT_USING_DEVICE

View File

@ -26,6 +26,9 @@ if GetDepend('RT_USING_MEMHEAP') == False:
if GetDepend('RT_USING_DEVICE') == False: if GetDepend('RT_USING_DEVICE') == False:
SrcRemove(src, ['device.c']) SrcRemove(src, ['device.c'])
if GetDepend('RT_USING_SMP') == False:
SrcRemove(src, ['cpu.c'])
group = DefineGroup('Kernel', src, depend = [''], CPPPATH = CPPPATH) group = DefineGroup('Kernel', src, depend = [''], CPPPATH = CPPPATH)
Return('group') Return('group')

View File

@ -12,9 +12,12 @@
#include <rthw.h> #include <rthw.h>
#ifdef RT_USING_SMP #ifdef RT_USING_SMP
/*********************************** static struct rt_cpu rt_cpus[RT_CPUS_NR];
rt_hw_spinlock_t _cpus_lock;
/*
* disable scheduler * disable scheduler
***********************************/ */
static void rt_preempt_disable(void) static void rt_preempt_disable(void)
{ {
register rt_base_t level; register rt_base_t level;
@ -23,7 +26,7 @@ static void rt_preempt_disable(void)
/* disable interrupt */ /* disable interrupt */
level = rt_hw_local_irq_disable(); level = rt_hw_local_irq_disable();
current_thread = rt_cpu_self()->current_thread; current_thread = rt_thread_self();
if (!current_thread) if (!current_thread)
{ {
rt_hw_local_irq_enable(level); rt_hw_local_irq_enable(level);
@ -37,9 +40,9 @@ static void rt_preempt_disable(void)
rt_hw_local_irq_enable(level); rt_hw_local_irq_enable(level);
} }
/*********************************** /*
* restore scheduler * enable scheduler
***********************************/ */
static void rt_preempt_enable(void) static void rt_preempt_enable(void)
{ {
register rt_base_t level; register rt_base_t level;
@ -48,7 +51,7 @@ static void rt_preempt_enable(void)
/* disable interrupt */ /* disable interrupt */
level = rt_hw_local_irq_disable(); level = rt_hw_local_irq_disable();
current_thread = rt_cpu_self()->current_thread; current_thread = rt_thread_self();
if (!current_thread) if (!current_thread)
{ {
rt_hw_local_irq_enable(level); rt_hw_local_irq_enable(level);
@ -62,73 +65,49 @@ static void rt_preempt_enable(void)
/* enable interrupt */ /* enable interrupt */
rt_hw_local_irq_enable(level); rt_hw_local_irq_enable(level);
} }
#endif
void rt_spin_lock_init(rt_spinlock_t *lock) void rt_spin_lock_init(struct rt_spinlock *lock)
{ {
#ifdef RT_USING_SMP
rt_hw_spin_lock_init(&lock->lock); rt_hw_spin_lock_init(&lock->lock);
#endif
} }
RTM_EXPORT(rt_spin_lock_init) RTM_EXPORT(rt_spin_lock_init)
void rt_spin_lock(rt_spinlock_t *lock) void rt_spin_lock(struct rt_spinlock *lock)
{ {
#ifdef RT_USING_SMP
rt_preempt_disable(); rt_preempt_disable();
rt_hw_spin_lock(&lock->lock); rt_hw_spin_lock(&lock->lock);
#else
rt_enter_critical();
#endif
} }
RTM_EXPORT(rt_spin_lock) RTM_EXPORT(rt_spin_lock)
void rt_spin_unlock(rt_spinlock_t *lock) void rt_spin_unlock(struct rt_spinlock *lock)
{ {
#ifdef RT_USING_SMP
rt_hw_spin_unlock(&lock->lock); rt_hw_spin_unlock(&lock->lock);
rt_preempt_enable(); rt_preempt_enable();
#else
rt_exit_critical();
#endif
} }
RTM_EXPORT(rt_spin_unlock) RTM_EXPORT(rt_spin_unlock)
rt_base_t rt_spin_lock_irqsave(rt_spinlock_t *lock) rt_base_t rt_spin_lock_irqsave(struct rt_spinlock *lock)
{ {
unsigned long level; unsigned long level;
#ifdef RT_USING_SMP
rt_preempt_disable(); rt_preempt_disable();
level = rt_hw_local_irq_disable(); level = rt_hw_local_irq_disable();
rt_hw_spin_lock(&lock->lock); rt_hw_spin_lock(&lock->lock);
return level; return level;
#else
return rt_hw_interrupt_disable();
#endif
} }
RTM_EXPORT(rt_spin_lock_irqsave) RTM_EXPORT(rt_spin_lock_irqsave)
void rt_spin_unlock_irqrestore(rt_spinlock_t *lock, rt_base_t level) void rt_spin_unlock_irqrestore(struct rt_spinlock *lock, rt_base_t level)
{ {
#ifdef RT_USING_SMP
rt_hw_spin_unlock(&lock->lock); rt_hw_spin_unlock(&lock->lock);
rt_hw_local_irq_enable(level); rt_hw_local_irq_enable(level);
rt_preempt_enable(); rt_preempt_enable();
#else
rt_hw_interrupt_enable(level);
#endif
} }
RTM_EXPORT(rt_spin_unlock_irqrestore) RTM_EXPORT(rt_spin_unlock_irqrestore)
#ifdef RT_USING_SMP
static struct rt_cpu rt_cpus[RT_CPUS_NR];
rt_hw_spinlock_t _cpus_lock;
/** /**
* This fucntion will return current cpu. * This fucntion will return current cpu.
*/ */
@ -155,7 +134,7 @@ rt_base_t rt_cpus_lock(void)
pcpu = rt_cpu_self(); pcpu = rt_cpu_self();
if (pcpu->current_thread != RT_NULL) if (pcpu->current_thread != RT_NULL)
{ {
register rt_uint16_t lock_nest = pcpu->current_thread->cpus_lock_nest; register rt_ubase_t lock_nest = pcpu->current_thread->cpus_lock_nest;
pcpu->current_thread->cpus_lock_nest++; pcpu->current_thread->cpus_lock_nest++;
if (lock_nest == 0) if (lock_nest == 0)