folly库高性能futex源码探究

ops/2024/12/20 13:12:48/

Futex(快速用户空间互斥量)是一个32位的无符号整型原子变量,它允许对该值执行futex()系统调用。它以模板化的方式设计,使其能够与确定性调度测试(DeterministicSchedule)正确交互。

h源码:

enum class FutexResult {VALUE_CHANGED, /* futex value didn't match expected */AWOKEN,        /* wakeup by matching futex wake, or spurious wakeup */INTERRUPTED,   /* wakeup by interrupting signal */TIMEDOUT,      /* wakeup by expiring deadline */
};/*** Futex is an atomic 32 bit unsigned integer that provides access to the* futex() syscall on that value.  It is templated in such a way that it* can interact properly with DeterministicSchedule testing.** If you don't know how to use futex(), you probably shouldn't be using* this class.  Even if you do know how, you should have a good reason* (and benchmarks to back you up).*/
template <template <typename> class Atom = std::atomic>
struct Futex : Atom<uint32_t> {Futex() : Atom<uint32_t>() {}explicit constexpr Futex(uint32_t init) : Atom<uint32_t>(init) {}/** Puts the thread to sleep if this->load() == expected.  Returns true when*  it is returning because it has consumed a wake() event, false for any*  other return (signal, this->load() != expected, or spurious wakeup). */FutexResult futexWait(uint32_t expected, uint32_t waitMask = -1) {auto rv = futexWaitImpl(expected, nullptr, nullptr, waitMask);assert(rv != FutexResult::TIMEDOUT);return rv;}/** Similar to futexWait but also accepts a deadline until when the wait call*  may block.**  Optimal clock types: std::chrono::system_clock, std::chrono::steady_clock.*  NOTE: On some systems steady_clock is just an alias for system_clock,*  and is not actually steady.**  For any other clock type, now() will be invoked twice. */template <class Clock, class Duration = typename Clock::duration>FutexResult futexWaitUntil(uint32_t expected,std::chrono::time_point<Clock, Duration> const& deadline,uint32_t waitMask = -1) {using Target = typename std::conditional<Clock::is_steady,std::chrono::steady_clock,std::chrono::system_clock>::type;auto const converted = time_point_conv<Target>(deadline);return converted == Target::time_point::max()? futexWaitImpl(expected, nullptr, nullptr, waitMask): futexWaitImpl(expected, converted, waitMask);}/** Wakens up to count waiters where (waitMask & wakeMask) !=*  0, returning the number of awoken threads, or -1 if an error*  occurred.  Note that when constructing a concurrency primitive*  that can guard its own destruction, it is likely that you will*  want to ignore EINVAL here (as well as making sure that you*  never touch the object after performing the memory store that*  is the linearization point for unlock or control handoff).*  See https://sourceware.org/bugzilla/show_bug.cgi?id=13690 */int futexWake(int count = std::numeric_limits<int>::max(),uint32_t wakeMask = -1);private:/** Optimal when TargetClock is the same type as Clock.**  Otherwise, both Clock::now() and TargetClock::now() must be invoked. */template <typename TargetClock, typename Clock, typename Duration>static typename TargetClock::time_point time_point_conv(std::chrono::time_point<Clock, Duration> const& time) {using std::chrono::duration_cast;using TimePoint = std::chrono::time_point<Clock, Duration>;using TargetDuration = typename TargetClock::duration;using TargetTimePoint = typename TargetClock::time_point;if (time == TimePoin

http://www.ppmy.cn/ops/143496.html

相关文章

电子应用设计方案-61:智能沙发系统方案设计

智能沙发系统方案设计 一、引言 智能沙发作为现代家居的创新产品&#xff0c;旨在为用户提供更加舒适、便捷和个性化的体验。本方案将详细介绍智能沙发系统的设计理念、功能特点、技术实现以及系统架构。 二、系统概述 1. 系统目标 - 提供多种舒适的坐姿和躺姿调节&#xff0c…

C# OpenCV机器视觉:图像旋转(让生活的角度更美好!)

在一个阳光明媚的下午&#xff0c;阿强坐在公园的长椅上&#xff0c;享受着温暖的阳光和微风。他的手里拿着一本书&#xff0c;书中的故事让他沉浸在一个奇幻的世界里。突然&#xff0c;他的手机响了&#xff0c;是他的朋友发来的信息&#xff1a;“快来看看我拍的照片&#xf…

【iOS】OC语法知识小结

文章目录 前言inWithCustomView:视图弹出方法修改UITextField的占位符文本颜色视图变换的动态效果导航栏的阴影效果导航栏阴影效果管理和自定义阴影效果 reloadDatareloadData 方法的使用reloadData 的工作原理高效使用 reloadData 键盘的不同属性类型总结 前言 在之前写项目和…

深入了解京东API接口:如何高效获取商品详情与SKU信息

在当今数字化时代&#xff0c;电商平台的数据接口成为了连接商家与消费者的桥梁。京东作为国内领先的电商平台&#xff0c;其API接口为开发者提供了丰富的商品信息获取途径。本文将深入探讨如何使用京东API接口高效获取商品详情与SKU信息&#xff0c;并附上简短而实用的代码示例…

电脑经常出现“msvcp110.dll文件丢失”的情况是什么原因“msvcp110.dll文件丢失”的解决方法

电脑经常出现“msvcp110.dll文件丢失”的情况&#xff0c;究竟是什么原因&#xff1f;以及&#xff0c;我们该如何解决&#xff1f; 在软件开发和日常使用电脑的过程中&#xff0c;不少朋友可能会遇到一些令人头疼的系统报错或文件丢失问题&#xff0c;比如“msvcp110.dll文件…

重生之我在异世界学编程之C语言:深入预处理篇(下)

大家好&#xff0c;这里是小编的博客频道 小编的博客&#xff1a;就爱学编程 很高兴在CSDN这个大家庭与大家相识&#xff0c;希望能在这里与大家共同进步&#xff0c;共同收获更好的自己&#xff01;&#xff01;&#xff01; 本文目录 引言正文一 条件编译&#xff08;1&#…

中国工程科技2040发展战略研究

近日&#xff0c;中国工程院“中国工程科技未来20年发展战略研究”总体项目组发布《愿景驱动的中国工程科技2040发展战略研究》&#xff0c;基于我国工程科技发展需求和世界发展趋势&#xff0c;提出“经济预测-需求分析-技术预见-愿景分析-战略架构-技术路线图-政策选择”战略…

代码随想录第48天

739. 每日温度 class Solution:def dailyTemperatures(self, temperatures: List[int]) -> List[int]:n len(temperatures)ans [0] * nst []for i in range(n - 1, -1, -1):t temperatures[i]while st and t > temperatures[st[-1]]:st.pop()if st:ans[i] st[-1] …