📄 spinlock.c
字号:
//// struct spinlock//// this structure is a primitive semaphore. It simple controls// the situation of a single integer... with an on/off value.#include <thread_spinlock.h>extern "C" {# include <sched.h>};//// testandset//// test if the spinlock is clear, and set it in one atomic// instruction. Ensuring it is owned by only one process.int spinlock::testandset(){ int ret; __asm__ __volatile__("xchgl %0, %1" : "=r"(ret), "=m"(s_spinlock) : "0"(1), "m"(s_spinlock)); return ret;}//// acquire//// continue to try and set the spinlock, until successfull. Yielding// to other processes in the scheduling while waiting.void spinlock::acquire(){ while (testandset()) __sched_yield();}//// release//// release the spinlock, by setting it to 0.void spinlock::release(){#ifndef RELEASE s_spinlock = 0;#else RELEASE(&s_spinlock);#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -