我在可加载模块中创建了2个Linux内核线程,并将它们绑定到运行在双核Android设备上的不同CPU核心。运行几次后,我注意到设备会因硬件看门狗计时器重置而重新启动。我一直遇到这个问题。可能是什么原因导致死锁?
基本上,我需要做的就是确保两个线程在不同的核心上同时运行do_something(),而没有任何人窃取CPU周期(即禁用中断)。为此,我使用自旋锁和易失变量。我还有一个信号量供父线程等待子线程。
基本上,我需要做的就是确保两个线程在不同的核心上同时运行do_something(),而没有任何人窃取CPU周期(即禁用中断)。为此,我使用自旋锁和易失变量。我还有一个信号量供父线程等待子线程。
#define CPU_COUNT 2
/* Globals */
spinlock_t lock;
struct semaphore sem;
volatile unsigned long count;
/* Thread util function for binding the thread to CPU*/
struct task_struct* thread_init(kthread_fn fn, void* data, int cpu)
{
struct task_struct *ts;
ts=kthread_create(fn, data, "per_cpu_thread");
kthread_bind(ts, cpu);
if (!IS_ERR(ts)) {
wake_up_process(ts);
}
else {
ERR("Failed to bind thread to CPU %d\n", cpu);
}
return ts;
}
/* Sync both threads */
void thread_sync()
{
spin_lock(&lock);
++count;
spin_unlock(&lock);
while (count != CPU_COUNT);
}
void do_something()
{
}
/* Child thread */
int per_cpu_thread_fn(void* data)
{
int i = 0;
unsigned long flags = 0;
int cpu = smp_processor_id();
DBG("per_cpu_thread entering (cpu:%d)...\n", cpu);
/* Disable local interrupts */
local_irq_save(flags);
/* sync threads */
thread_sync();
/* Do something */
do_something();
/* Enable interrupts */
local_irq_restore(flags);
/* Notify parent about exit */
up(&sem);
DBG("per_cpu_thread exiting (cpu:%d)...\n", cpu);
return value;
}
/* Main thread */
int main_thread()
{
int cpuB;
int cpu = smp_processor_id();
unsigned long flags = 0;
DBG("main thread running (cpu:%d)...\n", cpu);
/* Init globals*/
sema_init(&sem, 0);
spin_lock_init(&lock);
count = 0;
/* Launch child thread and bind to the other CPU core */
if (cpu == 0) cpuB = 1; else cpuB = 0;
thread_init(per_cpu_thread_fn, NULL, cpuB);
/* Disable local interrupts */
local_irq_save(flags);
/* thread sync */
thread_sync();
/* Do something here */
do_something();
/* Enable interrupts */
local_irq_restore(flags);
/* Wait for child to join */
DBG("main thread waiting for all child threads to finish ...\n");
down_interruptible(&sem);
}
count
的读取。我已经编辑了我的帖子,展示了如何做到这一点。 - Alexey Shmalko