162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci.. include:: ../disclaimer-zh_CN.rst 362306a36Sopenharmony_ci 462306a36Sopenharmony_ci:Original: Documentation/userspace-api/futex2.rst 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci:翻译: 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci 李睿 Rui Li <me@lirui.org> 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci====== 1162306a36Sopenharmony_cifutex2 1262306a36Sopenharmony_ci====== 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci:作者: André Almeida <andrealmeid@collabora.com> 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_cifutex,或者称为快速用户互斥锁(fast user mutex),是一组允许用户空间创建高性能同步 1762306a36Sopenharmony_ci机制的系统调用,比如用户空间中的互斥锁,信号量和条件变量。C标准库,如glibc,使用它作 1862306a36Sopenharmony_ci为实现更多高级接口的方式,如pthreads。 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_cifutex2是初代futex系统调用的后续版本,旨在克服原有接口的限制。 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci用户API 2362306a36Sopenharmony_ci======= 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci``futex_waitv()`` 2662306a36Sopenharmony_ci----------------- 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci等待一个futex数组,可由其中任意一个唤醒:: 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci futex_waitv(struct futex_waitv *waiters, unsigned int nr_futexes, 3162306a36Sopenharmony_ci unsigned int flags, struct timespec *timeout, clockid_t clockid) 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci struct futex_waitv { 3462306a36Sopenharmony_ci __u64 val; 3562306a36Sopenharmony_ci __u64 uaddr; 3662306a36Sopenharmony_ci __u32 flags; 3762306a36Sopenharmony_ci __u32 __reserved; 3862306a36Sopenharmony_ci }; 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci用户空间设置一个struct futex_waitv数组(最多128项),设置 ``uaddr`` 为等待的 4162306a36Sopenharmony_ci地址, ``val`` 为期望值, ``flags`` 为指定的类型(如private)和futex的大小。 4262306a36Sopenharmony_ci``__reserved`` 需要置为0,但是它可用作未来扩展。指向数组第一个元素的指针作为 4362306a36Sopenharmony_ci``waiters`` 传递。如果 ``waiters`` 或任何的 ``uaddr`` 地址无效,将返回 ``-EFAULT`` 。 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci如果用户空间拥有32位的指针,那么需要做显式转换来保证高位清零。 ``uintptr_t`` 设计 4662306a36Sopenharmony_ci得很精巧,在32/64位的指针上都正常工作。 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci``nr_futexes`` 指定了数组的大小。不在[1,128]区间内的值会使系统调用返回 ``-EINVAL`` 。 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci系统调用的 ``flags`` 参数需要置0,但可用作未来扩展。 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci对于每个 ``waiters`` 数组中的项,在 ``uaddr`` 的当前值会和 ``val`` 比较。如果 5362306a36Sopenharmony_ci不一致,系统调用会撤销截至目前完成的所有工作,并返回 ``-EAGAIN`` 。如果所有测试 5462306a36Sopenharmony_ci和验证都通过,系统调用会等待直到以下情况之一发生: 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci- 指定的timeout超时,返回 ``-ETIMEOUT`` 。 5762306a36Sopenharmony_ci- 一个信号被传递给睡眠中的任务,返回 ``-ERESTARTSYS`` 。 5862306a36Sopenharmony_ci- 某个列表中的futex被唤醒,返回那个被唤醒的futex的索引。 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci关于如何使用接口的例子可以在 ``tools/testing/selftests/futex/functional/futex_waitv.c`` 6162306a36Sopenharmony_ci中找到。 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci超时 6462306a36Sopenharmony_ci---- 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci``struct timespec *timeout`` 是一个指向绝对超时时间的可选参数。你需要在 ``clockid`` 6762306a36Sopenharmony_ci参数中指定要使用的时钟类型。支持 ``CLOCK_MONOTONIC`` 和 ``CLOCK_REALTIME`` 。这个 6862306a36Sopenharmony_ci系统调用只接受64位的timespec结构体。 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_cifutex的类型 7162306a36Sopenharmony_ci----------- 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_cifutex既可以是私有的也可以是共享的。私有用于多个进程共享同样的内存空间,并且futex的虚拟 7462306a36Sopenharmony_ci地址对所有进程都是一样的。这允许在内核中进行优化。要使用私有futex,需要在futex标志中指定 7562306a36Sopenharmony_ci``FUTEX_PRIVATE_FLAG`` 。对于那些不在同一内存空间共享的进程,可以让同一个futex拥有不同 7662306a36Sopenharmony_ci的虚拟地址(例如使用基于文件的共享内存),这需要不同的内部机制来使得正确进入队列。这是默认 7762306a36Sopenharmony_ci的行为,而且对私有futex和共享futex都适用。 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_cifutex可以是不同的大小:8,16,32或64位。目前只支持32位大小的futex,并且需要通过 ``FUTEX_32`` 8062306a36Sopenharmony_ci标志指定。 81