bbs.weharmonyos.com 上线

    鸿蒙研究站 | http://weharmonyos.com (国内)
              | https://weharmony.github.io (国外)
    论坛 | http://bbs.weharmonyos.com
    文档中心 | http://open.weharmonyos.com
    参考手册 | http://doxygen.weharmonyos.com
上级 30a4d146
此差异已折叠。
/* futex_demo.c
Usage: futex_demo [nloops]
(Default: 5)
Demonstrate the use of futexes in a program where parent and child
use a pair of futexes located inside a shared anonymous mapping to
synchronize access to a shared resource: the terminal. The two
processes each write 'num-loops' messages to the terminal and employ
a synchronization protocol that ensures that they alternate in
writing messages.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <linux/futex.h>
#include <sys/time.h>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)
static uint32_t *futex1, *futex2, *iaddr;
static int
futex(uint32_t *uaddr, int futex_op, uint32_t val,
const struct timespec *timeout, uint32_t *uaddr2, uint32_t val3)
{
return syscall(SYS_futex, uaddr, futex_op, val,
timeout, uaddr2, val3);
}
/* Acquire the futex pointed to by 'futexp': wait for its value to
become 1, and then set the value to 0. */
static void
fwait(uint32_t *futexp)
{
long s;
/* atomic_compare_exchange_strong(ptr, oldval, newval)
atomically performs the equivalent of:
if (*ptr == *oldval)
*ptr = newval;
It returns true if the test yielded true and *ptr was updated. */
while (1) {
/* Is the futex available? */
const uint32_t one = 1;
if (atomic_compare_exchange_strong(futexp, &one, 0))
break; /* Yes */
/* Futex is not available; wait. */
s = futex(futexp, FUTEX_WAIT, 0, NULL, NULL, 0);
if (s == -1 && errno != EAGAIN)
errExit("futex-FUTEX_WAIT");
}
}
/* Release the futex pointed to by 'futexp': if the futex currently
has the value 0, set its value to 1 and the wake any futex waiters,
so that if the peer is blocked in fwait(), it can proceed. */
static void
fpost(uint32_t *futexp)
{
long s;
/* atomic_compare_exchange_strong() was described
in comments above. */
const uint32_t zero = 0;
if (atomic_compare_exchange_strong(futexp, &zero, 1)) {
s = futex(futexp, FUTEX_WAKE, 1, NULL, NULL, 0);
if (s == -1)
errExit("futex-FUTEX_WAKE");
}
}
int
main(int argc, char *argv[])
{
pid_t childPid;
int nloops;
setbuf(stdout, NULL);
nloops = (argc > 1) ? atoi(argv[1]) : 5;
/* Create a shared anonymous mapping that will hold the futexes.
Since the futexes are being shared between processes, we
subsequently use the "shared" futex operations (i.e., not the
ones suffixed "_PRIVATE"). */
iaddr = mmap(NULL, sizeof(*iaddr) * 2, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if (iaddr == MAP_FAILED)
errExit("mmap");
futex1 = &iaddr[0];
futex2 = &iaddr[1];
*futex1 = 0; /* State: unavailable */
*futex2 = 1; /* State: available */
/* Create a child process that inherits the shared anonymous
mapping. */
childPid = fork();
if (childPid == -1)
errExit("fork");
if (childPid == 0) { /* Child */
for (int j = 0; j < nloops; j++) {
fwait(futex1);
printf("Child (%jd) %d\n", (intmax_t) getpid(), j);
fpost(futex2);
}
exit(EXIT_SUCCESS);
}
/* Parent falls through to here. */
for (int j = 0; j < nloops; j++) {
fwait(futex2);
printf("Parent (%jd) %d\n", (intmax_t) getpid(), j);
fpost(futex1);
}
wait(NULL);
exit(EXIT_SUCCESS);
}
\ No newline at end of file
git add -A
git commit -m ' 补充链接脚本的注解
百图画鸿蒙 + 百文说内核 + 百万注源码 => 挖透鸿蒙内核源码
git commit -m ' bbs.weharmonyos.com 上线
鸿蒙研究站 | http://weharmonyos.com (国内)
| https://weharmony.github.io (国外)
oschina | https://my.oschina.net/weharmony
博客园 | https://www.cnblogs.com/weharmony/
知乎 | https://www.zhihu.com/people/weharmonyos
csdn | https://blog.csdn.net/kuangyufei
51cto | https://harmonyos.51cto.com/column/34
掘金 | https://juejin.cn/user/756888642000808
公众号 | 鸿蒙研究站 (weharmonyos)
论坛 | http://bbs.weharmonyos.com
文档中心 | http://open.weharmonyos.com
参考手册 | http://doxygen.weharmonyos.com
'
git push gitee_origin master
......
嗨,朋友,感谢您的关注,如果觉得不错请分享更多人了解鸿蒙内核,您的鼓励是站长前进的动力。
站长 turing ,计算机硕士,某互联网公司技术副总裁 ,计划用 5 - 10 年时间把鸿蒙系统的底层实现整理出来。
包括:内核实现、驱动框架、协议栈、应用框架、编译构建、运行时系统 等其他核心子系统。工程浩大,自不量力,然兴趣所至,义无反顾, 此念不息,坚如磐石。
三百鸿蒙是指:
对内核写一百篇博客
对系统画一百张图
对源码注释一百万汉字
回复: 百图 获取3倍高清大图
回复: 百文 获取博客最新进展
回复: 百万 获取中文注解仓库
回复: 666 获取站长微信请酌情
百图画鸿蒙 + 百文说内核 + 百万注源码 => 挖透鸿蒙内核源码
鸿蒙研究站 | http://weharmonyos.com (国内)
| https://weharmony.github.io (国外)
oschina | https://my.oschina.net/weharmony
博客园 | https://www.cnblogs.com/weharmony/
知乎 | https://www.zhihu.com/people/weharmonyos
csdn | https://blog.csdn.net/kuangyufei
51cto | https://harmonyos.51cto.com/column/34
掘金 | https://juejin.cn/user/756888642000808
公众号 | 鸿蒙研究站 (weharmonyos)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册