【发布时间】:2021-06-18 19:38:24
【问题描述】:
阅读implementing locks的操作系统资料时的一些疑惑
struct lock {
int locked;
struct queue q;
int sync; /* Normally 0. */
};
void lock_acquire(struct lock *l) {
intr_disable();
while (swap(&l->sync, 1) != 0) {
/* Do nothing */
}
if (!l->locked) {
l->locked = 1;
l->sync = 0;
} else {
queue_add(&l->q, thread_current());
thread_block(&l->sync);
}
intr_enable();
}
void lock_release(struct lock *l) {
intr_disable();
while (swap(&l->sync, 1) != 0) {
/* Do nothing */
}
if (queue_empty(&l->q) {
l->locked = 0;
} else {
thread_unblock(queue_remove(&l->q));
}
l->sync = 0;
intr_enable();
}
sync的目的是什么?
【问题讨论】:
-
是的,队列通常是 FIFO 数据结构,您发布的代码 sn-p 似乎也是如此。
标签: c multithreading operating-system