An attempt to implement equivalent of C++ "P1478R1: Byte-wise atomic memcpy" in Rust

Overview

atomic-memcpy

crates.io docs.rs license rustc build status

Byte-wise atomic memcpy.

This is an attempt to implement equivalent of C++ "P1478R1: Byte-wise atomic memcpy" in Rust.

This is expected to allow algorithms such as Seqlock and Chase-Lev deque to be implemented without UB of data races. See P1478R1 for more.

Status

  • If the alignment of the type being copied is the same as the pointer width, atomic_load is possible to produce an assembly roughly equivalent to the case of using volatile read + atomic fence on many platforms. (e.g., aarch64, riscv64. See tests/asm-test/asm directory for more).
  • If the alignment of the type being copied is smaller than the pointer width, there will be some performance degradation. However, it is implemented in such a way that it does not cause extreme performance degradation at least on x86_64. (See the implementation comments of atomic_load for more.) It is possible that there is still room for improvement, especially on non-x86_64 platforms.
  • Optimization for the case where the alignment of the type being copied is larger than the pointer width has not yet been fully investigated. It is possible that there is still room for improvement, especially on 32-bit platforms where AtomicU64 is available.
  • If the type being copied contains uninitialized bytes (e.g., padding) it is undefined behavior because the copy goes through integers. This problem will probably not be resolved until something like AtomicMaybeUninit is supported.

Related Projects

  • portable-atomic: Portable atomic types. Using byte-wise atomic memcpy to implement Seqlock, which is used in the fallback implementation.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

You might also like...
This project aims to implement a CSS(less like) parser in rust. Currently the code is targeting the PostCSS AST. Very early stage, do not use in production.

CSS(less like) parser written in rust (WIP) This project aims to implement a CSS(less like) parser in rust. Currently the code is targeting the PostCS

Rust crate to implement a counterpart to the PBRT book's (3rd edition) C++ code.
Rust crate to implement a counterpart to the PBRT book's (3rd edition) C++ code.

Rust crate to implement a counterpart to the PBRT book's (3rd edition) C++ code.

A proxy implement with http / socks5 in-bound and vmess out-bound, written in Rust and tokio.rs

tokio-vmess an Asynchronous proxy implement with http / socks5 in-bound and vmess out-bound, written in Rust and tokio Run example first, Fill out the

Shows how to implement USB device on RP2040 in Rust, in a single file, with no hidden parts.

Rust RP2040 USB Device Example This is a worked example of implementing a USB device on the RP2040 microcontroller, in Rust. It is designed to be easy

QQ ChatBot with ChatGPT api, implement in Rust

qbot QQChatBot with ChatGPT gpt-3.5-turbo api Rust 实现,抽象了 cqhttp 的收发消息 msg、openai ai 的消息对象,具有一定管理权限、预设机器人角色、机器人指令等功能。 原理上是接受 cqhttp 的 local websocket

A fully-featured lexer generator, implement as a proc macro

lexgen: A fully-featured lexer generator, implemented as a proc macro lexer! { // First line specifies name of the lexer and the token type return

Bevy plugin to help implement loading states

This is a plugin for the Bevy game engine, to help you implement loading states.

A diff-based data management language to implement unlimited undo, auto-save for games, and cloud-apps which needs to retain every change.

Docchi is a diff-based data management language to implement unlimited undo, auto-save for games, and cloud-apps which needs to save very often. User'

Lightweight websocket implement for stream transmission.

Lightws Lightweight websocket implement for stream transmission. Features Avoid heap allocation. Avoid buffering frame payload. Use vectored-io if ava

Implement Quicktime screen sharing part protocol.

Quicktime Screen sharing for iOS devices implement Quicktime part protocol. take screen record from iOS devices. Thank's for quicktime_video_hack full

A demo repo that shows how to use the latest component model feature in wasmtime to implement a key-value capability defined in a WIT file.

Key-Value Component Demo This repo serves as an example of how to use the latest wasm runtime wasmtime and its component-model feature to build and ex

SOCKS5 implement library, with some useful utilities such as dns-query, socks5-server, dns2socks, udp-client, etc.

socks5-impl Fundamental abstractions and async read / write functions for SOCKS5 protocol and Relatively low-level asynchronized SOCKS5 server impleme

Implement MPRIS D-Bus interface in your application

MPRIS Server Implement MPRIS D-Bus interface in your application. This library provides the essential functionalities for implementing the MPRIS D-Bus

The joker_query is a cute query builder, with Joker can implement most complex queries with sugar syntax
The joker_query is a cute query builder, with Joker can implement most complex queries with sugar syntax

joker_query The joker_query is most sugared query builder of Rust, with joker_query can implement most complex queries with sugar syntax Features − (O

A Rust library with homemade machine learning models to classify the MNIST dataset. Built in an attempt to get familiar with advanced Rust concepts.

mnist-classifier Ideas UPDATED: Finish CLI Flags Parallelize conputationally intensive functions Class-based naive bayes README Image parsing Confusio

An attempt at an operating system written in Rust

Rust Operating System An attempt at a simple operating system in Rust and a semester project for the Operating Systems course at CS@UCU. Documentation

RusTCP is an attempt to rewrite some of the PyTCP stack functionality using Rust language.
RusTCP is an attempt to rewrite some of the PyTCP stack functionality using Rust language.

RusTCP is an attempt to rewrite some of the PyTCP stack functionality using Rust language. Currently, the main goal of this project is to create a stable IPv6 platform that could be used to facilitate the process of labing the SRv6 technology.

Advent of Code 2021, also an attempt to practice a bit of Rust.

Advent of Code 2021 Advent of Code 2021 (my first one!), also an attempt to practice a bit of Rust. Running (Assuming that the respective inputs are i

Brine is my attempt at writing a Minecraft client in Rust using the Bevy game engine.
Brine is my attempt at writing a Minecraft client in Rust using the Bevy game engine.

Brine Brine is my attempt at writing a Minecraft client in Rust using the Bevy game engine. It's EXTREMELY work-in-progress. The thing that makes Brin

Comments
  • Use portable-atomic crate on no-std target

    Use portable-atomic crate on no-std target

    This allows removing volatile read/write hack on targets do not support atomic load/store.

    https://github.com/taiki-e/atomic-memcpy/blob/6a2a8f63b1339f1e93f07f47199695841615b168/src/lib.rs#L832-L834

    refs: portable-atomic

    opened by taiki-e 3
  • Remove miri hack

    Remove miri hack

    We currently use a hack to avoid https://github.com/rust-lang/rust/issues/69488 and to make sure that Miri errors for atomic load/store of integers containing uninitialized bytes (which is probably not a problem and uncharted territory at best 1 2 3, and can be detected by -Zmiri-check-number-validity 4), do not mask Miri errors for the use of uninitialized bytes (which is definitely a problem).

    https://github.com/taiki-e/atomic-memcpy/blob/3507fef17534e4825b2b303d04702b4678e29dd0/src/lib.rs#L426-L450

    However, this actually causes another "unsupported operation" Miri error.

    error: unsupported operation: unable to turn pointer into raw bytes
       --> /Users/taiki/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:701:9
        |
    701 |         copy_nonoverlapping(src, tmp.as_mut_ptr(), 1);
        |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes
        |
        = help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support
    
    opened by taiki-e 2
Releases(v0.2.0)
MiniDump a process in memory with rust

safetydump Rust in-memory MiniDump implementation. Features ntdll!NtGetNextProcess to obtain a handle for the desired ProcessId as opposed to kernel32

null 26 Oct 11, 2022
A cross-platform and safe Rust API to create and manage memory mappings in the virtual address space of the calling process.

mmap-rs A cross-platform and safe Rust API to create and manage memory mappings in the virtual address space of the calling process. This crate can be

S.J.R. van Schaik 19 Oct 23, 2022
Cross-platform library for reading/writing memory in other processes for Rust

vmemory Rust library for reading/writing memory in other processes for Windows, macOS, Linux, and in the future potentially, BSD variants. Rationale A

Jason Johnson 26 Nov 7, 2022
Benchmarking manual implementation of memcpy in Rust

Manual memcpy Benchmark Benchmarks that compare copying data between two Vec<u8>s using std::slice::copy_from_slice and a loop that copies one byte at

Adam Bratschi-Kaye 0 Feb 2, 2022
An inquiry into nondogmatic software development. An experiment showing double performance of the code running on JVM comparing to equivalent native C code.

java-2-times-faster-than-c An experiment showing double performance of the code running on JVM comparing to equivalent native C code ⚠️ The title of t

xemantic 49 Aug 14, 2022
Turbine is a toy CLI app for converting Rails schema declarations into equivalent type declarations in other languages.

Turbine Turbine is a toy CLI app for converting Rails schema declarations into equivalent type declarations in other languages. It’s described as a to

Justin 2 Jan 21, 2022
lispr is a Rust macro that tries to implement a small subset of LISPs syntax in Rust

lispr lispr is a Rust macro that tries to implement a small subset of LISPs syntax in Rust. It is neither especially beautiful or efficient since it i

Jan Vaorin 0 Feb 4, 2022
A crate to implement leader election for Kubernetes workloads in Rust.

Kubernetes Leader Election in Rust This library provides simple leader election for Kubernetes workloads.

Hendrik Maus 33 Dec 29, 2022
This project aims to implement a CSS(less like) parser in rust. Currently the code is targeting the PostCSS AST.

CSS(less like) parser written in rust (WIP) This project aims to implement a CSS(less like) parser in rust. Currently the code is targeting the PostCS

Huang Liuhaoran 21 Aug 23, 2022
The Raft algorithm implement by Rust.

Raft The Raft algorithm implement by Rust. This project refers to Eli Bendersky's website, the link as follows: https://eli.thegreenplace.net/2020/imp

Qiang Zhao 1 Oct 23, 2021