kthread_work 和 kthread_work 其他功能
kthread_work and kthread_worker functionality
我需要为工作处理器使用 kthread 函数,我不太清楚两者之间的区别。这是我对字段的理解。
struct kthread_worker {
spinlock_t lock; // lock to update the work queue
struct list_head work_list; // list kthread_work items
struct task_struct *task; // handle for thread
struct kthread_work *current_work; // ?
};
struct kthread_work {
struct list_head node; // list of threads?
kthread_work_func_t func; // func to execute?
struct kthread_worker *worker; // worker associated with this work item
};
我的问题是:
任何不清楚的领域的澄清。
kthread_work_func_t 是一个 func ptr,期望参数为 kthread_work。这是如何运作的?它应该指向你希望线程执行的函数,对吧?
kthread_worker
是一个 worker,可以执行 works (kthread_work
)。 Work 可以随时添加到 worker。 Worker 一个一个执行works。如果当前没有 work,worker 等待。
kthread_work_func_t is a func ptr expecting an argument of kthread_work. How does that work? It should point to the function you want the thread to execute, right?
是的,这只是您要作为工作执行的功能。
如果只有一个作品使用这个函数(例如,这是某种垃圾收集器),函数可能会简单地忽略它的参数。
如果您想拥有多个使用相同功能但参数不同的作品,您可以将嵌入 kthread_work
结构到您的结构中,其中包含这些参数:
struct my_work
{
struct kthread_work work; //Base work object
int i; // Your parameter
}
// Parametrized work function
void my_func(struct kthread_work* work)
{
// Extract actual work object
struct my_work* my_work = container_of(work, struct my_work, work);
// Parameter for the work
int i = my_work->i;
// Do something
...
// Free memory used for work object
kfree(my_work);
}
// Helper for add your work with given parameter
void add_my_work(struct kthread_worker* worker, int i)
{
// Allocate your work object on the heap
struct my_work* my_work = kmalloc(sizeof(struct my_work), GFP_KERNEL);
// Initialize base work structure
init_kthread_work(&my_work->work, &my_func);
// Store parameter
work->i = i;
queue_kthread_work(worker, &my_work->work);
}
Any clarification on the unclear fields.
正如您从前面的示例中看到的那样,了解 struct kthread_worker
和 struct kthread_work
的字段对于仅使用它很少有用。但实际上语义很简单:
struct kthread_worker {
spinlock_t lock; // lock to update the work queue
struct list_head work_list; // list kthread_work items
struct task_struct *task; // handle for thread
struct kthread_work *current_work; // (+) currently executed work object
};
struct kthread_work {
struct list_head node; // (+) element in the kthread_worker.work_list
kthread_work_func_t func; // func to execute
struct kthread_worker *worker; // worker associated with this work item
};
我需要为工作处理器使用 kthread 函数,我不太清楚两者之间的区别。这是我对字段的理解。
struct kthread_worker {
spinlock_t lock; // lock to update the work queue
struct list_head work_list; // list kthread_work items
struct task_struct *task; // handle for thread
struct kthread_work *current_work; // ?
};
struct kthread_work {
struct list_head node; // list of threads?
kthread_work_func_t func; // func to execute?
struct kthread_worker *worker; // worker associated with this work item
};
我的问题是:
任何不清楚的领域的澄清。
kthread_work_func_t 是一个 func ptr,期望参数为 kthread_work。这是如何运作的?它应该指向你希望线程执行的函数,对吧?
kthread_worker
是一个 worker,可以执行 works (kthread_work
)。 Work 可以随时添加到 worker。 Worker 一个一个执行works。如果当前没有 work,worker 等待。
kthread_work_func_t is a func ptr expecting an argument of kthread_work. How does that work? It should point to the function you want the thread to execute, right?
是的,这只是您要作为工作执行的功能。
如果只有一个作品使用这个函数(例如,这是某种垃圾收集器),函数可能会简单地忽略它的参数。
如果您想拥有多个使用相同功能但参数不同的作品,您可以将嵌入 kthread_work
结构到您的结构中,其中包含这些参数:
struct my_work
{
struct kthread_work work; //Base work object
int i; // Your parameter
}
// Parametrized work function
void my_func(struct kthread_work* work)
{
// Extract actual work object
struct my_work* my_work = container_of(work, struct my_work, work);
// Parameter for the work
int i = my_work->i;
// Do something
...
// Free memory used for work object
kfree(my_work);
}
// Helper for add your work with given parameter
void add_my_work(struct kthread_worker* worker, int i)
{
// Allocate your work object on the heap
struct my_work* my_work = kmalloc(sizeof(struct my_work), GFP_KERNEL);
// Initialize base work structure
init_kthread_work(&my_work->work, &my_func);
// Store parameter
work->i = i;
queue_kthread_work(worker, &my_work->work);
}
Any clarification on the unclear fields.
正如您从前面的示例中看到的那样,了解 struct kthread_worker
和 struct kthread_work
的字段对于仅使用它很少有用。但实际上语义很简单:
struct kthread_worker {
spinlock_t lock; // lock to update the work queue
struct list_head work_list; // list kthread_work items
struct task_struct *task; // handle for thread
struct kthread_work *current_work; // (+) currently executed work object
};
struct kthread_work {
struct list_head node; // (+) element in the kthread_worker.work_list
kthread_work_func_t func; // func to execute
struct kthread_worker *worker; // worker associated with this work item
};