[libc-commits] [libc] 83abec2 - [libc] Implement sync in unistd (#230908)

via libc-commits libc-commits at lists.llvm.org
Mon Oct 12 02:27:10 PDT 2026


Author: Aman Maurya
Date: 2026-10-12T14:57:06+05:30
New Revision: 83abec2e7090abcf6d2db4a46e1e917a0c927568

URL: https://github.com/llvm/llvm-project/commit/83abec2e7090abcf6d2db4a46e1e917a0c927568
DIFF: https://github.com/llvm/llvm-project/commit/83abec2e7090abcf6d2db4a46e1e917a0c927568.diff

LOG: [libc] Implement sync in unistd (#230908)

Implement the standard POSIX function `void sync(void);` in
`<unistd.h>`.

Fixes #230854

Added: 
    libc/src/__support/OSUtil/linux/syscall_wrappers/sync.h
    libc/src/unistd/linux/sync.cpp
    libc/src/unistd/sync.h
    libc/test/src/unistd/sync_test.cpp

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/arm/entrypoints.txt
    libc/config/linux/riscv/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/include/unistd.yaml
    libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
    libc/src/unistd/CMakeLists.txt
    libc/src/unistd/linux/CMakeLists.txt
    libc/test/src/unistd/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 1e213af91b6637..046d94f429838f 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -507,6 +507,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.sleep
     libc.src.unistd.symlink
     libc.src.unistd.symlinkat
+    libc.src.unistd.sync
     libc.src.unistd.tcgetpgrp
     libc.src.unistd.tcsetpgrp
     libc.src.unistd.truncate

diff  --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index ac66f1f5184842..4e535fe5111d67 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -299,6 +299,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.setuid
     libc.src.unistd.symlink
     libc.src.unistd.symlinkat
+    libc.src.unistd.sync
     libc.src.unistd.tcgetpgrp
     libc.src.unistd.tcsetpgrp
     libc.src.unistd.truncate

diff  --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index b6fa7ce486bb79..548a568e80b5ba 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -527,6 +527,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.sleep
     libc.src.unistd.symlink
     libc.src.unistd.symlinkat
+    libc.src.unistd.sync
     libc.src.unistd.tcgetpgrp
     libc.src.unistd.tcsetpgrp
     libc.src.unistd.truncate

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 213016907d3211..600e0e29dbbb2e 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -547,6 +547,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.sleep
     libc.src.unistd.symlink
     libc.src.unistd.symlinkat
+    libc.src.unistd.sync
     libc.src.unistd.tcgetpgrp
     libc.src.unistd.tcsetpgrp
     libc.src.unistd.truncate

diff  --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index 92951152b7ca7c..160b909f3af7b4 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -571,6 +571,12 @@ functions:
       - type: const char *
       - type: int
       - type: const char *
+  - name: sync
+    standards:
+      - posix
+    return_type: void
+    arguments:
+      - type: void
   - name: sysconf
     standards:
       - posix

diff  --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index a3f56b1117c2ba..978a29a3384d04 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1061,6 +1061,17 @@ add_header_library(
     libc.include.sys_syscall
 )
 
+add_header_library(
+  sync
+  HDRS
+    sync.h
+  DEPENDS
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.include.sys_syscall
+)
+
 add_header_library(
   dup
   HDRS

diff  --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/sync.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/sync.h
new file mode 100644
index 00000000000000..29761002d71d72
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/sync.h
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for sync.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SYNC_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SYNC_H
+
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE void sync() { syscall_impl<int>(SYS_sync); }
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SYNC_H

diff  --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index a9228513c03124..795224f0e9df48 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -448,6 +448,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.__llvm_libc_syscall
 )
 
+add_entrypoint_object(
+  sync
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sync
+)
+
 add_entrypoint_object(
   sysconf
   ALIAS

diff  --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 2555c9b700982c..3624f4e1891f2f 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -794,6 +794,18 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  sync
+  SRCS
+    sync.cpp
+  HDRS
+    ../sync.h
+  DEPENDS
+    libc.src.__support.OSUtil.linux.syscall_wrappers.sync
+    libc.src.__support.common
+    libc.src.__support.macros.config
+)
+
 add_entrypoint_object(
   sysconf
   SRCS

diff  --git a/libc/src/unistd/linux/sync.cpp b/libc/src/unistd/linux/sync.cpp
new file mode 100644
index 00000000000000..77b7ff2316ac7d
--- /dev/null
+++ b/libc/src/unistd/linux/sync.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of sync.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/sync.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/sync.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void, sync, ()) { linux_syscalls::sync(); }
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/unistd/sync.h b/libc/src/unistd/sync.h
new file mode 100644
index 00000000000000..045278b0f587f8
--- /dev/null
+++ b/libc/src/unistd/sync.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for sync.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_SYNC_H
+#define LLVM_LIBC_SRC_UNISTD_SYNC_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+void sync();
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_SYNC_H

diff  --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 7588440c3d8df6..89d6dcece5e108 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -591,6 +591,16 @@ add_libc_test(
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
+add_libc_test(
+  sync_test
+  SUITE
+    libc_unistd_unittests
+  SRCS
+    sync_test.cpp
+  DEPENDS
+    libc.src.unistd.sync
+)
+
 add_libc_test(
   tcgetpgrp_test
   SUITE

diff  --git a/libc/test/src/unistd/sync_test.cpp b/libc/test/src/unistd/sync_test.cpp
new file mode 100644
index 00000000000000..bea524d50c8bf8
--- /dev/null
+++ b/libc/test/src/unistd/sync_test.cpp
@@ -0,0 +1,20 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for sync.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/sync.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcSyncTest, SmokeTest) {
+  // sync always succeeds. So, we just call it as a smoke test.
+  LIBC_NAMESPACE::sync();
+}


        


More information about the libc-commits mailing list