Update spinlock to avoid hungry.

This commit is contained in:
TXuian
2024-03-19 10:12:51 +08:00
parent 50dab7b553
commit 08c8f0b952
7 changed files with 39 additions and 63 deletions
@@ -25,8 +25,24 @@
#include "task.h"
#include "trap_common.h"
#include "list.h"
struct lock_node {
int cpu_id;
struct double_list_node node;
};
static struct double_list_node lock_request_guard;
static struct lock_node core_lock_request[NR_CPU];
static struct spinlock request_lock;
bool module_spinlock_use_intr_init(void)
{
for (int i = 0; i < NR_CPU; i++) {
core_lock_request[i].cpu_id = i;
doubleListNodeInit(&core_lock_request[i].node);
}
doubleListNodeInit(&lock_request_guard);
spinlock_init(&request_lock, "requestlock");
return true;
}
@@ -44,18 +60,31 @@ void spinlock_init(struct spinlock* lock, char* name)
}
extern int _spinlock_lock(struct spinlock* lock, uint32_t timeout);
void _spinlock_unlock(struct spinlock* lock);
void spinlock_lock(struct spinlock* lock)
{
if (lock->owner_cpu != SPINLOCK_STATE_UNLOCK && lock->owner_cpu == cur_cpuid()) {
ERROR("spinlock %s lock double locked by core %d\n", lock->name, lock->owner_cpu);
panic("");
}
// _spinlock_lock(&request_lock, SPINLOCK_LOCK_WAITFOREVER);
// doubleListAddOnBack(&core_lock_request[cur_cpuid()].node, &lock_request_guard);
// _spinlock_unlock(&request_lock);
// while (lock_request_guard.next != &core_lock_request[cur_cpuid()].node)
// ;
_spinlock_lock(lock, SPINLOCK_LOCK_WAITFOREVER);
}
void _spinlock_unlock(struct spinlock* lock);
void spinlock_unlock(struct spinlock* lock)
{
// assert(lock_request_guard.next == &core_lock_request[cur_cpuid()].node);
// _spinlock_lock(&request_lock, SPINLOCK_LOCK_WAITFOREVER);
// _double_list_del(core_lock_request[cur_cpuid()].node.prev, core_lock_request[cur_cpuid()].node.next);
// _spinlock_unlock(&request_lock);
_spinlock_unlock(lock);
}