3 Star 12 Fork 3

纵使有花兼明月何堪无酒亦无人 / xukey

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
spinlock.c 1.84 KB
一键复制 编辑 原始数据 按行查看 历史
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2007 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Liexusong <280259971@qq.com> |
+----------------------------------------------------------------------+
*/
/* GCC support */
#include <stdlib.h>
#include "spinlock.h"
extern int ncpu;
void spin_lock(atomic_t *lock, int which)
{
int i, n;
for ( ;; ) {
if (*lock == 0 &&
__sync_bool_compare_and_swap(lock, 0, which)) {
return;
}
if (ncpu > 1) {
for (n = 1; n < 129; n << 1) {
for (i = 0; i < n; i++) {
__asm("pause");
}
if (*lock == 0 &&
__sync_bool_compare_and_swap(lock, 0, which)) {
return;
}
}
}
sched_yield();
}
}
void spin_unlock(atomic_t *lock, int which)
{
__sync_bool_compare_and_swap(lock, which, 0);
}
C
1
https://gitee.com/xavier007/xukey.git
git@gitee.com:xavier007/xukey.git
xavier007
xukey
xukey
master

搜索帮助