From: acevest Date: Sat, 27 Apr 2024 11:37:48 +0000 (+0800) Subject: 修改atomic_t的定义 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zpipe.c?a=commitdiff_plain;h=e6b0835251d61592c54a78ad8e2ae097bc2181a8;p=kernel.git 修改atomic_t的定义 --- diff --git a/drivers/ide.c b/drivers/ide.c index dfe96f8..f7dab9e 100644 --- a/drivers/ide.c +++ b/drivers/ide.c @@ -49,9 +49,9 @@ void ide_pci_init(pci_device_t *pci) { semaphore_init(&ide_pci_controller[i].request_queue.sem, 0); semaphore_init(&ide_pci_controller[i].disk_intr_sem, 0); - ide_pci_controller[i].request_cnt = 0; - ide_pci_controller[i].irq_cnt = 0; - ide_pci_controller[i].consumed_cnt = 0; + atomic_set(&ide_pci_controller[i].request_cnt, 0); + atomic_set(&ide_pci_controller[i].irq_cnt, 0); + atomic_set(&ide_pci_controller[i].consumed_cnt, 0); iobase += i * 8; // secondary channel 需要加8 printd("ide pci Base IO Address Register %08x\n", iobase); @@ -137,8 +137,8 @@ void ide_irq_bh_handler(void *arg) { // printlxy(MPL_IDE, MPO_IDE, "disk irq %u req %u consumed %u ", disk_inter_cnt, disk_request_cnt, // disk_handled_cnt); - printlxy(MPL_IDE0 + channel, MPO_IDE, "IDE%d req %u irq %u consumed %u", channel, ide_ctrl->request_cnt, - ide_ctrl->irq_cnt, ide_ctrl->consumed_cnt); + printlxy(MPL_IDE0 + channel, MPO_IDE, "IDE%d req %u irq %u consumed %u", channel, + atomic_read(&(ide_ctrl->request_cnt)), ide_ctrl->irq_cnt, ide_ctrl->consumed_cnt); // up里不会立即重新调度进程 up(&ide_ctrl->disk_intr_sem); diff --git a/fs/buffer.c b/fs/buffer.c index fc98755..a754b5a 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -112,7 +112,8 @@ again: assert(NULL != t); assert(t->block_size == size); - if (t->ref_count == 0) { + // if (t->ref_count == 0) { + if (atomic_read(&t->ref_count) == 0) { b = t; break; } @@ -139,7 +140,7 @@ again: b->block = block; b->block_size = size; b->dev = dev; - b->ref_count = 1; + atomic_set(&(b->ref_count), 1); b->uptodate = 0; return b; @@ -147,7 +148,7 @@ again: void brelse(bbuffer_t *b) { assert(b != NULL); - assert(b->ref_count > 0); + assert(atomic_read(&(b->ref_count)) > 0); wait_completion(&b->io_done); @@ -217,7 +218,7 @@ void init_buffer() { b->block = 0; b->block_size = blocksize; - b->ref_count = 0; + atomic_set(&(b->ref_count), 0); b->data = data + j * blocksize; b->dev = 0; b->page = page; diff --git a/include/atomic.h b/include/atomic.h index 4afeb37..f16af72 100644 --- a/include/atomic.h +++ b/include/atomic.h @@ -11,9 +11,32 @@ #include +#if 1 + +typedef struct atomic { + volatile int counter; +} atomic_t; + +static inline void atomic_inc(atomic_t *v) { __sync_add_and_fetch(&(v->counter), 1); } +static inline void atomic_dec(atomic_t *v) { __sync_sub_and_fetch(&(v->counter), 1); } + +static inline int atomic_read(atomic_t *v) { return *((int *)(&(v->counter))); } + +static inline void atomic_set(atomic_t *v, int i) { __sync_lock_test_and_set(&(v->counter), i); } + +#else + +// 以下的定义方式 +// 优点: +// 1. 简化了类型定义,代码更简洁 +// 2. 可能在某些情况下减少内存开锁 +// 缺点: +// 1. 会丢失volatile限定符。volatile对于多线程和中断处理等场景中很重要。 +// 2. 限制了扩展性 typedef uint32_t atomic_t; #define atomic_inc(x) __sync_add_and_fetch((x), 1) #define atomic_dec(x) __sync_sub_and_fetch((x), 1) #define atomic_add(x, y) __sync_add_and_fetch((x), (y)) #define atomic_sub(x, y) __sync_sub_and_fetch((x), (y)) +#endif