|
@@ -1,8 +1,8 @@
|
|
|
-#include <common/kthread.h>
|
|
|
#include <common/glib.h>
|
|
|
+#include <common/kthread.h>
|
|
|
#include <common/spinlock.h>
|
|
|
-#include <sched/sched.h>
|
|
|
#include <debug/bug.h>
|
|
|
+#include <sched/sched.h>
|
|
|
#include <time/sleep.h>
|
|
|
|
|
|
static spinlock_t __kthread_create_lock; // kthread创建过程的锁
|
|
@@ -28,28 +28,13 @@ struct kthread_create_info_t
|
|
|
void *data;
|
|
|
int node;
|
|
|
|
|
|
- // kthreadd守护进程传递给kthread_create的结果, 成功则返回PCB,不成功则该值为负数错误码。若该值为NULL,意味着创建过程尚未完成
|
|
|
+ // kthreadd守护进程传递给kthread_create的结果,
|
|
|
+ // 成功则返回PCB,不成功则该值为负数错误码。若该值为NULL,意味着创建过程尚未完成
|
|
|
struct process_control_block *result;
|
|
|
|
|
|
struct List list;
|
|
|
};
|
|
|
|
|
|
-/**
|
|
|
- * @brief kthread信息
|
|
|
- * 该结构体将会绑定到pcb的worker_private中
|
|
|
- */
|
|
|
-struct kthread_info_t
|
|
|
-{
|
|
|
- uint64_t flags;
|
|
|
- uint32_t cpu;
|
|
|
- int result;
|
|
|
- int (*thread_fn)(void *);
|
|
|
- void *data;
|
|
|
- // todo: 将这里改为completion机制
|
|
|
- bool exited; // 是否已退出
|
|
|
- char *full_name; // 内核线程的名称
|
|
|
-};
|
|
|
-
|
|
|
/**
|
|
|
* @brief 获取pcb中的kthread结构体
|
|
|
*
|
|
@@ -62,8 +47,7 @@ static inline struct kthread_info_t *to_kthread(struct process_control_block *pc
|
|
|
return pcb->worker_private;
|
|
|
}
|
|
|
|
|
|
-static struct process_control_block *__kthread_create_on_node(int (*thread_fn)(void *data), void *data,
|
|
|
- int node,
|
|
|
+static struct process_control_block *__kthread_create_on_node(int (*thread_fn)(void *data), void *data, int node,
|
|
|
const char name_fmt[], va_list args)
|
|
|
{
|
|
|
struct process_control_block *pcb = NULL;
|
|
@@ -96,6 +80,22 @@ static struct process_control_block *__kthread_create_on_node(int (*thread_fn)(v
|
|
|
if (!IS_ERR(create->result))
|
|
|
{
|
|
|
// todo: 为内核线程设置名字
|
|
|
+ char pcb_name[PCB_NAME_LEN];
|
|
|
+ va_list get_args;
|
|
|
+ va_copy(get_args, args);
|
|
|
+ //获取到字符串的前16字节
|
|
|
+ int len = vsnprintf(pcb_name, name_fmt, PCB_NAME_LEN, get_args);
|
|
|
+ if (len >= PCB_NAME_LEN)
|
|
|
+ {
|
|
|
+ //名字过大 放到full_name字段中
|
|
|
+ struct kthread_info_t *kthread = to_kthread(pcb);
|
|
|
+ char *full_name = kzalloc(1024, __GFP_ZERO);
|
|
|
+ vsprintf(full_name, name_fmt, get_args);
|
|
|
+ kthread->full_name = full_name;
|
|
|
+ }
|
|
|
+ //将前16Bytes放到pcb的name字段
|
|
|
+ process_set_pcb_name(pcb, pcb_name);
|
|
|
+ va_end(get_args);
|
|
|
}
|
|
|
|
|
|
kfree(create);
|
|
@@ -131,8 +131,7 @@ void kthread_exit(long result)
|
|
|
* 当内核线程被唤醒时,会运行thread_fn函数,并将data作为参数传入。
|
|
|
* 内核线程可以直接返回,也可以在kthread_should_stop为真时返回。
|
|
|
*/
|
|
|
-struct process_control_block *kthread_create_on_node(int (*thread_fn)(void *data), void *data,
|
|
|
- int node,
|
|
|
+struct process_control_block *kthread_create_on_node(int (*thread_fn)(void *data), void *data, int node,
|
|
|
const char name_fmt[], ...)
|
|
|
{
|
|
|
struct process_control_block *pcb;
|
|
@@ -171,7 +170,7 @@ static int kthread(void *_create)
|
|
|
// 将当前pcb返回给创建者
|
|
|
create->result = current_pcb;
|
|
|
|
|
|
- current_pcb->state &= ~PROC_RUNNING; // 设置当前进程不是RUNNING态
|
|
|
+ current_pcb->state &= ~PROC_RUNNING; // 设置当前进程不是RUNNING态
|
|
|
// 发起调度,使得当前内核线程休眠。直到创建者通过process_wakeup将当前内核线程唤醒
|
|
|
sched();
|
|
|
|
|
@@ -221,7 +220,8 @@ int kthreadd(void *unused)
|
|
|
{
|
|
|
|
|
|
// 从链表中取出第一个要创建的内核线程任务
|
|
|
- struct kthread_create_info_t *create = container_of(kthread_create_list.next, struct kthread_create_info_t, list);
|
|
|
+ struct kthread_create_info_t *create =
|
|
|
+ container_of(kthread_create_list.next, struct kthread_create_info_t, list);
|
|
|
list_del_init(&create->list);
|
|
|
spin_unlock(&__kthread_create_lock);
|
|
|
|