From ddf3d15ba11361f9a5515bf6105a11a05aa057b0 Mon Sep 17 00:00:00 2001 From: acevest Date: Fri, 20 Oct 2023 20:43:31 +0800 Subject: [PATCH] =?utf8?q?=E5=9C=A8=20buffer=20=E4=B8=8A=E6=B7=BB=E5=8A=A0?= =?utf8?q?=E7=AD=89=E5=BE=85=E9=98=9F=E5=88=97=E5=92=8C=20io=20=E7=BB=93?= =?utf8?q?=E6=9D=9Fcompletion?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- fs/buffer.c | 14 +++++++------- include/buffer.h | 5 ++--- include/completion.h | 2 -- include/sched.h | 2 -- include/semaphore.h | 2 -- include/task.h | 2 +- include/wait.h | 2 +- kernel/completion.c | 2 -- kernel/irq.c | 1 + 9 files changed, 12 insertions(+), 20 deletions(-) diff --git a/fs/buffer.c b/fs/buffer.c index a5f0777..5d98a22 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -108,7 +108,7 @@ again: assert(NULL != t); assert(t->block_size == size); - if (t->ref_count == 0 && t->locked == 0) { + if (t->ref_count == 0) { b = t; break; } @@ -123,13 +123,13 @@ again: goto again; } - // 此时b->locked不一定为0 + // 把它从free_list上删掉 + list_del_init(&b->node); + + // 虽然此时该bbuffer_t上的ref_count为0但其可能还有I/O操作没有完成 // 因为可能有的进程调用了write、read后再直接调用brelse - // 虽然此时ref_count为0,但io操作标记的b->locked并未结束 // 所以需要在此等待其结束 - while (b->locked == 1) { - // 此时是不用担心wake_up在这个sleep_on之前调用 - } + wait_completion(&b->io_done); // 找到了 b->block = block; @@ -190,7 +190,7 @@ void init_buffer() { b->dev = 0; b->page = page; b->uptodate = 0; - b->locked = 0; + init_completion(&b->io_done); list_init(&b->node); assert(NULL != b->data); diff --git a/include/buffer.h b/include/buffer.h index 8f570cc..c64a3a8 100644 --- a/include/buffer.h +++ b/include/buffer.h @@ -10,11 +10,11 @@ #pragma once #include +#include #include #include #include #include - typedef struct bbuffer { uint32_t block; // block number void *data; // @@ -22,8 +22,7 @@ typedef struct bbuffer { dev_t dev; page_t *page; list_head_t node; + completion_t io_done; uint16_t block_size; // block size uint16_t uptodate : 1; - uint16_t locked : 1; - uint16_t dirt : 1; // 还不支持 } bbuffer_t; diff --git a/include/completion.h b/include/completion.h index 291f73a..93afe91 100644 --- a/include/completion.h +++ b/include/completion.h @@ -9,7 +9,6 @@ #pragma once -#if 0 #include typedef struct completion { @@ -24,4 +23,3 @@ void wait_completion(completion_t *x); void init_completion(completion_t *x); void complete(completion_t *x); -#endif diff --git a/include/sched.h b/include/sched.h index ed00ce6..7bc55cc 100644 --- a/include/sched.h +++ b/include/sched.h @@ -25,8 +25,6 @@ void schedule(); -void wake_up(wait_queue_head_t *wqh); - extern task_union root_task; extern void load_cr3(task_union *tsk); diff --git a/include/semaphore.h b/include/semaphore.h index 27eb009..207341c 100644 --- a/include/semaphore.h +++ b/include/semaphore.h @@ -28,8 +28,6 @@ void semaphore_init(semaphore_t *s, unsigned int v); // 如果s->cnt == 0 会重新调度进程 volatile void down(semaphore_t *s); -// volatile bool try_down(semaphore_t *s); - // up // 只会唤醒进程,但不会立即重新调度进程 volatile void up(semaphore_t *s); diff --git a/include/task.h b/include/task.h index 1882723..795b28e 100644 --- a/include/task.h +++ b/include/task.h @@ -22,7 +22,7 @@ #include #include #include -#include +// #include enum { TASK_UNUSED, diff --git a/include/wait.h b/include/wait.h index 8f29e93..f1bf5ea 100644 --- a/include/wait.h +++ b/include/wait.h @@ -14,7 +14,7 @@ #include #include - +#include union task_union; typedef struct wait_queue_head { diff --git a/kernel/completion.c b/kernel/completion.c index d556f31..78c191c 100644 --- a/kernel/completion.c +++ b/kernel/completion.c @@ -7,7 +7,6 @@ * ------------------------------------------------------------------------ */ -#if 0 #include #include @@ -25,4 +24,3 @@ void init_completion(completion_t *x) { x->done = 0; init_wait_queue_head(&x->wait); } -#endif diff --git a/kernel/irq.c b/kernel/irq.c index ff2d265..b6f6a5b 100644 --- a/kernel/irq.c +++ b/kernel/irq.c @@ -75,6 +75,7 @@ void dump_irq_nr_stack() { #endif void irq_bh_handler(); +void schedule(); __attribute__((regparm(1))) void irq_handler(pt_regs_t *regs) { unsigned int irq = regs->irq; -- 2.44.0