Skip to content

Commit

Permalink
馃巿 perf: perf rt_hw_interrupt_disable/enable (#8042)
Browse files Browse the repository at this point in the history
Signed-off-by: Shell <smokewood@qq.com>
Co-authored-by: Shell <smokewood@qq.com>
  • Loading branch information
xqyjlj and polarvid committed Oct 25, 2023
1 parent 91fc52d commit 3283f54
Show file tree
Hide file tree
Showing 80 changed files with 2,455 additions and 1,939 deletions.
4 changes: 3 additions & 1 deletion bsp/qemu-virt64-aarch64/drivers/secondary_cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "gic.h"
#include "interrupt.h"
#include "mmu.h"
#include "gtimer.h"

#ifdef RT_USING_SMP

Expand All @@ -29,8 +30,9 @@ void rt_hw_secondary_cpu_bsp_start(void)
arm_gic_cpu_init(0, 0);

// local timer init
rt_hw_gtimer_init();

rt_system_scheduler_start();
}

#endif // SMP
#endif // SMP
4 changes: 3 additions & 1 deletion components/dfs/dfs_v1/src/dfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -800,11 +800,13 @@ struct dfs_fdtable *dfs_fdtable_get_pid(int pid)
struct rt_lwp *lwp = RT_NULL;
struct dfs_fdtable *fdt = RT_NULL;

lwp = lwp_from_pid(pid);
lwp_pid_lock_take();
lwp = lwp_from_pid_locked(pid);
if (lwp)
{
fdt = &lwp->fdt;
}
lwp_pid_lock_release();

return fdt;
}
Expand Down
4 changes: 3 additions & 1 deletion components/dfs/dfs_v2/src/dfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,13 @@ struct dfs_fdtable *dfs_fdtable_get_pid(int pid)
struct rt_lwp *lwp = RT_NULL;
struct dfs_fdtable *fdt = RT_NULL;

lwp = lwp_from_pid(pid);
lwp_pid_lock_take();
lwp = lwp_from_pid_locked(pid);
if (lwp)
{
fdt = &lwp->fdt;
}
lwp_pid_lock_release();

return fdt;
}
Expand Down
2 changes: 2 additions & 0 deletions components/drivers/include/drivers/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ struct rt_serial_device
void *serial_rx;
void *serial_tx;

struct rt_spinlock spinlock;

struct rt_device_notify rx_notify;
};
typedef struct rt_serial_device rt_serial_t;
Expand Down
1 change: 1 addition & 0 deletions components/drivers/include/ipc/completion.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct rt_completion

/* suspended list */
rt_list_t suspended_list;
struct rt_spinlock spinlock;
};

void rt_completion_init(struct rt_completion *completion);
Expand Down
1 change: 1 addition & 0 deletions components/drivers/include/ipc/dataqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct rt_data_queue
rt_uint16_t is_full : 1;

struct rt_data_item *queue;
struct rt_spinlock spinlock;

rt_list_t suspended_push_list;
rt_list_t suspended_pop_list;
Expand Down
1 change: 1 addition & 0 deletions components/drivers/include/ipc/ringblk_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ struct rt_rbb
rt_slist_t *tail;
/* free node list */
rt_slist_t free_list;
struct rt_spinlock spinlock;
};
typedef struct rt_rbb *rt_rbb_t;

Expand Down
2 changes: 2 additions & 0 deletions components/drivers/include/ipc/waitqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct rt_wqueue_node
rt_thread_t polling_thread;
rt_list_t list;

rt_wqueue_t *wqueue;
rt_wqueue_func_t wakeup;
rt_uint32_t key;
};
Expand All @@ -39,6 +40,7 @@ rt_inline void rt_wqueue_init(rt_wqueue_t *queue)

queue->flag = RT_WQ_FLAG_CLEAN;
rt_list_init(&(queue->waiting_list));
rt_spin_lock_init(&(queue->spinlock));
}

void rt_wqueue_add(rt_wqueue_t *queue, struct rt_wqueue_node *node);
Expand Down
1 change: 1 addition & 0 deletions components/drivers/include/ipc/workqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct rt_workqueue

struct rt_semaphore sem;
rt_thread_t work_thread;
struct rt_spinlock spinlock;
};

struct rt_work
Expand Down
20 changes: 11 additions & 9 deletions components/drivers/ipc/completion.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Date Author Notes
* 2012-09-30 Bernard first version.
* 2021-08-18 chenyingchun add comments
* 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
*/

#include <rthw.h>
Expand All @@ -25,10 +26,11 @@ void rt_completion_init(struct rt_completion *completion)
rt_base_t level;
RT_ASSERT(completion != RT_NULL);

level = rt_hw_interrupt_disable();
rt_spin_lock_init(&(completion->spinlock));
level = rt_spin_lock_irqsave(&(completion->spinlock));
completion->flag = RT_UNCOMPLETED;
rt_list_init(&completion->suspended_list);
rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(completion->spinlock), level);
}
RTM_EXPORT(rt_completion_init);

Expand Down Expand Up @@ -62,7 +64,7 @@ rt_err_t rt_completion_wait(struct rt_completion *completion,
result = RT_EOK;
thread = rt_thread_self();

level = rt_hw_interrupt_disable();
level = rt_spin_lock_irqsave(&(completion->spinlock));
if (completion->flag != RT_COMPLETED)
{
/* only one thread can suspend on complete */
Expand Down Expand Up @@ -97,22 +99,22 @@ rt_err_t rt_completion_wait(struct rt_completion *completion,
rt_timer_start(&(thread->thread_timer));
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(completion->spinlock), level);

/* do schedule */
rt_schedule();

/* thread is waked up */
result = thread->error;

level = rt_hw_interrupt_disable();
level = rt_spin_lock_irqsave(&(completion->spinlock));
}
}
/* clean completed flag */
completion->flag = RT_UNCOMPLETED;

__exit:
rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(completion->spinlock), level);

return result;
}
Expand All @@ -131,7 +133,7 @@ void rt_completion_done(struct rt_completion *completion)
if (completion->flag == RT_COMPLETED)
return;

level = rt_hw_interrupt_disable();
level = rt_spin_lock_irqsave(&(completion->spinlock));
completion->flag = RT_COMPLETED;

if (!rt_list_isempty(&(completion->suspended_list)))
Expand All @@ -146,14 +148,14 @@ void rt_completion_done(struct rt_completion *completion)

/* resume it */
rt_thread_resume(thread);
rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(completion->spinlock), level);

/* perform a schedule */
rt_schedule();
}
else
{
rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(completion->spinlock), level);
}
}
RTM_EXPORT(rt_completion_done);
Expand Down
49 changes: 26 additions & 23 deletions components/drivers/ipc/dataqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Date Author Notes
* 2012-09-30 Bernard first version.
* 2016-10-31 armink fix some resume push and pop thread bugs
* 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
*/

#include <rthw.h>
Expand Down Expand Up @@ -57,6 +58,8 @@ rt_data_queue_init(struct rt_data_queue *queue,
queue->is_empty = 1;
queue->is_full = 0;

rt_spin_lock_init(&(queue->spinlock));

rt_list_init(&(queue->suspended_push_list));
rt_list_init(&(queue->suspended_pop_list));

Expand Down Expand Up @@ -103,7 +106,7 @@ rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
result = RT_EOK;
thread = rt_thread_self();

level = rt_hw_interrupt_disable();
level = rt_spin_lock_irqsave(&(queue->spinlock));
while (queue->is_full)
{
/* queue is full */
Expand Down Expand Up @@ -131,14 +134,14 @@ rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
}

/* enable interrupt */
rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(queue->spinlock), level);

/* do schedule */
rt_schedule();

/* thread is waked up */
result = thread->error;
level = rt_hw_interrupt_disable();
level = rt_spin_lock_irqsave(&(queue->spinlock));
if (result != RT_EOK) goto __exit;
}

Expand All @@ -165,7 +168,7 @@ rt_err_t rt_data_queue_push(struct rt_data_queue *queue,

/* resume it */
rt_thread_resume(thread);
rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(queue->spinlock), level);

/* perform a schedule */
rt_schedule();
Expand All @@ -174,7 +177,7 @@ rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
}

__exit:
rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
if ((result == RT_EOK) && queue->evt_notify != RT_NULL)
{
queue->evt_notify(queue, RT_DATAQUEUE_EVENT_PUSH);
Expand Down Expand Up @@ -222,7 +225,7 @@ rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
result = RT_EOK;
thread = rt_thread_self();

level = rt_hw_interrupt_disable();
level = rt_spin_lock_irqsave(&(queue->spinlock));
while (queue->is_empty)
{
/* queue is empty */
Expand All @@ -249,14 +252,14 @@ rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
}

/* enable interrupt */
rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(queue->spinlock), level);

/* do schedule */
rt_schedule();

/* thread is waked up */
result = thread->error;
level = rt_hw_interrupt_disable();
level = rt_spin_lock_irqsave(&(queue->spinlock));
if (result != RT_EOK)
goto __exit;
}
Expand Down Expand Up @@ -286,14 +289,14 @@ rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,

/* resume it */
rt_thread_resume(thread);
rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(queue->spinlock), level);

/* perform a schedule */
rt_schedule();
}
else
{
rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
}

if (queue->evt_notify != RT_NULL)
Expand All @@ -303,7 +306,7 @@ rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
}

__exit:
rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
if ((result == RT_EOK) && (queue->evt_notify != RT_NULL))
{
queue->evt_notify(queue, RT_DATAQUEUE_EVENT_POP);
Expand Down Expand Up @@ -339,12 +342,12 @@ rt_err_t rt_data_queue_peek(struct rt_data_queue *queue,
return -RT_EEMPTY;
}

level = rt_hw_interrupt_disable();
level = rt_spin_lock_irqsave(&(queue->spinlock));

*data_ptr = queue->queue[queue->get_index].data_ptr;
*size = queue->queue[queue->get_index].data_size;

rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(queue->spinlock), level);

return RT_EOK;
}
Expand All @@ -366,14 +369,14 @@ void rt_data_queue_reset(struct rt_data_queue *queue)
RT_ASSERT(queue != RT_NULL);
RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);

level = rt_hw_interrupt_disable();
level = rt_spin_lock_irqsave(&(queue->spinlock));

queue->get_index = 0;
queue->put_index = 0;
queue->is_empty = 1;
queue->is_full = 0;

rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(queue->spinlock), level);

rt_enter_critical();
/* wakeup all suspend threads */
Expand All @@ -382,7 +385,7 @@ void rt_data_queue_reset(struct rt_data_queue *queue)
while (!rt_list_isempty(&(queue->suspended_pop_list)))
{
/* disable interrupt */
level = rt_hw_interrupt_disable();
level = rt_spin_lock_irqsave(&(queue->spinlock));

/* get next suspend thread */
thread = rt_list_entry(queue->suspended_pop_list.next,
Expand All @@ -399,14 +402,14 @@ void rt_data_queue_reset(struct rt_data_queue *queue)
rt_thread_resume(thread);

/* enable interrupt */
rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
}

/* resume on push list */
while (!rt_list_isempty(&(queue->suspended_push_list)))
{
/* disable interrupt */
level = rt_hw_interrupt_disable();
level = rt_spin_lock_irqsave(&(queue->spinlock));

/* get next suspend thread */
thread = rt_list_entry(queue->suspended_push_list.next,
Expand All @@ -423,7 +426,7 @@ void rt_data_queue_reset(struct rt_data_queue *queue)
rt_thread_resume(thread);

/* enable interrupt */
rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
}
rt_exit_critical();

Expand All @@ -448,9 +451,9 @@ rt_err_t rt_data_queue_deinit(struct rt_data_queue *queue)
/* wakeup all suspend threads */
rt_data_queue_reset(queue);

level = rt_hw_interrupt_disable();
level = rt_spin_lock_irqsave(&(queue->spinlock));
queue->magic = 0;
rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(queue->spinlock), level);

rt_free(queue->queue);

Expand Down Expand Up @@ -478,7 +481,7 @@ rt_uint16_t rt_data_queue_len(struct rt_data_queue *queue)
return 0;
}

level = rt_hw_interrupt_disable();
level = rt_spin_lock_irqsave(&(queue->spinlock));

if (queue->put_index > queue->get_index)
{
Expand All @@ -489,7 +492,7 @@ rt_uint16_t rt_data_queue_len(struct rt_data_queue *queue)
len = queue->size + queue->put_index - queue->get_index;
}

rt_hw_interrupt_enable(level);
rt_spin_unlock_irqrestore(&(queue->spinlock), level);

return len;
}
Expand Down

0 comments on commit 3283f54

Please sign in to comment.