spinlock.c
来自「多线程库」· C语言 代码 · 共 55 行
C
55 行
//// 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 + =
减小字号Ctrl + -
显示快捷键?