为什么 Ruby 的 IO#pwrite 是线程安全的?

Why is Ruby's IO#pwrite thread-safe?

我想知道是否有人可以向我解释为什么 Ruby 的 IO::pwrite 函数在文档中被称为线程安全的:

This is advantageous to combining IO#seek and IO#write in that it is atomic, allowing multiple threads/process to share the same IO object for reading the file at various location

我对原子性的理解是全有或全无,如果出现错误,“事务”将回滚,因此在这种情况下,文件将以其原始内容关闭(正确吗?)。
但是,原子性不保证线程同步,除非 rb_thread_io_blocking_region 是同步方法?
这是 pwrite 函数的源代码片段,也可用 here

    n = (ssize_t)rb_thread_io_blocking_region(internal_pwrite_func, &arg, fptr->fd);
    if (n < 0) rb_sys_fail_path(fptr->pathv);
    rb_str_tmp_frozen_release(str, tmp);

    return SSIZET2NUM(n);
}

同步是由内核(操作系统)执行的,而不是Ruby。

根据文档,Ruby 的 pwrite 调用 this pwrite 负责同步。

描述了 pwrite 系统调用的行为 here。具体来说:

After a write() to a regular file has successfully returned:

  • Any successful read() from each byte position in the file that was modified by that write shall return the data specified by the write() for that position until such byte positions are again modified.

  • Any subsequent successful write() to the same byte position in the file shall overwrite that file data.

广泛的基本原理更详细地讨论了序列化。