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

Aman Maurya via libc-commits libc-commits at lists.llvm.org
Sat Oct 10 23:58:57 PDT 2026


https://github.com/amanmaurya92 created https://github.com/llvm/llvm-project/pull/230908

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

Fixes #230854

>From 3f5d94a33ff6567cec1e33b2d4d6d4c5e197cfb4 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sun, 11 Oct 2026 03:33:35 +0530
Subject: [PATCH] [libc] Implement sync in unistd (#230854)

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

Fixes #230854
---
 libc/config/linux/aarch64/entrypoints.txt     |  1 +
 libc/config/linux/arm/entrypoints.txt         |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  1 +
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 libc/include/unistd.yaml                      |  6 ++++
 .../linux/syscall_wrappers/CMakeLists.txt     | 11 +++++++
 .../OSUtil/linux/syscall_wrappers/sync.h      | 30 +++++++++++++++++++
 libc/src/unistd/CMakeLists.txt                |  7 +++++
 libc/src/unistd/linux/CMakeLists.txt          | 12 ++++++++
 libc/src/unistd/linux/sync.cpp                | 19 ++++++++++++
 libc/src/unistd/sync.h                        | 20 +++++++++++++
 libc/test/src/unistd/CMakeLists.txt           | 10 +++++++
 libc/test/src/unistd/sync_test.cpp            | 15 ++++++++++
 13 files changed, 134 insertions(+)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/sync.h
 create mode 100644 libc/src/unistd/linux/sync.cpp
 create mode 100644 libc/src/unistd/sync.h
 create mode 100644 libc/test/src/unistd/sync_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index b8a9a5cff2040..2df6bbd07a722 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 ac66f1f518484..4e535fe5111d6 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 0728bd41a40f9..06068b75ab8bf 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 2df3fb0dfb363..40727d821bcfb 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 92951152b7ca7..160b909f3af7b 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 a3f56b1117c2b..978a29a3384d0 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 0000000000000..aea7faeca1c14
--- /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_SYSCALL_WRAPPERS_SYNC_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_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_SYSCALL_WRAPPERS_SYNC_H
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index a9228513c0312..795224f0e9df4 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 2555c9b700982..3624f4e1891f2 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 0000000000000..0ed28984ae57f
--- /dev/null
+++ b/libc/src/unistd/linux/sync.cpp
@@ -0,0 +1,19 @@
+//===-- Linux implementation of sync -------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#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 0000000000000..aa276afb936c2
--- /dev/null
+++ b/libc/src/unistd/sync.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for sync --------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#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 7588440c3d8df..89d6dcece5e10 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 0000000000000..ac5d1649736d5
--- /dev/null
+++ b/libc/test/src/unistd/sync_test.cpp
@@ -0,0 +1,15 @@
+//===-- Unittests for sync ------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#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