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);
// 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);
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;
}
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;
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);
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;
#include <types.h>
+#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