diff --git a/src/sys/kernel/spinlock.c b/src/sys/kernel/spinlock.c new file mode 100755 index 0000000..94f87ce --- /dev/null +++ b/src/sys/kernel/spinlock.c @@ -0,0 +1,76 @@ +#include + +spinlock_t * spinlockCreate() +{ + spinlock_t * lock = kmalloc(sizeof(spinlock_t)); + + if (lock == NULL) + kpanic("SPINLOCK: Couldn't create spinlock\n"); + + memset(lock, '\0', sizeof(spinlock_t)); + + return lock; +} + +void spinlockDelete(spinlock_t * lock) +{ + int i; + int notComplete = 1; + + deleteInProgress = 1; + + while (notComplete == 1) + { + notComplete = 0; + for (i = 0; i < 16; i++) + { + while(choiceInProgress[i] != 0) + notComplete = 1; + while(ticket[i] != 0) + notComplete = 1; + } + } + + delete lock; +} + +int spinlockLock(spinlock_t *, int cpuID) +{ + int max_ticket = 0; + int i; + + if (deleteInProgress == 1) + return SPINLOCK_FAIL_DELETING; + + choiceInProgress[cpuID] = 1; + + for (i = 0; i < 16; i++) + if (ticket[i] >= max_ticket) + max_ticket = ticket[i]; + + max_ticket++; + + ticket[cpuID] = max_ticket; + choiceInProgress[cpuID] = 0; + + for (i = 0; i < 16; i++) + { + while(choiceInProgress[i] == 1) + ; + + while((ticket[i] < max_ticket) || + ((ticket[i] == max_ticket) && (i >= cpuID))) + ; + } + + return SPINLOCK_SUCCESS; + +} + +int spinlockUnlock(spinlock_t * lock, int cpuID) +{ + ticket[cpuID] = 0; + + return SPINLOCK_SUCCESS; +} + \ No newline at end of file